Public Class Form1
    Dim devhandle As UShort
    Dim axishandle As UShort
    Private Sub SendErrorMsg(msg As String, rtn As Short)
        If rtn <> 0 Then
            GC.Frame.Motion.Privt.CNMCLib20.NMC_MtEstp(axishandle)
            MessageBox.Show(msg + "=" + rtn.ToString())
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim rtn As Short = 0
        '创建控制器句柄
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_DevOpenByID("CARD1", devhandle)
        SendErrorMsg("NMC_DevOpenByID", rtn)
        '加载控制器配置
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_LoadConfigFromFile(devhandle, System.Text.ASCIIEncoding.Default.GetBytes("GCN400.cfg"))
        SendErrorMsg("NMC_LoadConfigFromFile", rtn)
        '使能轴1
        '打开轴1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtOpen(devhandle, 0, axishandle)
        SendErrorMsg("NMC_MtOpen", rtn)
        '清除轴状态
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtClrError(axishandle)
        SendErrorMsg("NMC_MtClrError", rtn)
        '位置清零
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtZeroPos(axishandle)
        SendErrorMsg("NMC_MtZeroPos", rtn)
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        '使能
        Dim rtn As Short = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetSvOn(axishandle)
        SendErrorMsg("NMC_MtSetSvOn", rtn)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        '卸使能
        Dim rtn As Short = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetSvOff(axishandle)
        SendErrorMsg("NMC_MtSetSvOff", rtn)
    End Sub

    Private Sub Button4_MouseDown(sender As Object, e As MouseEventArgs) Handles Button4.MouseDown
        '清除错误状态
        Dim rtn As Short = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtClrError(axishandle)
        '设置运动模式
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetPrfMode(axishandle, GC.Frame.Motion.Privt.CNMCLib20.MT_JOG_PRF_MODE)
        SendErrorMsg("NMC_MtSetPrfMode", rtn)
        Dim ptpPara As GC.Frame.Motion.Privt.CNMCLib20.TJogPara
        ptpPara.acc = Convert.ToDouble(TextBox2.Text)
        ptpPara.dec = Convert.ToDouble(TextBox2.Text)
        ptpPara.smoothCoef = 0
        '设置运动参数
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetJogPara(axishandle, ptpPara)
        SendErrorMsg("NMC_MtSetJogPara", rtn)
        '设置目标速度
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetVel(axishandle, Convert.ToDouble(TextBox1.Text))
        SendErrorMsg("NMC_MtSetVel", rtn)
        '启动运动
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtUpdate(axishandle)
        SendErrorMsg("NMC_MtUpdate", rtn)
    End Sub

    Private Sub Button4_MouseUp(sender As Object, e As MouseEventArgs) Handles Button4.MouseUp
        '停止
        GC.Frame.Motion.Privt.CNMCLib20.NMC_MtStop(axishandle)
    End Sub

    Private Sub Button5_MouseDown(sender As Object, e As MouseEventArgs) Handles Button5.MouseDown
        '清除错误状态
        Dim rtn As Short = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtClrError(axishandle)
        '设置运动模式
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetPrfMode(axishandle, GC.Frame.Motion.Privt.CNMCLib20.MT_JOG_PRF_MODE)
        SendErrorMsg("NMC_MtSetPrfMode", rtn)
        Dim ptpPara As GC.Frame.Motion.Privt.CNMCLib20.TJogPara
        ptpPara.acc = Convert.ToDouble(TextBox2.Text)
        ptpPara.dec = Convert.ToDouble(TextBox2.Text)
        ptpPara.smoothCoef = 0
        '设置运动参数
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetJogPara(axishandle, ptpPara)
        SendErrorMsg("NMC_MtSetJogPara", rtn)
        '设置目标速度
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetVel(axishandle, -Convert.ToDouble(TextBox1.Text))
        SendErrorMsg("NMC_MtSetVel", rtn)
        '启动运动
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtUpdate(axishandle)
        SendErrorMsg("NMC_MtUpdate", rtn)
    End Sub

    Private Sub Button5_MouseUp(sender As Object, e As MouseEventArgs) Handles Button5.MouseUp
        '停止
        GC.Frame.Motion.Privt.CNMCLib20.NMC_MtStop(axishandle)
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim rtn As Short = 0
        Dim pos As Integer = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtGetPrfPos(axishandle, pos)
        TextBox3.Text = pos.ToString()
    End Sub
End Class
