Imports System.IO
Public Class Form1
    Dim devhandle As UShort
    Dim axishandle As UShort
    ''' <summary>
    ''' 错误信息显示
    ''' </summary>
    ''' <param name="msg"></param>
    ''' <param name="rtn"></param>
    Private Sub SendErrorMsg(msg As String, rtn As Short)
        If rtn <> 0 Then
            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_Click(sender As Object, e As EventArgs) Handles Button4.Click
        '清除错误状态
        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_PTP_PRF_MODE)
        SendErrorMsg("NMC_MtSetPrfMode", rtn)
        Dim ptpPara As GC.Frame.Motion.Privt.CNMCLib20.TPtpPara
        ptpPara.acc = Convert.ToDouble(TextBox3.Text)
        ptpPara.dec = Convert.ToDouble(TextBox3.Text)
        ptpPara.endVel = 0
        ptpPara.smoothCoef = 0
        ptpPara.startVel = 0
        '设置运动参数
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetPtpPara(axishandle, ptpPara)
        SendErrorMsg("NMC_MtSetPtpPara", rtn)
        '设置目标速度
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetVel(axishandle, Convert.ToDouble(TextBox2.Text))
        SendErrorMsg("NMC_MtSetVel", rtn)
        '设置目标位置
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetPtpTgtPos(axishandle, Convert.ToInt32(TextBox1.Text))
        SendErrorMsg("NMC_MtSetPtpTgtPos", rtn)
        '启动运动
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtUpdate(axishandle)
        SendErrorMsg("NMC_MtUpdate", rtn)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        '停止运动
        Dim rtn As Short = 0
        rtn = 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)
        TextBox4.Text = pos.ToString()
    End Sub
End Class
