Imports i8092_NET
Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox

#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 設計工具進行修改。
    '請勿使用程式碼編輯器來修改這些程序。
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Label5 = New System.Windows.Forms.Label
        Me.Label6 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.TextBox3 = New System.Windows.Forms.TextBox
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular)
        Me.Label1.Location = New System.Drawing.Point(40, 24)
        Me.Label1.Size = New System.Drawing.Size(64, 24)
        Me.Label1.Text = "Step 1:"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular)
        Me.Label2.Location = New System.Drawing.Point(40, 64)
        Me.Label2.Size = New System.Drawing.Size(64, 24)
        Me.Label2.Text = "Step 2:"
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular)
        Me.Label3.Location = New System.Drawing.Point(40, 104)
        Me.Label3.Size = New System.Drawing.Size(64, 24)
        Me.Label3.Text = "Step 3:"
        '
        'Label4
        '
        Me.Label4.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular)
        Me.Label4.Location = New System.Drawing.Point(40, 184)
        Me.Label4.Size = New System.Drawing.Size(64, 24)
        Me.Label4.Text = "Step 4:"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(120, 24)
        Me.Button1.Size = New System.Drawing.Size(128, 32)
        Me.Button1.Text = "Driver init"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(120, 64)
        Me.Button2.Size = New System.Drawing.Size(128, 32)
        Me.Button2.Text = "Config IO"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(120, 104)
        Me.Button3.Size = New System.Drawing.Size(128, 32)
        Me.Button3.Text = "Check ERROR"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(120, 176)
        Me.Button4.Size = New System.Drawing.Size(128, 32)
        Me.Button4.Text = "Move X axis"
        '
        'Label5
        '
        Me.Label5.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular)
        Me.Label5.Location = New System.Drawing.Point(120, 144)
        Me.Label5.Size = New System.Drawing.Size(160, 24)
        Me.Label5.Text = "Start Logic Position"
        '
        'Label6
        '
        Me.Label6.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Regular)
        Me.Label6.Location = New System.Drawing.Point(120, 216)
        Me.Label6.Size = New System.Drawing.Size(152, 24)
        Me.Label6.Text = "Now Logic Position"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(288, 136)
        Me.TextBox1.Size = New System.Drawing.Size(168, 22)
        Me.TextBox1.Text = "0"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(288, 216)
        Me.TextBox2.Size = New System.Drawing.Size(168, 22)
        Me.TextBox2.Text = "0"
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(40, 272)
        Me.TextBox3.Size = New System.Drawing.Size(416, 22)
        Me.TextBox3.Text = ""
        '
        'Form1
        '
        Me.ClientSize = New System.Drawing.Size(482, 399)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label6)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Label4)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        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
    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
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '====='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
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '======'Step 3 Check ERROR
        Dim KK As Long = i8092MF.i8092MF_GET_ERROR(cardNo)
        Dim MSGG As String
        If (KK = YES) Then
            'Please check the ERROR CODE
            'Get X ERROR CODE
            KK = Convert.ToInt32(i8092MF.i8092MF_GET_ERROR_CODE(cardNo, AXIS_X))
            MSGG = "Xerr:" & KK & "  "
            'Get Y ERROR CODE
            KK = Convert.ToInt32(i8092MF.i8092MF_GET_ERROR_CODE(cardNo, AXIS_Y))
            MSGG = MSGG & "Yerr:" & KK & "  "
            '====================================
            TextBox3.Text = MSGG
        Else
            TextBox3.Text = "No Error"
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'No ERROR: Step 4 Move X axis
        Dim axis As UInt16 = AXIS_X 'for AXIS_X it can be to AXIS_XY
        TextBox1.Text = i8092MF.i8092MF_GET_LP(cardNo, AXIS_X)
        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()                       'Get X Now position
            System.Threading.Thread.Sleep(1)
            TextBox2.Text = i8092MF.i8092MF_GET_LP(cardNo, AXIS_X)
            'wait for axis to stop
        Loop
        TextBox2.Text = i8092MF.i8092MF_GET_LP(cardNo, axis)    'Get X Now position
    End Sub

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

    End Sub
End Class
