Public Class Form1

    Private Sub btnEnable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnable.Click, btnDisable.Click
        If AxMotecDriverCmd1.IsOpenCom() Then
            If sender.name = "btnEnable" Then
                AxMotecDriverCmd1.PutMotorEnableStatus(1)
                btnEnable.BackColor = Color.LimeGreen
                btnDisable.BackColor = Color.Gray
            Else
                AxMotecDriverCmd1.PutMotorEnableStatus(0)
                btnDisable.BackColor = Color.LimeGreen
                btnEnable.BackColor = Color.Gray
            End If
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''初始化
        cbCom.SelectedIndex = 3
        cbBaud.SelectedIndex = 3
        btnConnect.BackColor = Color.Red
        btnDisable.BackColor = Color.LimeGreen
        btnEnable.BackColor = Color.Gray
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Dim bResult As Boolean
        If btnConnect.Text = "连接" Then
            bResult = AxMotecDriverCmd1.OpenCom(cbCom.SelectedIndex + 1, Int(cbBaud.Text), 8, 0, 0) ''返回0 连接成功
            bResult = AxMotecDriverCmd1.EchoTesting ''必须使用此句确定驱动器的校验方式
            If bResult Then
                btnConnect.Text = "断开"
                btnConnect.BackColor = Color.Lime
                Timer1.Enabled = True
            End If
        Else
            AxMotecDriverCmd1.CloseCom()
            btnConnect.Text = "连接"
            btnConnect.BackColor = Color.Red
            Timer1.Enabled = False
        End If
    End Sub

    Private Sub btnSetVel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetVel.Click

        AxMotecDriverCmd1.PutSProfileParameter(Int(txtVel.Text), 50, 50)
    End Sub

    Private Sub btnRelative_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRelative.Click
        AxMotecDriverCmd1.MoveRelative(Int(txtDis.Text))
        AxMotecDriverCmd1.Go()
    End Sub

    Private Sub btnAbsolute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbsolute.Click
        AxMotecDriverCmd1.MoveAbsolute(Int(txtDis.Text))
        AxMotecDriverCmd1.Go()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim fVel1, fVel2 As Single
        AxMotecDriverCmd1.GetVelocity(fVel1, fVel2)
        txtDisDisp.Text = fVel1.ToString
        Dim nPos1, nPos2 As Integer
        AxMotecDriverCmd1.GetPosition(nPos1, nPos2)
        txtVelDisp.Text = nPos1.ToString()
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        AxMotecDriverCmd1.QuickStop()
    End Sub
End Class
