www.pudn.com > E516_Stepper-motor-control-program.zip > MainForm.vb, change:2015-04-04,size:8852b


Public Class MainForm 
    Dim GPIBAddress As Integer = 4 
    Dim ID As Short 
    Dim fileName As String 
    Dim dataCount As Integer 
    Dim dataPos(,) As Double 
    Dim outputCount As Integer 
    Dim bInvert As Boolean 
    Dim bAbsolute As Boolean 
    Dim bNano As Boolean 
    Dim total_line As Integer 
 
 
 
    Private Sub MainForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
        E516_CloseConnection(ID) 
    End Sub 
    Private Sub LaserFabrication_Close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing 
        CH375CloseDevice(0) 
    End Sub 
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        Dim testFlag As Boolean 
        ID = E516_ConnectNIgpib(0, GPIBAddress) 
        testFlag = E516_SVO(ID, {"A", "B", "C"}, {True, True, True}) 
        CH375OpenDevice(0) 
    End Sub 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXMov.Click 
        Try 
            If txtXPos.Text >= 0 And txtXPos.Text <= 100 Then 
                E516_MOV(ID, "A", {txtXPos.Text}) 
            Else 
                MsgBox("请输入正确的X范围:0-100") 
            End If 
        Catch ex As Exception 
            MsgBox("请输入正确的X范围:0-100") 
        End Try 
    End Sub 
 
    Private Sub cmdYMov_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdYMov.Click 
        Try 
            If txtYPos.Text >= 0 And txtYPos.Text <= 100 Then 
                E516_MOV(ID, "B", {txtYPos.Text}) 
            Else 
                MsgBox("请输入正确的Y范围:0-100") 
            End If 
        Catch ex As Exception 
            MsgBox("请输入正确的Y范围:0-100") 
        End Try 
    End Sub 
 
    Private Sub cmdZMov_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdZMov.Click 
        Try 
            If txtZPos.Text >= 0 And txtZPos.Text <= 20 Then 
                E516_MOV(ID, "C", {txtZPos.Text}) 
            Else 
                MsgBox("请输入正确的Z范围:0-20") 
            End If 
        Catch ex As Exception 
            MsgBox("请输入正确的Z范围:0-20") 
        End Try 
    End Sub 
 
    Private Sub cmdAllMov_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAllMov.Click 
        Try 
            If txtXPos.Text >= 0 And txtXPos.Text <= 100 And txtYPos.Text >= 0 And txtYPos.Text <= 100 And txtZPos.Text >= 0 And txtZPos.Text <= 20 Then 
                E516_MOV(ID, "ABC", {txtXPos.Text, txtYPos.Text, txtZPos.Text}) 
            Else 
                MsgBox("请输入正确的X范围:0-100" & vbCrLf & "请输入正确的Y范围:0-100" & vbCrLf & "请输入正确的Z范围:0-20") 
            End If 
        Catch ex As Exception 
            MsgBox("请输入正确的X范围:0-100" & vbCrLf & "请输入正确的Y范围:0-100" & vbCrLf & "请输入正确的Z范围:0-20") 
        End Try 
    End Sub 
 
 
    Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click 
        OpenFileDialog1.FileName = "" 
        OpenFileDialog1.Filter = "数据文件(*.txt)|*.txt" 
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then 
            If OpenFileDialog1.FileName = "" Then 
                Exit Sub 
            End If 
            fileName = OpenFileDialog1.FileName 
        Else 
            Exit Sub 
        End If 
        dataCount = 0 
        Dim srPre As IO.StreamReader = New IO.StreamReader(fileName) 
        Dim sr As IO.StreamReader = New IO.StreamReader(fileName) 
        Dim strTemp As String 
        Dim strArrayTemp(2) As String 
        ReDim strArrayTemp(2) 
        Do Until srPre.EndOfStream 
            strTemp = srPre.ReadLine() 
            dataCount = dataCount + 1 
        Loop 
        Dim i As Integer 
        ReDim dataPos(dataCount - 1, 2) 
        Do Until sr.EndOfStream 
            Try 
                strTemp = sr.ReadLine() 
                strArrayTemp = strTemp.Split(",") 
                dataPos(i, 0) = strArrayTemp(0) : dataPos(i, 1) = strArrayTemp(1) : dataPos(i, 2) = strArrayTemp(2) 
                i = i + 1 
            Catch ex As Exception 
                MsgBox("数据读入错误") 
                btnContinuousMov.Enabled = False 
                Exit Sub 
            End Try 
        Loop 
        total_line = i 
        btnContinuousMov.Enabled = True 
    End Sub 
 
    Private Sub btnContinuousMov_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContinuousMov.Click 
        Try 
            If txtTimePerStep.Text > 0 Then 
                tmrOutputTick.Interval = txtTimePerStep.Text 
            Else 
                MsgBox("请输入正确的单步时间范围") 
                Exit Sub 
            End If 
            outputCount = 0 
            bInvert = chkInvert.Checked 
            bAbsolute = chkAbsolute.Checked 
            If rdoNano.Checked = True Then 
                bNano = True 
            Else 
                bNano = False 
            End If 
            tmrOutputTick.Enabled = True 
            btnContinuousMov.Enabled = False 
            Shutter_open() 
        Catch ex As Exception 
            MsgBox("单步时间格式不正确") 
        End Try 
    End Sub 
 
    Private Sub tmrOutputTick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrOutputTick.Tick 
        If outputCount < dataCount Then 
            If bAbsolute = False Then 
                If bInvert = False Then 
                    If bNano = False Then 
                        E516_MOV(ID, "AB", {dataPos(outputCount, 0), dataPos(outputCount, 1)}) 
                        If outputCount > 0 Then 
                            E516_MVR(ID, "C", {dataPos(outputCount - 1, 2) - dataPos(outputCount, 2)}) 
                        End If 
                    Else 
                        E516_MOV(ID, "AB", {dataPos(outputCount, 0) / 1000, dataPos(outputCount, 1) / 1000}) 
                        If outputCount > 0 Then 
                            E516_MVR(ID, "C", {dataPos(outputCount - 1, 2) / 1000 - dataPos(outputCount, 2) / 1000}) 
                        End If 
                    End If 
                Else 
                    If bNano = False Then 
                        E516_MOV(ID, "AB", {dataPos(outputCount, 0), dataPos(outputCount, 1)}) 
                        If outputCount > 0 Then 
                            E516_MVR(ID, "C", {dataPos(outputCount, 2) - dataPos(outputCount - 1, 2)}) 
                        End If 
                    Else 
                        E516_MOV(ID, "AB", {dataPos(outputCount, 0) / 1000, dataPos(outputCount, 1) / 1000}) 
                        If outputCount > 0 Then 
                            E516_MVR(ID, "C", {dataPos(outputCount, 2) / 1000 - dataPos(outputCount - 1, 2) / 1000}) 
                        End If 
                    End If 
                End If 
            Else 
                If bNano = False Then 
                    E516_MOV(ID, "ABC", {dataPos(outputCount, 0), dataPos(outputCount, 1), dataPos(outputCount, 2)}) 
                Else 
                    E516_MOV(ID, "ABC", {dataPos(outputCount, 0) / 1000, dataPos(outputCount, 1) / 1000, dataPos(outputCount, 2) / 1000}) 
                End If 
            End If 
        Else 
            tmrOutputTick.Enabled = False 
            btnContinuousMov.Enabled = True 
            Shutter_close() 
        End If 
        outputCount = outputCount + 1 
    End Sub 
 
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged 
        If CheckBox1.Checked = True Then 
            Shutter_open() 
        Else 
            Shutter_close() 
        End If 
    End Sub 
 
    Private Sub MOVEthenOPEN_Click(sender As Object, e As EventArgs) Handles MOVEthenOPEN.Click 
        For j = 0 To total_line Step 1 
            'E516_MOV(ID, "B", {txtYPos.Text}) 
            'E516_MOV(ID, "B", {txtYPos.Text}) 
            'E516_MOV(ID, "B", {txtYPos.Text}) 
            'E516_MOV(ID, "ABC", {txtXPos.Text, txtYPos.Text, txtZPos.Text}) 
            tmrOutputTick.Enabled = False 
 
            Shutter_close() 
 
            E516_MOV(ID, "ABC", {dataPos(j, 0), dataPos(j, 1), dataPos(j, 2)}) 
            Timer1.Interval = True 
            Shutter_open() 
 
 
        Next 
 
    End Sub 
 
 
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
        Timer1.Interval = txtTimePerStep.Text 
        Timer1.Enabled = False 
 
        Shutter_close() 
 
    End Sub 
End Class