﻿Option Strict Off
Option Explicit On 
Imports System.Text

Friend Class Sync
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox5 As System.Windows.Forms.GroupBox
    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 Label5 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents Edit_S_DevName As System.Windows.Forms.TextBox
    Friend WithEvents Edit_M_DevName As System.Windows.Forms.TextBox
    Friend WithEvents Edit_M_TransCount As System.Windows.Forms.TextBox
    Friend WithEvents Edit_M_Status As System.Windows.Forms.TextBox
    Friend WithEvents Edit_M_Error As System.Windows.Forms.TextBox
    Friend WithEvents Edit_M_Return As System.Windows.Forms.TextBox
    Friend WithEvents Edit_S_TransCount As System.Windows.Forms.TextBox
    Friend WithEvents Edit_S_Status As System.Windows.Forms.TextBox
    Friend WithEvents Edit_S_Error As System.Windows.Forms.TextBox
    Friend WithEvents Edit_S_Return As System.Windows.Forms.TextBox
    Friend WithEvents Button_S_DioInit As System.Windows.Forms.Button
    Friend WithEvents Button_S_Condition As System.Windows.Forms.Button
    Friend WithEvents Button_S_Start As System.Windows.Forms.Button
    Friend WithEvents Button_S_Stop As System.Windows.Forms.Button
    Friend WithEvents Button_S_Data As System.Windows.Forms.Button
    Friend WithEvents Button_S_Count As System.Windows.Forms.Button
    Friend WithEvents Button_S_Status As System.Windows.Forms.Button
    Friend WithEvents Button_S_DioExit As System.Windows.Forms.Button
    Friend WithEvents Button_M_DioInit As System.Windows.Forms.Button
    Friend WithEvents Button_M_Condition As System.Windows.Forms.Button
    Friend WithEvents Button_M_Start As System.Windows.Forms.Button
    Friend WithEvents Button_M_Stop As System.Windows.Forms.Button
    Friend WithEvents Button_M_Data As System.Windows.Forms.Button
    Friend WithEvents Button_M_Count As System.Windows.Forms.Button
    Friend WithEvents Button_M_Status As System.Windows.Forms.Button
    Friend WithEvents Button_M_DioExit As System.Windows.Forms.Button
    Friend WithEvents Button_Exit As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Edit_S_DevName = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Edit_M_DevName = New System.Windows.Forms.TextBox()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.Edit_M_TransCount = New System.Windows.Forms.TextBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Edit_M_Status = New System.Windows.Forms.TextBox()
        Me.Edit_M_Error = New System.Windows.Forms.TextBox()
        Me.Edit_M_Return = New System.Windows.Forms.TextBox()
        Me.GroupBox3 = New System.Windows.Forms.GroupBox()
        Me.Edit_S_TransCount = New System.Windows.Forms.TextBox()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.Label9 = New System.Windows.Forms.Label()
        Me.Label10 = New System.Windows.Forms.Label()
        Me.Edit_S_Status = New System.Windows.Forms.TextBox()
        Me.Edit_S_Error = New System.Windows.Forms.TextBox()
        Me.Edit_S_Return = New System.Windows.Forms.TextBox()
        Me.GroupBox4 = New System.Windows.Forms.GroupBox()
        Me.Button_S_DioInit = New System.Windows.Forms.Button()
        Me.Button_S_Condition = New System.Windows.Forms.Button()
        Me.Button_S_Start = New System.Windows.Forms.Button()
        Me.Button_S_Stop = New System.Windows.Forms.Button()
        Me.Button_S_Data = New System.Windows.Forms.Button()
        Me.Button_S_Count = New System.Windows.Forms.Button()
        Me.Button_S_Status = New System.Windows.Forms.Button()
        Me.Button_S_DioExit = New System.Windows.Forms.Button()
        Me.GroupBox5 = New System.Windows.Forms.GroupBox()
        Me.Button_M_DioInit = New System.Windows.Forms.Button()
        Me.Button_M_Condition = New System.Windows.Forms.Button()
        Me.Button_M_Start = New System.Windows.Forms.Button()
        Me.Button_M_Stop = New System.Windows.Forms.Button()
        Me.Button_M_Data = New System.Windows.Forms.Button()
        Me.Button_M_Count = New System.Windows.Forms.Button()
        Me.Button_M_Status = New System.Windows.Forms.Button()
        Me.Button_M_DioExit = New System.Windows.Forms.Button()
        Me.Button_Exit = New System.Windows.Forms.Button()
        Me.GroupBox1.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.GroupBox4.SuspendLayout()
        Me.GroupBox5.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Edit_S_DevName, Me.Label1, Me.Label2, Me.Edit_M_DevName})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 10)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(328, 54)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Device Name:"
        '
        'Edit_S_DevName
        '
        Me.Edit_S_DevName.Location = New System.Drawing.Point(224, 23)
        Me.Edit_S_DevName.Name = "Edit_S_DevName"
        Me.Edit_S_DevName.Size = New System.Drawing.Size(88, 20)
        Me.Edit_S_DevName.TabIndex = 2
        Me.Edit_S_DevName.Text = "TextBox1"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(174, 25)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(40, 17)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Slave:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 23)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(48, 17)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "Master:"
        '
        'Edit_M_DevName
        '
        Me.Edit_M_DevName.Location = New System.Drawing.Point(64, 23)
        Me.Edit_M_DevName.Name = "Edit_M_DevName"
        Me.Edit_M_DevName.Size = New System.Drawing.Size(96, 20)
        Me.Edit_M_DevName.TabIndex = 1
        Me.Edit_M_DevName.Text = "TextBox1"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.Edit_M_TransCount, Me.Label3, Me.Label4, Me.Label5, Me.Label6, Me.Edit_M_Status, Me.Edit_M_Error, Me.Edit_M_Return})
        Me.GroupBox2.Location = New System.Drawing.Point(8, 70)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(328, 140)
        Me.GroupBox2.TabIndex = 0
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Master Status"
        '
        'Edit_M_TransCount
        '
        Me.Edit_M_TransCount.Location = New System.Drawing.Point(80, 15)
        Me.Edit_M_TransCount.Name = "Edit_M_TransCount"
        Me.Edit_M_TransCount.Size = New System.Drawing.Size(240, 20)
        Me.Edit_M_TransCount.TabIndex = 1
        Me.Edit_M_TransCount.Text = "TextBox3"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(9, 18)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(40, 13)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "Count:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(9, 46)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(47, 15)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "Status:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(9, 74)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(39, 17)
        Me.Label5.TabIndex = 0
        Me.Label5.Text = "Error:"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(9, 105)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(72, 19)
        Me.Label6.TabIndex = 0
        Me.Label6.Text = "Return Code:"
        '
        'Edit_M_Status
        '
        Me.Edit_M_Status.Location = New System.Drawing.Point(80, 44)
        Me.Edit_M_Status.Name = "Edit_M_Status"
        Me.Edit_M_Status.Size = New System.Drawing.Size(240, 20)
        Me.Edit_M_Status.TabIndex = 1
        Me.Edit_M_Status.Text = "TextBox3"
        '
        'Edit_M_Error
        '
        Me.Edit_M_Error.Location = New System.Drawing.Point(80, 73)
        Me.Edit_M_Error.Name = "Edit_M_Error"
        Me.Edit_M_Error.Size = New System.Drawing.Size(240, 20)
        Me.Edit_M_Error.TabIndex = 1
        Me.Edit_M_Error.Text = "TextBox3"
        '
        'Edit_M_Return
        '
        Me.Edit_M_Return.Location = New System.Drawing.Point(80, 102)
        Me.Edit_M_Return.Name = "Edit_M_Return"
        Me.Edit_M_Return.Size = New System.Drawing.Size(240, 20)
        Me.Edit_M_Return.TabIndex = 1
        Me.Edit_M_Return.Text = "TextBox3"
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.Edit_S_TransCount, Me.Label7, Me.Label8, Me.Label9, Me.Label10, Me.Edit_S_Status, Me.Edit_S_Error, Me.Edit_S_Return})
        Me.GroupBox3.Location = New System.Drawing.Point(8, 218)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(328, 139)
        Me.GroupBox3.TabIndex = 0
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Slave Status"
        '
        'Edit_S_TransCount
        '
        Me.Edit_S_TransCount.Location = New System.Drawing.Point(80, 19)
        Me.Edit_S_TransCount.Name = "Edit_S_TransCount"
        Me.Edit_S_TransCount.Size = New System.Drawing.Size(240, 20)
        Me.Edit_S_TransCount.TabIndex = 1
        Me.Edit_S_TransCount.Text = "TextBox7"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(8, 105)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(72, 18)
        Me.Label7.TabIndex = 0
        Me.Label7.Text = "Return Code:"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(9, 19)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(40, 13)
        Me.Label8.TabIndex = 0
        Me.Label8.Text = "Count:"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(8, 47)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(41, 17)
        Me.Label9.TabIndex = 0
        Me.Label9.Text = "Status:"
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(8, 74)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(33, 14)
        Me.Label10.TabIndex = 0
        Me.Label10.Text = "Error:"
        '
        'Edit_S_Status
        '
        Me.Edit_S_Status.Location = New System.Drawing.Point(80, 47)
        Me.Edit_S_Status.Name = "Edit_S_Status"
        Me.Edit_S_Status.Size = New System.Drawing.Size(240, 20)
        Me.Edit_S_Status.TabIndex = 1
        Me.Edit_S_Status.Text = "TextBox7"
        '
        'Edit_S_Error
        '
        Me.Edit_S_Error.Location = New System.Drawing.Point(80, 74)
        Me.Edit_S_Error.Name = "Edit_S_Error"
        Me.Edit_S_Error.Size = New System.Drawing.Size(240, 20)
        Me.Edit_S_Error.TabIndex = 1
        Me.Edit_S_Error.Text = "TextBox7"
        '
        'Edit_S_Return
        '
        Me.Edit_S_Return.Location = New System.Drawing.Point(80, 102)
        Me.Edit_S_Return.Name = "Edit_S_Return"
        Me.Edit_S_Return.Size = New System.Drawing.Size(240, 20)
        Me.Edit_S_Return.TabIndex = 1
        Me.Edit_S_Return.Text = "TextBox7"
        '
        'GroupBox4
        '
        Me.GroupBox4.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_S_DioInit, Me.Button_S_Condition, Me.Button_S_Start, Me.Button_S_Stop, Me.Button_S_Data, Me.Button_S_Count, Me.Button_S_Status, Me.Button_S_DioExit})
        Me.GroupBox4.Location = New System.Drawing.Point(480, 10)
        Me.GroupBox4.Name = "GroupBox4"
        Me.GroupBox4.Size = New System.Drawing.Size(129, 346)
        Me.GroupBox4.TabIndex = 1
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "Slave:"
        '
        'Button_S_DioInit
        '
        Me.Button_S_DioInit.Location = New System.Drawing.Point(11, 18)
        Me.Button_S_DioInit.Name = "Button_S_DioInit"
        Me.Button_S_DioInit.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_DioInit.TabIndex = 4
        Me.Button_S_DioInit.Text = "DioInit"
        '
        'Button_S_Condition
        '
        Me.Button_S_Condition.Location = New System.Drawing.Point(11, 59)
        Me.Button_S_Condition.Name = "Button_S_Condition"
        Me.Button_S_Condition.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_Condition.TabIndex = 4
        Me.Button_S_Condition.Text = "Set Condition..."
        '
        'Button_S_Start
        '
        Me.Button_S_Start.Location = New System.Drawing.Point(11, 100)
        Me.Button_S_Start.Name = "Button_S_Start"
        Me.Button_S_Start.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_Start.TabIndex = 4
        Me.Button_S_Start.Text = "Start  Transfer "
        '
        'Button_S_Stop
        '
        Me.Button_S_Stop.Location = New System.Drawing.Point(11, 141)
        Me.Button_S_Stop.Name = "Button_S_Stop"
        Me.Button_S_Stop.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_Stop.TabIndex = 4
        Me.Button_S_Stop.Text = "Stop Transfer"
        '
        'Button_S_Data
        '
        Me.Button_S_Data.Location = New System.Drawing.Point(11, 182)
        Me.Button_S_Data.Name = "Button_S_Data"
        Me.Button_S_Data.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_Data.TabIndex = 4
        Me.Button_S_Data.Text = "Get Data..."
        '
        'Button_S_Count
        '
        Me.Button_S_Count.Location = New System.Drawing.Point(11, 223)
        Me.Button_S_Count.Name = "Button_S_Count"
        Me.Button_S_Count.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_Count.TabIndex = 4
        Me.Button_S_Count.Text = "Transfer Count"
        '
        'Button_S_Status
        '
        Me.Button_S_Status.Location = New System.Drawing.Point(11, 264)
        Me.Button_S_Status.Name = "Button_S_Status"
        Me.Button_S_Status.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_Status.TabIndex = 4
        Me.Button_S_Status.Text = "Transfer Status"
        '
        'Button_S_DioExit
        '
        Me.Button_S_DioExit.Location = New System.Drawing.Point(11, 305)
        Me.Button_S_DioExit.Name = "Button_S_DioExit"
        Me.Button_S_DioExit.Size = New System.Drawing.Size(104, 28)
        Me.Button_S_DioExit.TabIndex = 4
        Me.Button_S_DioExit.Text = "DioExit"
        '
        'GroupBox5
        '
        Me.GroupBox5.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_M_DioInit, Me.Button_M_Condition, Me.Button_M_Start, Me.Button_M_Stop, Me.Button_M_Data, Me.Button_M_Count, Me.Button_M_Status, Me.Button_M_DioExit})
        Me.GroupBox5.Location = New System.Drawing.Point(343, 10)
        Me.GroupBox5.Name = "GroupBox5"
        Me.GroupBox5.Size = New System.Drawing.Size(129, 346)
        Me.GroupBox5.TabIndex = 0
        Me.GroupBox5.TabStop = False
        Me.GroupBox5.Text = "Master:"
        '
        'Button_M_DioInit
        '
        Me.Button_M_DioInit.Location = New System.Drawing.Point(13, 18)
        Me.Button_M_DioInit.Name = "Button_M_DioInit"
        Me.Button_M_DioInit.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_DioInit.TabIndex = 3
        Me.Button_M_DioInit.Text = "DioInit"
        '
        'Button_M_Condition
        '
        Me.Button_M_Condition.Location = New System.Drawing.Point(13, 59)
        Me.Button_M_Condition.Name = "Button_M_Condition"
        Me.Button_M_Condition.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_Condition.TabIndex = 3
        Me.Button_M_Condition.Text = "Set Condition..."
        '
        'Button_M_Start
        '
        Me.Button_M_Start.Location = New System.Drawing.Point(13, 100)
        Me.Button_M_Start.Name = "Button_M_Start"
        Me.Button_M_Start.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_Start.TabIndex = 3
        Me.Button_M_Start.Text = "Start  Transfer "
        '
        'Button_M_Stop
        '
        Me.Button_M_Stop.Location = New System.Drawing.Point(13, 141)
        Me.Button_M_Stop.Name = "Button_M_Stop"
        Me.Button_M_Stop.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_Stop.TabIndex = 3
        Me.Button_M_Stop.Text = "Stop Transfer"
        '
        'Button_M_Data
        '
        Me.Button_M_Data.Location = New System.Drawing.Point(13, 182)
        Me.Button_M_Data.Name = "Button_M_Data"
        Me.Button_M_Data.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_Data.TabIndex = 3
        Me.Button_M_Data.Text = "Get Data..."
        '
        'Button_M_Count
        '
        Me.Button_M_Count.Location = New System.Drawing.Point(13, 223)
        Me.Button_M_Count.Name = "Button_M_Count"
        Me.Button_M_Count.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_Count.TabIndex = 3
        Me.Button_M_Count.Text = "Transfer Count"
        '
        'Button_M_Status
        '
        Me.Button_M_Status.Location = New System.Drawing.Point(13, 264)
        Me.Button_M_Status.Name = "Button_M_Status"
        Me.Button_M_Status.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_Status.TabIndex = 3
        Me.Button_M_Status.Text = "Transfer Status"
        '
        'Button_M_DioExit
        '
        Me.Button_M_DioExit.Location = New System.Drawing.Point(13, 305)
        Me.Button_M_DioExit.Name = "Button_M_DioExit"
        Me.Button_M_DioExit.Size = New System.Drawing.Size(104, 28)
        Me.Button_M_DioExit.TabIndex = 3
        Me.Button_M_DioExit.Text = "DioExit"
        '
        'Button_Exit
        '
        Me.Button_Exit.Location = New System.Drawing.Point(422, 362)
        Me.Button_Exit.Name = "Button_Exit"
        Me.Button_Exit.Size = New System.Drawing.Size(104, 28)
        Me.Button_Exit.TabIndex = 5
        Me.Button_Exit.Text = "Exit"
        '
        'Sync
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(624, 398)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_Exit, Me.GroupBox1, Me.GroupBox2, Me.GroupBox3, Me.GroupBox4, Me.GroupBox5})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Sync"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Sync Sampling"
        Me.GroupBox1.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.GroupBox5.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Shared M_Id As Integer            'Device Id master
    Public Shared S_Id As Integer            'Device Id slave
    Dim M_GCh As GCHandle                    'Handle master
    Dim S_GCh As GCHandle                    'Handle slave
    Dim pMBmBuf As IntPtr                    'Buffer point master
    Dim pSBmBuf As IntPtr                    'Buffer point slave
    Const DATA_SIZE As Short = 1000          'Data Size
    Dim M_BmBuffer(DATA_SIZE) As Integer     'Data buffer master
    Dim S_BmBuffer(DATA_SIZE) As Integer     'Data buffer slave
    Dim szError As New StringBuilder("", 256)
    Dim Ret As Integer                       'Return code
    Dim gM_ConditionDlg As New M_ConditionDlg()
    Dim gS_ConditionDlg As New S_ConditionDlg()

    '=========================================================
    ' Dialog initialization
    '=========================================================
    Private Sub SYNC_DIALOG_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_M_TransCount.Text = ""
        Edit_M_Status.Text = ""
        Edit_M_Error.Text = ""
        Edit_M_Return.Text = ""
        Edit_S_TransCount.Text = ""
        Edit_S_Status.Text = ""
        Edit_S_Error.Text = ""
        Edit_S_Return.Text = ""
        Edit_M_DevName.Text = "DIO000"
        Edit_S_DevName.Text = "DIO001"
        '--------------------------------
        ' Pinned type handle is allocated to M_BmBuffer
        '--------------------------------
        If M_GCh.IsAllocated = False Then
            M_GCh = GCHandle.Alloc(M_BmBuffer, GCHandleType.Pinned)
        End If
        '--------------------------------
        ' Pinned type handle is allocated to S_BmBuffer
        '--------------------------------
        If S_GCh.IsAllocated = False Then
            S_GCh = GCHandle.Alloc(S_BmBuffer, GCHandleType.Pinned)
        End If
    End Sub

    '=========================================================
    ' DioInit master()
    '=========================================================
    Private Sub BUTTON_M_DIO_INIT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_DioInit.Click
        '--------------------------------
        ' Execute initialization function and get ID
        '--------------------------------
        Ret = DioInit(Edit_M_DevName.Text, M_Id)
        Call DioGetErrorString(Ret, szError)
        Edit_M_Return.Text = "DioInit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' DioInit slave()
    '=========================================================
    Private Sub BUTTON_S_DIO_INIT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_DioInit.Click
        '--------------------------------
        ' Execute initialization function and get ID
        '--------------------------------
        Ret = DioInit(Edit_S_DevName.Text, S_Id)
        Call DioGetErrorString(Ret, szError)
        Edit_S_Return.Text = "DioInit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Show M_ConditionDlg window
    '=========================================================
    Private Sub BUTTON_M_CONDITION_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_Condition.Click
        gM_ConditionDlg.Show()
    End Sub

    '=========================================================
    ' Show S_ConditionDlg window
    '=========================================================
    Private Sub BUTTON_S_CONDITION_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_Condition.Click
        gS_ConditionDlg.Show()
    End Sub

    '=========================================================
    ' Start transfer master
    '=========================================================
    Private Sub BUTTON_M_START_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_Start.Click
        Dim Direction As Integer

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_M_TransCount.Text = ""
        Edit_M_Status.Text = ""
        Edit_M_Error.Text = ""
        '--------------------------------
        ' Set stop message
        '--------------------------------
        Direction = DIODM_DIR_IN
        Ret = DioDmSetStopEvent(M_Id, Direction, Me.Handle)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_M_Return.Text = "DioDmSetStopEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Set notification of message
        '--------------------------------
        Ret = DioDmSetCountEvent(M_Id, Direction, M_ConditionDlg.NumberOfData, Me.Handle)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_M_Return.Text = "DioDmSetCountEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Reset FIFO (input)
        '--------------------------------
        Ret = DioDmFifoReset(M_Id, DIODM_RESET_FIFO_IN)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_M_Return.Text = "DioDmFifoReset Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' M_BmBuffer is initialized by 0
        '--------------------------------
        System.Array.Clear(M_BmBuffer, 0, M_BmBuffer.Length)
        '--------------------------------
        ' The fixed pointer to an object is acquired
        '--------------------------------
        pMBmBuf = M_GCh.AddrOfPinnedObject()
        '--------------------------------
        ' Set data buffer
        '--------------------------------
        Ret = DioDmSetBuffer(M_Id, Direction, pMBmBuf, DATA_SIZE, M_ConditionDlg.IsRing)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_M_Return.Text = "DioDmSetBuffer Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Start transfer
        '--------------------------------
        Ret = DioDmTransferStart(M_Id, Direction)
        Call DioGetErrorString(Ret, szError)
        Edit_M_Return.Text = "DioDmTransferStart Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Start transfer slave
    '=========================================================
    Private Sub BUTTON_S_START_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_Start.Click
        Dim Direction As Integer

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_S_TransCount.Text = ""
        Edit_S_Status.Text = ""
        Edit_S_Error.Text = ""
        '--------------------------------
        ' Set stop message
        '--------------------------------
        Direction = DIODM_DIR_IN
        Ret = DioDmSetStopEvent(S_Id, Direction, Me.Handle)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_S_Return.Text = "DioDmSetStopEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Set notification of message
        '--------------------------------
        Ret = DioDmSetCountEvent(S_Id, Direction, S_ConditionDlg.NumberOfData, Me.Handle)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_S_Return.Text = "DioDmSetCountEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Reset FIFO (input)
        '--------------------------------
        Ret = DioDmFifoReset(S_Id, DIODM_RESET_FIFO_IN)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_S_Return.Text = "DioDmFifoReset Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' S_BmBuffer is initialized by 0
        '--------------------------------
        System.Array.Clear(S_BmBuffer, 0, S_BmBuffer.Length)
        '--------------------------------
        ' The fixed pointer to an object is acquired
        '--------------------------------
        pSBmBuf = S_GCh.AddrOfPinnedObject()
        '--------------------------------
        ' Set data buffer
        '--------------------------------
        Ret = DioDmSetBuffer(S_Id, Direction, pSBmBuf, DATA_SIZE, S_ConditionDlg.IsRing)
        If Ret <> 0 Then
            Call DioGetErrorString(Ret, szError)
            Edit_S_Return.Text = "DioDmSetBuffer Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Start transfer
        '--------------------------------
        Ret = DioDmTransferStart(S_Id, Direction)
        Call DioGetErrorString(Ret, szError)
        Edit_S_Return.Text = "DioDmTransferStart Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Stop transfer master
    '=========================================================
    Private Sub BUTTON_M_STOP_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_Stop.Click
        '--------------------------------
        ' Stop transfer
        '--------------------------------
        Ret = DioDmTransferStop(M_Id, DIODM_DIR_IN)
        Call DioGetErrorString(Ret, szError)
        Edit_M_Return.Text = "DioDmTransferStop Ret = " & Ret & ":" & szError.ToString()

    End Sub

    '=========================================================
    ' Stop transfer slave
    '=========================================================
    Private Sub BUTTON_S_STOP_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_Stop.Click
        '--------------------------------
        ' Stop transfer
        '--------------------------------
        Ret = DioDmTransferStop(S_Id, DIODM_DIR_IN)
        Call DioGetErrorString(Ret, szError)
        Edit_S_Return.Text = "DioDmTransferStop Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Get data master
    '=========================================================
    Private Sub BUTTON_M_DATA_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_Data.Click
        Dim szExeFilePath As String
        Dim szFileName As String
        Dim szLine As String
        Dim i As Integer

        szFileName = "MSamplingData.csv"
        '--------------------------------
        ' Open file
        '--------------------------------
        FileOpen(1, szFileName, OpenMode.Output)
        '--------------------------------
        ' Write data into file
        '--------------------------------
        For i = 0 To DATA_SIZE - 1
            szLine = String.Format("{0:d8}: ,{1:X8}", i, M_BmBuffer(i))
            PrintLine(1, szLine)
        Next i
        '--------------------------------
        ' Close file
        '--------------------------------
        FileClose(1)
        '--------------------------------
        ' Show file
        '--------------------------------
        szExeFilePath = "notepad.exe " & szFileName
        Shell(szExeFilePath, 1)
    End Sub

    '=========================================================
    ' Get data slave
    '=========================================================
    Private Sub BUTTON_S_DATA_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_Data.Click
        Dim szExeFilePath As String
        Dim szFileName As String
        Dim szLine As String
        Dim i As Integer

        szFileName = "SSamplingData.csv"
        '--------------------------------
        ' Open file
        '--------------------------------
        FileOpen(1, szFileName, OpenMode.Output)
        '--------------------------------
        ' Write data into file
        '--------------------------------
        For i = 0 To DATA_SIZE - 1
            szLine = String.Format("{0:d8}: ,{1:X8}", i, S_BmBuffer(i))
            PrintLine(1, szLine)
        Next i
        '--------------------------------
        ' Close file
        '--------------------------------
        FileClose(1)
        '--------------------------------
        ' Show file
        '--------------------------------
        szExeFilePath = "notepad.exe " & szFileName
        Shell(szExeFilePath, 1)
    End Sub

    '=========================================================
    ' Transfer count master
    '=========================================================
    Private Sub BUTTON_M_COUNT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_Count.Click
        Dim Count As Integer
        Dim Carry As Integer

        '--------------------------------
        ' Get transfer count
        '--------------------------------
        Ret = DioDmGetCount(M_Id, DIODM_DIR_IN, Count, Carry)
        Call DioGetErrorString(Ret, szError)
        Edit_M_Return.Text = "DioDmGetCount Ret = " & Ret & ":" & szError.ToString()
        Edit_M_TransCount.Text = Str(Count)
    End Sub

    '=========================================================
    ' Transfer count slave
    '=========================================================
    Private Sub BUTTON_S_COUNT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_Count.Click
        Dim Count As Integer
        Dim Carry As Integer

        '--------------------------------
        ' Get transfer count
        '--------------------------------
        Ret = DioDmGetCount(S_Id, DIODM_DIR_IN, Count, Carry)
        Call DioGetErrorString(Ret, szError)
        Edit_S_Return.Text = "DioDmGetCount Ret = " & Ret & ":" & szError.ToString()
        Edit_S_TransCount.Text = Str(Count)
    End Sub

    '=========================================================
    ' Transfer status master
    '=========================================================
    Private Sub BUTTON_M_STATUS_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_Status.Click
        Dim Status As Integer
        Dim Err As Integer
        Dim szMsg As String

        '--------------------------------
        ' Get transfer status
        '--------------------------------
        Ret = DioDmGetStatus(M_Id, DIODM_DIR_IN, Status, Err)
        Call DioGetErrorString(Ret, szError)
        Edit_M_Return.Text = "DioDmGetStatus Ret = " & Ret & ":" & szError.ToString()
        If Ret = DIO_ERR_SUCCESS Then
            '--------------------------------
            ' Status
            '--------------------------------
            szMsg = ""
            If (Status And DIODM_STATUS_BMSTOP) <> 0 Then szMsg = szMsg & "BmStop,"
            If (Status And DIODM_STATUS_PIOSTART) <> 0 Then szMsg = szMsg & "PioStart,"
            If (Status And DIODM_STATUS_PIOSTOP) <> 0 Then szMsg = szMsg & "PioStop,"
            If (Status And DIODM_STATUS_TRGIN) <> 0 Then szMsg = szMsg & "TrgIn,"
            If (Status And DIODM_STATUS_OVERRUN) <> 0 Then szMsg = szMsg & "Overrun"
            Edit_M_Status.Text = szMsg
            '--------------------------------
            ' Error
            '--------------------------------
            szMsg = ""
            If (Err And DIODM_STATUS_FIFOEMPTY) <> 0 Then szMsg = szMsg & "FifoEmpty,"
            If (Err And DIODM_STATUS_FIFOFULL) <> 0 Then szMsg = szMsg & "FifoFull,"
            If (Err And DIODM_STATUS_SGOVERIN) <> 0 Then szMsg = szMsg & "S/GOverIn,"
            If (Err And DIODM_STATUS_TRGERR) <> 0 Then szMsg = szMsg & "TrgErr,"
            If (Err And DIODM_STATUS_CLKERR) <> 0 Then szMsg = szMsg & "ClkErr,"
            If (Err And DIODM_STATUS_SLAVEHALT) <> 0 Then szMsg = szMsg & "SlaveHalt,"
            If (Err And DIODM_STATUS_MASTERHALT) <> 0 Then szMsg = szMsg & "MasterHalt"
            Edit_M_Error.Text = szMsg
        Else
            Edit_M_Status.Text = ""
            Edit_M_Error.Text = ""
        End If
    End Sub

    '=========================================================
    ' Transfer status slave
    '=========================================================
    Private Sub BUTTON_S_STATUS_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_Status.Click
        Dim Status As Integer
        Dim Err As Integer
        Dim szMsg As String

        '--------------------------------
        ' Get transfer status
        '--------------------------------
        Ret = DioDmGetStatus(S_Id, DIODM_DIR_IN, Status, Err)
        Call DioGetErrorString(Ret, szError)
        Edit_S_Return.Text = "DioDmGetStatus Ret = " & Ret & ":" & szError.ToString()
        '--------------------------------
        ' Status
        '--------------------------------
        szMsg = ""
        If (Status And DIODM_STATUS_BMSTOP) <> 0 Then szMsg = szMsg & "BmStop,"
        If (Status And DIODM_STATUS_PIOSTART) <> 0 Then szMsg = szMsg & "PioStart,"
        If (Status And DIODM_STATUS_PIOSTOP) <> 0 Then szMsg = szMsg & "PioStop,"
        If (Status And DIODM_STATUS_TRGIN) <> 0 Then szMsg = szMsg & "TrgIn,"
        If (Status And DIODM_STATUS_OVERRUN) <> 0 Then szMsg = szMsg & "Overrun"
        Edit_S_Status.Text = szMsg
        '--------------------------------
        ' Error
        '--------------------------------
        szMsg = ""
        If (Err And DIODM_STATUS_FIFOEMPTY) <> 0 Then szMsg = szMsg & "FifoEmpty,"
        If (Err And DIODM_STATUS_FIFOFULL) <> 0 Then szMsg = szMsg & "FifoFull,"
        If (Err And DIODM_STATUS_SGOVERIN) <> 0 Then szMsg = szMsg & "S/GOverIn,"
        If (Err And DIODM_STATUS_TRGERR) <> 0 Then szMsg = szMsg & "TrgErr,"
        If (Err And DIODM_STATUS_CLKERR) <> 0 Then szMsg = szMsg & "ClkErr,"
        If (Err And DIODM_STATUS_SLAVEHALT) <> 0 Then szMsg = szMsg & "SlaveHalt,"
        If (Err And DIODM_STATUS_MASTERHALT) <> 0 Then szMsg = szMsg & "MasterHalt"
        Edit_S_Error.Text = szMsg
    End Sub

    '=========================================================
    ' DioExit master
    '=========================================================
    Private Sub BUTTON_M_DIO_EXIT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_M_DioExit.Click
        '--------------------------------
        ' DioExit
        '--------------------------------
        Ret = DioExit(M_Id)
        Call DioGetErrorString(Ret, szError)
        Edit_M_Return.Text = "DioExit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' DioExit slave
    '=========================================================
    Private Sub BUTTON_S_DIO_EXIT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_S_DioExit.Click
        '--------------------------------
        ' DioExit
        '--------------------------------
        Ret = DioExit(S_Id)
        Call DioGetErrorString(Ret, szError)
        Edit_S_Return.Text = "DioExit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Dialog exit
    '=========================================================
    Private Sub BUTTON_EXIT_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button_Exit.Click

        Ret = DioExit(M_Id)
        Ret = DioExit(S_Id)
        End
    End Sub

    '=========================================================
    ' Dialog close
    '=========================================================
    Private Sub Sync_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
        Ret = DioExit(M_Id)
        Ret = DioExit(S_Id)

        '--------------------------------
        ' GCHandle is freed
        '--------------------------------
        If M_GCh.IsAllocated = True Then
            M_GCh.Free()
        End If
        If S_GCh.IsAllocated = True Then
            S_GCh.Free()
        End If
        End
    End Sub

    '=========================================================
    ' Message handler
    '=========================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        Dim Count As Integer
        Dim Carry As Integer

        '--------------------------------
        ' Transfer count message or Transfer stop message
        '--------------------------------
        If m.Msg = DIO_DMM_COUNT Or m.Msg = DIO_DMM_STOP Then
            If m.WParam.ToInt32 = M_Id Then
                Ret = DioDmGetCount(M_Id, DIODM_DIR_IN, Count, Carry)
                Edit_M_TransCount.Text = Str(Count)
            ElseIf m.WParam.ToInt32 = S_Id Then
                Ret = DioDmGetCount(S_Id, DIODM_DIR_IN, Count, Carry)
                Edit_S_TransCount.Text = Str(Count)
            End If
        End If
        '--------------------------------
        ' Transfer stop message
        '--------------------------------
        If m.Msg = DIO_DMM_STOP Then
            If m.WParam.ToInt32 = M_Id Then
                Edit_M_Status.Text = "Stopped!"
                BUTTON_M_COUNT_Click(Button_M_Count, New System.EventArgs())
            ElseIf m.WParam.ToInt32 = S_Id Then
                Edit_S_Status.Text = "Stopped!"
                BUTTON_S_COUNT_Click(Button_S_Count, New System.EventArgs())
            End If
        End If
        MyBase.WndProc(m)
    End Sub

End Class
