Imports i8092_NET
Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents Button1 As System.Windows.Forms.Button

#Region " Windows Form 設計工具產生的程式碼 "

    Public Sub New()

        MyBase.New()

        '此為 Windows Form 設計工具所需的呼叫。
        InitializeComponent()

        '在 InitializeComponent() 呼叫之後加入所有的初始設定

    End Sub

    'Form 覆寫 Dispose 以清除元件清單。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
    End Sub

    '注意: 以下為 Windows Form 設計工具所需的程序
    '您可以使用 Windows Form 設計工具進行修改。
    '請勿使用程式碼編輯器來修改這些程序。
    Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(48, 24)
        Me.Button1.Size = New System.Drawing.Size(144, 40)
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(394, 303)
        Me.Controls.Add(Me.Button1)
        Me.Text = "Form1"

    End Sub

    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub

#End Region


    '// Axis define
    Public AXIS_X As UInt16 = Convert.ToUInt16(&H1)
    Public AXIS_Y As UInt16 = Convert.ToUInt16(&H2)
    Public AXIS_XY As UInt16 = Convert.ToUInt16(&H3)

    Public Const YES = 1
    Public Const NO = 0


    Dim Driver_Open As Boolean = False
    Dim cardNo As Byte




    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '====='Step 1 Driver init
        If Not Driver_Open Then
            i8092MF.i8092MF_REGISTRATION(cardNo, 1)
            Driver_Open = True
        End If
        '====='Step 2 CONFIG IO
        i8092MF.i8092MF_RESET_CARD(cardNo)
        i8092MF.i8092MF_SET_PULSE_MODE(cardNo, AXIS_XY, 2)  'set the pulse output mode
        i8092MF.i8092MF_SET_ALARM(cardNo, AXIS_XY, 0, 0)  'disable the SERVO ALARM Input
        i8092MF.i8092MF_SET_ENCODER(cardNo, AXIS_XY, 0, 0, 0) 'set the encoder input type
        i8092MF.i8092MF_SET_MAX_V(cardNo, AXIS_XY, Convert.ToUInt32(16000))  'set the max speed for XY
        i8092MF.i8092MF_EXD_DISABLE(cardNo, AXIS_XY)   'set the external input Off
        i8092MF.i8092MF_SET_LP(cardNo, AXIS_XY, 0)     'set the Logic position =0
        i8092MF.i8092MF_SET_EP(cardNo, AXIS_XY, 0)     'set the Encoger position =0
        i8092MF.i8092MF_SET_A(cardNo, AXIS_XY, Convert.ToUInt32(1000))   'set the Acc =1000
        i8092MF.i8092MF_SERVO_ON(cardNo, AXIS_XY)    'set the Servo_ON to servo motors
        '======'Step 3 Check ERROR
        Dim KK As Long = 0
        KK = i8092MF.i8092MF_GET_ERROR(cardNo)
        Dim MSGG As String
        If (KK <> YES) Then
            'No ERROR: Step 4 Move X axis
            Dim axis As UInt16 = AXIS_X 'for AXIS_X it can be to AXIS_XY
            i8092MF.i8092MF_SET_MAX_V(cardNo, axis, Convert.ToUInt32(20000))
            i8092MF.i8092MF_NORMAL_SPEED(cardNo, axis, Convert.ToUInt16(0))  'set axis as Symmetrical T curve mode
            i8092MF.i8092MF_SET_V(cardNo, axis, Convert.ToUInt32(20000))  'set v=10000 PPS
            i8092MF.i8092MF_SET_A(cardNo, axis, Convert.ToUInt32(100000))  'set acc=100000 PPS/S
            i8092MF.i8092MF_SET_SV(cardNo, axis, Convert.ToUInt32(10))   'set start speed=1000 PPS
            i8092MF.i8092MF_SET_AO(cardNo, axis, 0)   'set offset pulse (at SV speed)= 0 PS
            i8092MF.i8092MF_FIXED_MOVE(cardNo, axis, 10000) 'run the fixed 10000 Pulse move.
            Do While (i8092MF.i8092MF_STOP_WAIT(cardNo, axis) = NO)
                i8092MF.system.DoEvents()
                System.Threading.Thread.Sleep(1)
                'wait for axis to stop
            Loop
            Dim AA As Long = i8092MF.i8092MF_GET_LP(cardNo, axis)   'Get X Now position
        Else

            'Please check the ERROR CODE
            'Get X ERROR CODE
            KK = Convert.ToInt32(i8092MF.i8092MF_GET_ERROR_CODE(cardNo, AXIS_X))
            'Get Y ERROR CODE
            KK = Convert.ToInt32(i8092MF.i8092MF_GET_ERROR_CODE(cardNo, AXIS_Y))
            '====================================
        End If
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
