Public Class Form1
    Dim devhandle As UShort
    Dim crdhandle As UShort
    Dim axishandle As UShort() = New UShort(3) {}
    Dim seg As Integer = 0
    Private Sub SendErrorMsg(msg As String, rtn As Short)
        If rtn <> 0 Then
            Me.RichTextBox1.AppendText(msg + ":" + rtn.ToString() + vbCrLf)
        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
        For i As Integer = 0 To 2
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtOpen(devhandle, i, axishandle(i))
            SendErrorMsg("NMC_MtOpen", rtn)
            '清除轴状态
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtClrError(axishandle(i))
            SendErrorMsg("NMC_MtClrError", rtn)
            '位置清零
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtZeroPos(axishandle(i))
            SendErrorMsg("NMC_MtZeroPos", rtn)
        Next
        '创建坐标系
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdOpen(devhandle, crdhandle)
        SendErrorMsg("NMC_CrdOpen", rtn)
        '配置坐标系
        Dim crd As GC.Frame.Motion.Privt.CNMCLib20.TCrdConfig = New GC.Frame.Motion.Privt.CNMCLib20.TCrdConfig(True)  '轴配置
        Dim crdPara As GC.Frame.Motion.Privt.CNMCLib20.TCrdPara = New GC.Frame.Motion.Privt.CNMCLib20.TCrdPara(True) '运动参数
        crd.axCnts = 3
        crd.pAxis(0) = 0
        crd.pAxis(1) = 1
        crd.pAxis(2) = 2
        crd.pAxis(3) = -1
        crd.port(0) = 0
        crd.port(1) = 0
        crd.port(2) = 0
        crd.port(3) = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdConfig(crdhandle, crd)
        SendErrorMsg("NMC_CrdConfig", rtn)
        crdPara.orgFlag = 0 '默认原点（0，0，0）
        crdPara.synAccMax = 50 '最大合成加速度p/ms^2,加速度为10已经是比较大的加速度了
        crdPara.synVelMax = 800 '最大合成速度,在轴允许范围内，这两个参数可以尽量大一些
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdSetPara(crdhandle, crdPara)
        SendErrorMsg("NMC_CrdSetPara", rtn)
        Timer1.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim rtn As Short
        For i As Integer = 0 To 2
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetSvOn(axishandle(i))
            SendErrorMsg("NMC_MtSetSvOn" + "轴" + i.ToString(), rtn)
        Next
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim rtn As Short
        Dim pos As Integer() = New Integer(2) {0, 0, 0}
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdGetPrfPos(crdhandle, 3, pos(0))
        TextBox1.Text = pos(0).ToString()
        TextBox2.Text = pos(1).ToString()
        TextBox3.Text = pos(2).ToString()

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim rtn As Short
        For i As Integer = 0 To 2
            rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetSvOff(axishandle(i))
            SendErrorMsg("NMC_MtSetSvOn" + "轴" + i.ToString(), rtn)
        Next
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim rtn As Short
        '清空缓存区
        seg = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdBufClr(crdhandle)
        '插入直线
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdLineXYZ(crdhandle, seg, 3, New Integer(1) {10000, 10000}, 0, 10, 1) 'X Y运动
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdLineXYZ(crdhandle, seg, 7, New Integer(2) {20000, 0, 10000}, 0, 10, 1) 'X Y运动
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdLineXYZ(crdhandle, seg, 7, New Integer(2) {0, 0, 0}, 0, 10, 1) 'X Y运动
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        '插入三点圆弧
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdArcPPP(crdhandle, seg, New Integer(1) {10000, 10000}, New Integer(1) {20000, 0}, 0, 10, 1)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdArcPPP(crdhandle, seg, New Integer(1) {10000, -10000}, New Integer(1) {0, 0}, 0, 10, 1)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        '插入圆心坐标系圆弧
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdArcCenter(crdhandle, seg, New Integer(1) {0, 0}, New Integer(1) {10000, 0}, 0, 0, 10, 1)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        '插入半径圆弧
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdArcRadius(crdhandle, seg, New Integer(1) {20000, 0}, 10000, 0, 0, 10, 1)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdArcRadius(crdhandle, seg, New Integer(1) {0, 0}, 10000, 0, 0, 10, 1)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        '插入IO,index=0输出低电平
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdBufDo(crdhandle, seg, 3, 0, 0)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdLineXYZ(crdhandle, seg, 3, New Integer(1) {0, 0}, 0, 10, 1) 'X Y运动
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdBufDo(crdhandle, seg, 3, 0, 1)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        '出入延时2s
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdBufDelay(crdhandle, seg, 0, 2000)
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        seg = seg + 1
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdLineXYZ(crdhandle, seg, 3, New Integer(1) {10000, 10000}, 0, 10, 1) 'X Y运动
        SendErrorMsg("指令插入第" + seg.ToString() + "行NG", rtn)
        '结束压指令
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdEndMtn(crdhandle)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Dim rtn As Short
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdStartMtn(crdhandle)
        SendErrorMsg("NMC_CrdStartMtn", rtn)
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim rtn As Short
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_CrdStopMtn(crdhandle)
    End Sub
End Class
