Public Class Form1
    Dim devhandle As UShort
    Dim axishandle As UShort
    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 Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim rtn As Short = 0
        Dim pos As Integer = 0
        Dim sts As Integer = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtGetPrfPos(axishandle, pos)
        TextBox1.Text = pos.ToString()
        '回零状态字
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtGetHomeSts(axishandle, sts)
        Label1.Text = IIf((sts And (1 << 0)) = 0, "0--不在回零中", "1--回零中")
        Label2.Text = IIf((sts And (1 << 1)) = 0, "0--尚未回零完成", "1--已经回零完成")
        Label3.Text = IIf((sts And (1 << 2)) = 0, "0--正常", "1--回零失败")
        Label4.Text = IIf((sts And (1 << 3)) = 0, "0--正常", "1--参数错误导致失败")
        Label5.Text = IIf((sts And (1 << 4)) = 0, "0--正常", "1--搜寻中未触发开关")
    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 Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim rtn As Short = 0
        Dim homeSetup As GC.Frame.Motion.Privt.CNMCLib20.THomeSetting
        homeSetup.mode = GC.Frame.Motion.Privt.CNMCLib20.THomeMode.HM_MODE1       ' 回零模式
        homeSetup.dir = 0               ' 回零初始方向 : 负向
        homeSetup.offset = 0            ' 原点偏移
        homeSetup.scan1stVel = 5        '基本搜寻速度
        homeSetup.scan2ndVel = 1        ' 低速
        homeSetup.acc = 0.5             ' 加速度
        homeSetup.reScanEn = 1          ' 两次搜寻零点
        homeSetup.homeEdge = 0          ' 原点，触发沿,下降沿
        homeSetup.lmtEdge = 0           '
        homeSetup.zEdge = 0             '
        homeSetup.iniRetPos = 0         ' 起始反向运动距离
        homeSetup.retSwOffset = 0       ' 反向运动时离开开关距离
        homeSetup.safeLen = 0           ' 安全距离
        homeSetup.usePreSetPtpPara = 0  '当usePreSetPtpPara=0时，回零运动的减加速度默认等于acc,起跳速度、终点速度、平滑系数默认为0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtSetHomePara(axishandle, homeSetup)
        SendErrorMsg("NMC_MtSetHomePara", 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_MtHome(axishandle)
        SendErrorMsg("NMC_MtHome", rtn)
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim rtn As Short = 0
        rtn = GC.Frame.Motion.Privt.CNMCLib20.NMC_MtHomeStop(axishandle)
    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
End Class
