﻿Option Strict Off
Option Explicit On 
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text

Public 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()

        'Add any initialization after the InitializeComponent() call

    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 GroupBox5 As System.Windows.Forms.GroupBox
    Friend WithEvents btnSCntExit As System.Windows.Forms.Button
    Friend WithEvents btnSStatus As System.Windows.Forms.Button
    Friend WithEvents btnSCount As System.Windows.Forms.Button
    Friend WithEvents btnSData As System.Windows.Forms.Button
    Friend WithEvents btnSStop As System.Windows.Forms.Button
    Friend WithEvents btnSStart As System.Windows.Forms.Button
    Friend WithEvents btnSSampMode As System.Windows.Forms.Button
    Friend WithEvents btnSCntMode As System.Windows.Forms.Button
    Friend WithEvents btnSInit As System.Windows.Forms.Button
    Friend WithEvents btnExit As System.Windows.Forms.Button
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    Friend WithEvents txtSRet As System.Windows.Forms.TextBox
    Friend WithEvents txtSError As System.Windows.Forms.TextBox
    Friend WithEvents txtSStatus As System.Windows.Forms.TextBox
    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 txtSCount As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox4 As System.Windows.Forms.GroupBox
    Friend WithEvents btnMCntExit As System.Windows.Forms.Button
    Friend WithEvents btnMStatus As System.Windows.Forms.Button
    Friend WithEvents btnMCount As System.Windows.Forms.Button
    Friend WithEvents btnMData As System.Windows.Forms.Button
    Friend WithEvents btnMStop As System.Windows.Forms.Button
    Friend WithEvents btnMStart As System.Windows.Forms.Button
    Friend WithEvents btnMSmpMode As System.Windows.Forms.Button
    Friend WithEvents btnMCntMode As System.Windows.Forms.Button
    Friend WithEvents btnMInit As System.Windows.Forms.Button
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents txtMRet As System.Windows.Forms.TextBox
    Friend WithEvents txtMError As System.Windows.Forms.TextBox
    Friend WithEvents txtMStatus As System.Windows.Forms.TextBox
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents txtMCount As System.Windows.Forms.TextBox
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtSDeviceName As System.Windows.Forms.TextBox
    Friend WithEvents txtMDeviceName As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox5 = New System.Windows.Forms.GroupBox()
        Me.btnSCntExit = New System.Windows.Forms.Button()
        Me.btnSStatus = New System.Windows.Forms.Button()
        Me.btnSCount = New System.Windows.Forms.Button()
        Me.btnSData = New System.Windows.Forms.Button()
        Me.btnSStop = New System.Windows.Forms.Button()
        Me.btnSStart = New System.Windows.Forms.Button()
        Me.btnSSampMode = New System.Windows.Forms.Button()
        Me.btnSCntMode = New System.Windows.Forms.Button()
        Me.btnSInit = New System.Windows.Forms.Button()
        Me.btnExit = New System.Windows.Forms.Button()
        Me.GroupBox3 = New System.Windows.Forms.GroupBox()
        Me.txtSRet = New System.Windows.Forms.TextBox()
        Me.txtSError = New System.Windows.Forms.TextBox()
        Me.txtSStatus = 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.txtSCount = New System.Windows.Forms.TextBox()
        Me.GroupBox4 = New System.Windows.Forms.GroupBox()
        Me.btnMCntExit = New System.Windows.Forms.Button()
        Me.btnMStatus = New System.Windows.Forms.Button()
        Me.btnMCount = New System.Windows.Forms.Button()
        Me.btnMData = New System.Windows.Forms.Button()
        Me.btnMStop = New System.Windows.Forms.Button()
        Me.btnMStart = New System.Windows.Forms.Button()
        Me.btnMSmpMode = New System.Windows.Forms.Button()
        Me.btnMCntMode = New System.Windows.Forms.Button()
        Me.btnMInit = New System.Windows.Forms.Button()
        Me.GroupBox2 = New System.Windows.Forms.GroupBox()
        Me.txtMRet = New System.Windows.Forms.TextBox()
        Me.txtMError = New System.Windows.Forms.TextBox()
        Me.txtMStatus = New System.Windows.Forms.TextBox()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.txtMCount = New System.Windows.Forms.TextBox()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtSDeviceName = New System.Windows.Forms.TextBox()
        Me.txtMDeviceName = New System.Windows.Forms.TextBox()
        Me.GroupBox5.SuspendLayout()
        Me.GroupBox3.SuspendLayout()
        Me.GroupBox4.SuspendLayout()
        Me.GroupBox2.SuspendLayout()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox5
        '
        Me.GroupBox5.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSCntExit, Me.btnSStatus, Me.btnSCount, Me.btnSData, Me.btnSStop, Me.btnSStart, Me.btnSSampMode, Me.btnSCntMode, Me.btnSInit})
        Me.GroupBox5.Location = New System.Drawing.Point(544, 14)
        Me.GroupBox5.Name = "GroupBox5"
        Me.GroupBox5.Size = New System.Drawing.Size(144, 364)
        Me.GroupBox5.TabIndex = 15
        Me.GroupBox5.TabStop = False
        Me.GroupBox5.Text = "Slave"
        '
        'btnSCntExit
        '
        Me.btnSCntExit.Location = New System.Drawing.Point(16, 319)
        Me.btnSCntExit.Name = "btnSCntExit"
        Me.btnSCntExit.Size = New System.Drawing.Size(112, 28)
        Me.btnSCntExit.TabIndex = 8
        Me.btnSCntExit.Text = "CntExit"
        '
        'btnSStatus
        '
        Me.btnSStatus.Location = New System.Drawing.Point(16, 282)
        Me.btnSStatus.Name = "btnSStatus"
        Me.btnSStatus.Size = New System.Drawing.Size(112, 27)
        Me.btnSStatus.TabIndex = 7
        Me.btnSStatus.Text = "Status"
        '
        'btnSCount
        '
        Me.btnSCount.Location = New System.Drawing.Point(16, 244)
        Me.btnSCount.Name = "btnSCount"
        Me.btnSCount.Size = New System.Drawing.Size(112, 28)
        Me.btnSCount.TabIndex = 6
        Me.btnSCount.Text = "Count"
        '
        'btnSData
        '
        Me.btnSData.Location = New System.Drawing.Point(16, 207)
        Me.btnSData.Name = "btnSData"
        Me.btnSData.Size = New System.Drawing.Size(112, 28)
        Me.btnSData.TabIndex = 5
        Me.btnSData.Text = "Data..."
        '
        'btnSStop
        '
        Me.btnSStop.Location = New System.Drawing.Point(16, 170)
        Me.btnSStop.Name = "btnSStop"
        Me.btnSStop.Size = New System.Drawing.Size(112, 28)
        Me.btnSStop.TabIndex = 4
        Me.btnSStop.Text = "Stop"
        '
        'btnSStart
        '
        Me.btnSStart.Location = New System.Drawing.Point(16, 133)
        Me.btnSStart.Name = "btnSStart"
        Me.btnSStart.Size = New System.Drawing.Size(112, 28)
        Me.btnSStart.TabIndex = 3
        Me.btnSStart.Text = "Start"
        '
        'btnSSampMode
        '
        Me.btnSSampMode.Location = New System.Drawing.Point(16, 95)
        Me.btnSSampMode.Name = "btnSSampMode"
        Me.btnSSampMode.Size = New System.Drawing.Size(112, 28)
        Me.btnSSampMode.TabIndex = 2
        Me.btnSSampMode.Text = "Sampling Mode..."
        '
        'btnSCntMode
        '
        Me.btnSCntMode.Location = New System.Drawing.Point(16, 58)
        Me.btnSCntMode.Name = "btnSCntMode"
        Me.btnSCntMode.Size = New System.Drawing.Size(112, 28)
        Me.btnSCntMode.TabIndex = 1
        Me.btnSCntMode.Text = "Mode..."
        '
        'btnSInit
        '
        Me.btnSInit.Location = New System.Drawing.Point(16, 21)
        Me.btnSInit.Name = "btnSInit"
        Me.btnSInit.Size = New System.Drawing.Size(112, 28)
        Me.btnSInit.TabIndex = 0
        Me.btnSInit.Text = "CntInit"
        '
        'btnExit
        '
        Me.btnExit.Location = New System.Drawing.Point(488, 384)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.Size = New System.Drawing.Size(112, 27)
        Me.btnExit.TabIndex = 16
        Me.btnExit.Text = "Exit"
        '
        'GroupBox3
        '
        Me.GroupBox3.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtSRet, Me.txtSError, Me.txtSStatus, Me.Label7, Me.Label8, Me.Label9, Me.Label10, Me.txtSCount})
        Me.GroupBox3.Location = New System.Drawing.Point(8, 228)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(376, 149)
        Me.GroupBox3.TabIndex = 13
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "Slave"
        '
        'txtSRet
        '
        Me.txtSRet.Location = New System.Drawing.Point(104, 105)
        Me.txtSRet.Name = "txtSRet"
        Me.txtSRet.Size = New System.Drawing.Size(256, 20)
        Me.txtSRet.TabIndex = 15
        Me.txtSRet.Text = ""
        '
        'txtSError
        '
        Me.txtSError.Location = New System.Drawing.Point(104, 77)
        Me.txtSError.Name = "txtSError"
        Me.txtSError.Size = New System.Drawing.Size(256, 20)
        Me.txtSError.TabIndex = 14
        Me.txtSError.Text = ""
        '
        'txtSStatus
        '
        Me.txtSStatus.Location = New System.Drawing.Point(104, 49)
        Me.txtSStatus.Name = "txtSStatus"
        Me.txtSStatus.Size = New System.Drawing.Size(256, 20)
        Me.txtSStatus.TabIndex = 13
        Me.txtSStatus.Text = ""
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(16, 105)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(80, 18)
        Me.Label7.TabIndex = 12
        Me.Label7.Text = "Return Value:"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(16, 77)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(72, 18)
        Me.Label8.TabIndex = 11
        Me.Label8.Text = "Error:"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(16, 49)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(72, 18)
        Me.Label9.TabIndex = 10
        Me.Label9.Text = "Status:"
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(16, 21)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(72, 19)
        Me.Label10.TabIndex = 9
        Me.Label10.Text = "Count:"
        '
        'txtSCount
        '
        Me.txtSCount.Location = New System.Drawing.Point(104, 21)
        Me.txtSCount.Name = "txtSCount"
        Me.txtSCount.Size = New System.Drawing.Size(256, 20)
        Me.txtSCount.TabIndex = 8
        Me.txtSCount.Text = ""
        '
        'GroupBox4
        '
        Me.GroupBox4.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnMCntExit, Me.btnMStatus, Me.btnMCount, Me.btnMData, Me.btnMStop, Me.btnMStart, Me.btnMSmpMode, Me.btnMCntMode, Me.btnMInit})
        Me.GroupBox4.Location = New System.Drawing.Point(392, 14)
        Me.GroupBox4.Name = "GroupBox4"
        Me.GroupBox4.Size = New System.Drawing.Size(144, 364)
        Me.GroupBox4.TabIndex = 14
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "Master"
        '
        'btnMCntExit
        '
        Me.btnMCntExit.Location = New System.Drawing.Point(16, 319)
        Me.btnMCntExit.Name = "btnMCntExit"
        Me.btnMCntExit.Size = New System.Drawing.Size(112, 28)
        Me.btnMCntExit.TabIndex = 8
        Me.btnMCntExit.Text = "CntExit"
        '
        'btnMStatus
        '
        Me.btnMStatus.Location = New System.Drawing.Point(16, 282)
        Me.btnMStatus.Name = "btnMStatus"
        Me.btnMStatus.Size = New System.Drawing.Size(112, 27)
        Me.btnMStatus.TabIndex = 7
        Me.btnMStatus.Text = "Status"
        '
        'btnMCount
        '
        Me.btnMCount.Location = New System.Drawing.Point(16, 244)
        Me.btnMCount.Name = "btnMCount"
        Me.btnMCount.Size = New System.Drawing.Size(112, 28)
        Me.btnMCount.TabIndex = 6
        Me.btnMCount.Text = "Count"
        '
        'btnMData
        '
        Me.btnMData.Location = New System.Drawing.Point(16, 207)
        Me.btnMData.Name = "btnMData"
        Me.btnMData.Size = New System.Drawing.Size(112, 28)
        Me.btnMData.TabIndex = 5
        Me.btnMData.Text = "Data..."
        '
        'btnMStop
        '
        Me.btnMStop.Location = New System.Drawing.Point(16, 170)
        Me.btnMStop.Name = "btnMStop"
        Me.btnMStop.Size = New System.Drawing.Size(112, 28)
        Me.btnMStop.TabIndex = 4
        Me.btnMStop.Text = "Stop"
        '
        'btnMStart
        '
        Me.btnMStart.Location = New System.Drawing.Point(16, 133)
        Me.btnMStart.Name = "btnMStart"
        Me.btnMStart.Size = New System.Drawing.Size(112, 28)
        Me.btnMStart.TabIndex = 3
        Me.btnMStart.Text = "Start"
        '
        'btnMSmpMode
        '
        Me.btnMSmpMode.Location = New System.Drawing.Point(16, 95)
        Me.btnMSmpMode.Name = "btnMSmpMode"
        Me.btnMSmpMode.Size = New System.Drawing.Size(112, 28)
        Me.btnMSmpMode.TabIndex = 2
        Me.btnMSmpMode.Text = "Sampling Mode..."
        '
        'btnMCntMode
        '
        Me.btnMCntMode.Location = New System.Drawing.Point(16, 58)
        Me.btnMCntMode.Name = "btnMCntMode"
        Me.btnMCntMode.Size = New System.Drawing.Size(112, 28)
        Me.btnMCntMode.TabIndex = 1
        Me.btnMCntMode.Text = "Mode..."
        '
        'btnMInit
        '
        Me.btnMInit.Location = New System.Drawing.Point(16, 21)
        Me.btnMInit.Name = "btnMInit"
        Me.btnMInit.Size = New System.Drawing.Size(112, 28)
        Me.btnMInit.TabIndex = 0
        Me.btnMInit.Text = "CntInit"
        '
        'GroupBox2
        '
        Me.GroupBox2.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtMRet, Me.txtMError, Me.txtMStatus, Me.Label6, Me.Label5, Me.Label4, Me.Label3, Me.txtMCount})
        Me.GroupBox2.Location = New System.Drawing.Point(8, 79)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(376, 139)
        Me.GroupBox2.TabIndex = 12
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "Master"
        '
        'txtMRet
        '
        Me.txtMRet.Location = New System.Drawing.Point(104, 105)
        Me.txtMRet.Name = "txtMRet"
        Me.txtMRet.Size = New System.Drawing.Size(256, 20)
        Me.txtMRet.TabIndex = 7
        Me.txtMRet.Text = ""
        '
        'txtMError
        '
        Me.txtMError.Location = New System.Drawing.Point(104, 77)
        Me.txtMError.Name = "txtMError"
        Me.txtMError.Size = New System.Drawing.Size(256, 20)
        Me.txtMError.TabIndex = 6
        Me.txtMError.Text = ""
        '
        'txtMStatus
        '
        Me.txtMStatus.Location = New System.Drawing.Point(104, 49)
        Me.txtMStatus.Name = "txtMStatus"
        Me.txtMStatus.Size = New System.Drawing.Size(256, 20)
        Me.txtMStatus.TabIndex = 5
        Me.txtMStatus.Text = ""
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 105)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(80, 18)
        Me.Label6.TabIndex = 4
        Me.Label6.Text = "Return Value:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 77)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(72, 18)
        Me.Label5.TabIndex = 3
        Me.Label5.Text = "Error:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 49)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(72, 18)
        Me.Label4.TabIndex = 2
        Me.Label4.Text = "Status:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 21)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 19)
        Me.Label3.TabIndex = 1
        Me.Label3.Text = "Count:"
        '
        'txtMCount
        '
        Me.txtMCount.Location = New System.Drawing.Point(104, 21)
        Me.txtMCount.Name = "txtMCount"
        Me.txtMCount.Size = New System.Drawing.Size(256, 20)
        Me.txtMCount.TabIndex = 0
        Me.txtMCount.Text = ""
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label2, Me.Label1, Me.txtSDeviceName, Me.txtMDeviceName})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 14)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(376, 55)
        Me.GroupBox1.TabIndex = 11
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Device Name"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(216, 23)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(56, 19)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Slave:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 23)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 19)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Master:"
        '
        'txtSDeviceName
        '
        Me.txtSDeviceName.Location = New System.Drawing.Point(280, 23)
        Me.txtSDeviceName.Name = "txtSDeviceName"
        Me.txtSDeviceName.Size = New System.Drawing.Size(72, 20)
        Me.txtSDeviceName.TabIndex = 1
        Me.txtSDeviceName.Text = "TextBox2"
        '
        'txtMDeviceName
        '
        Me.txtMDeviceName.Location = New System.Drawing.Point(80, 23)
        Me.txtMDeviceName.Name = "txtMDeviceName"
        Me.txtMDeviceName.Size = New System.Drawing.Size(88, 20)
        Me.txtMDeviceName.TabIndex = 0
        Me.txtMDeviceName.Text = "TextBox1"
        '
        'Sync
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(696, 425)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox5, Me.btnExit, Me.GroupBox3, Me.GroupBox4, Me.GroupBox2, Me.GroupBox1})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Sync"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Synchronous Sampling"
        Me.GroupBox5.ResumeLayout(False)
        Me.GroupBox3.ResumeLayout(False)
        Me.GroupBox4.ResumeLayout(False)
        Me.GroupBox2.ResumeLayout(False)
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Shared M_Id As Short
    Public Shared S_Id As Short
    Public Shared M_IsRing As Short
    Public Shared S_IsRing As Short

    Dim M_SampData(DATA_NUM - 1, CHANNEL_NUM - 1) As UInt32 ' Buffer for sampling
    Dim S_SampData(DATA_NUM - 1, CHANNEL_NUM - 1) As UInt32 ' Buffer for sampling

    Dim gCntMode As New frmCntMode()
    Dim gMSmpMode As New frmMSmpMode()
    Dim gSSmpMode As New frmSSmpMode()

    Dim M_gCh, S_gCh As GCHandle
	Dim pM_SampData, pS_SampData As IntPtr

    '===================================================
    '	Dialog initialization
    '===================================================
    Private Sub Sync_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '-----------------------------------
        ' Specify Pinned handle to M_SampData
        '-----------------------------------
        If M_gCh.IsAllocated = False Then
            M_gCh = GCHandle.Alloc(M_SampData, GCHandleType.Pinned)
        End If

        '-----------------------------------
        ' Specify Pinned handle to S_SampData
        '-----------------------------------
        If S_gCh.IsAllocated = False Then
            S_gCh = GCHandle.Alloc(S_SampData, GCHandleType.Pinned)
        End If

        '-----------------------------------
        '	Initialize dialog display
        '-----------------------------------
        txtMDeviceName.Text = "CNT000"
        txtSDeviceName.Text = "CNT001"

    End Sub

    '===================================================
    '	Master CntInit()
    '===================================================
    Private Sub btnMInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMInit.Click
        Dim str As String
        Dim error_string As New StringBuilder("", 256)
        Dim buf As New String("", 100)
        Dim ret As Integer

        '-----------------------------------
        '	Get device name
        '-----------------------------------
        buf = txtMDeviceName.Text

        '-----------------------------------
        '	Initialization
        '-----------------------------------
        ret = CntInit(buf, M_Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntInit ret = {0:d}: {1}", ret, error_string)
        txtMRet.Text = str
    End Sub

    '===================================================
    '	Slave CntInit()
    '===================================================
    Private Sub btnSInit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSInit.Click
        Dim str As String
        Dim error_string As New StringBuilder("", 256)
        Dim buf As New String("", 100)
        Dim ret As Integer

        '-----------------------------------
        '	Get device name
        '-----------------------------------
        buf = txtSDeviceName.Text

        '-----------------------------------
        '	Initialization
        '-----------------------------------
        ret = CntInit(buf, S_Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntInit ret = {0:d}: {1}", ret, error_string)
        txtSRet.Text = str

    End Sub

    '===================================================
    '	Master Condition Setting
    '===================================================
    Private Sub btnMCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMCntMode.Click
        '-----------------------------------
        '	Display Condition Setting dialog
        '-----------------------------------
        frmCntMode.Id = M_Id
        gCntMode.Show()
    End Sub

    '===================================================
    '	Slave Condition Setting
    '===================================================
    Private Sub btnSCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSCntMode.Click
        '-----------------------------------
        '	Display Condition Setting dialog
        '-----------------------------------
        frmCntMode.Id = S_Id
        gCntMode.Show()
    End Sub

    '===================================================
    '	Master Sampling Mode Setting
    '===================================================
    Private Sub btnMSmpMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSmpMode.Click
        gMSmpMode.Show()
    End Sub

    '===================================================
    '	Slave Sampling Mode Setting
    '===================================================
    Private Sub btnSSampMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSSampMode.Click
        gSSmpMode.Show()
    End Sub

    '===================================================
    '	Master Start
    '===================================================
    Private Sub btnMStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMStart.Click
        Dim ret As Integer
        Dim csMsg As String
        Dim i As Integer
        Dim ChNo(CHANNEL_NUM) As Short
        Dim error_string As New StringBuilder("", 256)
        Dim str As String

        '-----------------------------------
        ' Set notification
        '-----------------------------------
        ret = CntNotifySamplingStop(M_Id, Me.Handle)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string)
            txtMRet.Text = str
            Exit Sub
        End If

        ret = CntNotifySamplingCount(M_Id, frmMSmpMode.M_NotifyCount, Me.Handle)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string)
            txtMRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Reset FIFO
        '-----------------------------------
        ret = CntSamplingFifoReset(M_Id)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string)
            txtMRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Set buffer
        '-----------------------------------
        Array.Clear(M_SampData, 0, M_SampData.Length)

        pM_SampData = M_gCh.AddrOfPinnedObject
        ret = CntSamplingSetBuffer(M_Id, pM_SampData, CHANNEL_NUM, DATA_NUM, M_IsRing)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string)
            txtMRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Start counter
        '-----------------------------------
        For i = 0 To CHANNEL_NUM - 1
            ChNo(i) = i
        Next i
        ret = CntStartCount(M_Id, ChNo, CHANNEL_NUM)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntStartCount ret = {0:d}: {1}", ret, error_string)
            txtMRet.Text = str
            Exit Sub
        End If

        '-----------------------------------
        ' Start sampling
        '-----------------------------------
        ret = CntSamplingStart(M_Id)
        csMsg = String.Format("CntStartSampling : RetCode = {0:X} H", ret)
        txtMRet.Text = csMsg
        txtMCount.Text = ""
        txtMStatus.Text = ""
        txtMError.Text = ""
    End Sub

    '===================================================
    '	Slave Start
    '===================================================
    Private Sub btnSStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSStart.Click
        Dim ret As Integer
        Dim csMsg As String
        Dim i As Integer
        Dim ChNo(CHANNEL_NUM) As Short
        Dim error_string As New StringBuilder("", 256)
        Dim str As String

        '-----------------------------------
        ' Set notification
        '-----------------------------------
        ret = CntNotifySamplingStop(S_Id, Me.Handle)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string)
            txtSRet.Text = str
            Exit Sub
        End If
        ret = CntNotifySamplingCount(S_Id, frmSSmpMode.S_NotifyCount, Me.Handle)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string)
            txtSRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Reset FIFO
        '-----------------------------------
        ret = CntSamplingFifoReset(S_Id)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string)
            txtSRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Set buffer
        '-----------------------------------
        Array.Clear(S_SampData, 0, S_SampData.Length)

        pS_SampData = S_gCh.AddrOfPinnedObject
        ret = CntSamplingSetBuffer(S_Id, pS_SampData, CHANNEL_NUM, DATA_NUM, S_IsRing)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string)
            txtSRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Start count
        '-----------------------------------
        For i = 0 To CHANNEL_NUM - 1
            ChNo(i) = i
        Next i
        ret = CntStartCount(S_Id, ChNo, CHANNEL_NUM)
        If ret <> 0 Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntStartCount ret = {0:d}: {1}", ret, error_string)
            txtSRet.Text = str
            Exit Sub
        End If
        '-----------------------------------
        ' Start sampling
        '-----------------------------------
        ret = CntSamplingStart(S_Id)
        csMsg = String.Format("CntStartSampling : RetCode = {0:X} H", ret)
        txtSRet.Text = csMsg
        txtSCount.Text = ""
        txtSStatus.Text = ""
        txtSError.Text = ""
    End Sub

    '===================================================
    '	Master Stop
    '===================================================
    Private Sub btnMStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMStop.Click
        Dim ret As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim str As String

        ret = CntSamplingStop(M_Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string)
        txtMRet.Text = str
    End Sub

    '===================================================
    '	Slave Stop
    '===================================================
    Private Sub btnSStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSStop.Click
        Dim ret As Integer
        Dim csMsg As String

        ret = CntSamplingStop(S_Id)
        csMsg = String.Format("CntStopSampling : RetCode = {0:X} H", ret)
        txtSRet.Text = csMsg
    End Sub

    '===================================================
    '	Master Data Getting
    '===================================================
    Private Sub btnMData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMData.Click
        Dim i, j As Integer
        Dim szLine As New String("", 500)
        Dim fp As StreamWriter
        Dim file_name As String = "M_SamplingData.csv"

        '-----------------------------------
        ' Open file
        '-----------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            txtMRet.Text = "fopen : file open error."
            Exit Sub
        End Try
        '-----------------------------------
        ' Writer data to file
        '-----------------------------------
        For i = 0 To DATA_NUM - 1
            szLine = String.Format("{0:d8}: , ", i)
            For j = 0 To CHANNEL_NUM - 2
                szLine = String.Format("{0}{1:X8}, ", szLine, M_SampData(i, j))
            Next j
            szLine = String.Format("{0}{1:X8}", szLine, M_SampData(i, j))
            fp.WriteLine(szLine)
        Next i
        '-----------------------------------
        ' Close file
        '-----------------------------------
        fp.Close()
        '-----------------------------------
        ' notepad
        '-----------------------------------
        Process.Start("notepad.exe", file_name)
    End Sub

    '===================================================
    '	Slave Data Getting
    '===================================================
    Private Sub btnSData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSData.Click
        Dim i, j As Integer
        Dim szLine As New String("", 500)
        Dim fp As StreamWriter
        Dim file_name As String = "S_SamplingData.csv"

        '-----------------------------------
        ' Open file
        '-----------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            txtSRet.Text = "fopen : file open error."
            Exit Sub
        End Try
        '-----------------------------------
        ' Write data to file
        '-----------------------------------
        For i = 0 To DATA_NUM - 1
            szLine = String.Format("{0:d8}: , ", i)
            For j = 0 To CHANNEL_NUM - 2
                szLine = String.Format("{0}{1:X8}, ", szLine, S_SampData(i, j))
            Next j
            szLine = String.Format("{0}{1:X8}", szLine, S_SampData(i, j))
            fp.WriteLine(szLine)
        Next i

        '-----------------------------------
        ' Close file
        '-----------------------------------
        fp.Close()
        '-----------------------------------
        ' notepad
        '-----------------------------------
        Process.Start("notepad.exe", file_name)
    End Sub

    '===================================================
    '	Master Count Value Getting
    '===================================================
    Private Sub btnMCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMCount.Click
        Dim ret As Integer
        Dim Count As Integer
        Dim ScanCarry As Integer
        Dim csMsg As String

        '-----------------------------------
        ' Get count value
        '-----------------------------------
        ret = CntGetSamplingCount(M_Id, Count, ScanCarry)
        csMsg = String.Format("CntGetSamplingCount : Ret = {0:X} H", ret)
        txtMRet.Text = csMsg
        ' In order to display the count value to unsigned, convert it once through hexadecimal characters
        txtMCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(Count), 16))
    End Sub

    '===================================================
    '	Slave Count Value Getting
    '===================================================
    Private Sub btnSCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSCount.Click
        Dim ret As Integer
        Dim Count As Integer
        Dim ScanCarry As Integer
        Dim csMsg As String

        '-----------------------------------
        ' Get count value
        '-----------------------------------
        ret = CntGetSamplingCount(S_Id, Count, ScanCarry)
        csMsg = String.Format("CntGetSamplingCount : Ret = {0:X} H", ret)
        txtSRet.Text = csMsg
        ' In order to display the count value to unsigned, convert it once through hexadecimal characters
        txtSCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(Count), 16))
    End Sub

    '===================================================
    '	Master Status Information Getting
    '===================================================
    Private Sub btnMStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMStatus.Click
        Dim ret As Integer
        Dim Status, Err As Integer
        Dim csMsg As String

        '-----------------------------------
        ' Get status information
        '-----------------------------------
        ret = CntGetSamplingStatus(M_Id, Status, Err)
        csMsg = String.Format("CntGetSamplingStatus : Ret = {0:X} H", ret)
        txtMRet.Text = csMsg
        ' Status
        csMsg = ""
        If Status And CNTS_STATUS_BMSTOP Then csMsg = csMsg & "BmStop,"
        If Status And CNTS_STATUS_CNTSTART Then csMsg = csMsg & "CntStart,"
        If Status And CNTS_STATUS_CNTSTOP Then csMsg = csMsg & "CntStop,"
        If Status And CNTS_STATUS_TRGIN Then csMsg = csMsg & "TrgIn,"
        If Status And CNTS_STATUS_OVERRUN Then csMsg = csMsg & "Overrun"
        txtMStatus.Text = csMsg
        ' Error
        csMsg = ""
        If Err And CNTS_STATUS_FIFOFULL Then csMsg = csMsg & "FifoFull,"
        If Err And CNTS_STATUS_SGOVERIN Then csMsg = csMsg & "S/GOverIn,"
        If Err And CNTS_STATUS_TRGERR Then csMsg = csMsg & "TrgErr,"
        If Err And CNTS_STATUS_CLKERR Then csMsg = csMsg & "ClkErr,"
        If Err And CNTS_STATUS_SLAVEHALT Then csMsg = csMsg & "SlaveHalt,"
        If Err And CNTS_STATUS_MASTERHALT Then csMsg = csMsg & "MasterHalt"
        txtMError.Text = csMsg
    End Sub

    '===================================================
    '	Slave Status Information Getting
    '===================================================
    Private Sub btnSStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSStatus.Click
        Dim ret As Integer
        Dim Status, Err As Integer
        Dim csMsg As String

        '-----------------------------------
        ' Get status information
        '-----------------------------------
        ret = CntGetSamplingStatus(S_Id, Status, Err)
        csMsg = String.Format("CntGetSamplingStatus : Ret = {0:X} H", ret)
        txtSRet.Text = csMsg
        ' Status
        csMsg = ""
        If Status And CNTS_STATUS_BMSTOP Then csMsg = csMsg & "BmStop,"
        If Status And CNTS_STATUS_CNTSTART Then csMsg = csMsg & "CntStart,"
        If Status And CNTS_STATUS_CNTSTOP Then csMsg = csMsg & "CntStop,"
        If Status And CNTS_STATUS_TRGIN Then csMsg = csMsg & "TrgIn,"
        If Status And CNTS_STATUS_OVERRUN Then csMsg = csMsg & "Overrun"
        txtSStatus.Text = csMsg
        ' Error
        csMsg = ""
        If Err And CNTS_STATUS_FIFOFULL Then csMsg = csMsg & "FifoFull,"
        If Err And CNTS_STATUS_SGOVERIN Then csMsg = csMsg & "S/GOverIn,"
        If Err And CNTS_STATUS_TRGERR Then csMsg = csMsg & "TrgErr,"
        If Err And CNTS_STATUS_CLKERR Then csMsg = csMsg & "ClkErr,"
        If Err And CNTS_STATUS_SLAVEHALT Then csMsg = csMsg & "SlaveHalt,"
        If Err And CNTS_STATUS_MASTERHALT Then csMsg = csMsg & "MasterHalt"
        txtSError.Text = csMsg
    End Sub

    '===================================================
    '	Master Completion processing
    '===================================================
    Private Sub btnMCntExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMCntExit.Click
        Dim szMsg As New String("", 100)
        Dim ret As Integer
        '------------------------------
        ' Completion processing
        '------------------------------
        ret = CntExit(M_Id)
        szMsg = String.Format("CntClose : Ret = {0:X} H", ret)
        txtMRet.Text = szMsg
    End Sub

    '===================================================
    '	Slave Completion processing
    '===================================================
    Private Sub btnSCntExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSCntExit.Click
        Dim szMsg As New String("", 100)
        Dim ret As Integer

        '------------------------------
        ' Completion processing
        '------------------------------
        ret = CntExit(S_Id)
        szMsg = String.Format("CntClose : Ret = {0:X} H", ret)
        txtSRet.Text = szMsg
    End Sub

    '===================================================
    ' Exit sample program
    '===================================================
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dim ret As Integer

        '-----------------------------------
        ' Free GCHandle
        '-----------------------------------
        If M_gCh.IsAllocated = True Then
            M_gCh.Free()
        End If
        If S_gCh.IsAllocated = True Then
            S_gCh.Free()
        End If

        ret = CntExit(M_Id)
        ret = CntExit(S_Id)
        Me.Close()
    End Sub

    '===================================================
    ' Counter event notification processing
    '===================================================
    Protected Overrides Sub WndProc(ByRef m As Message)

        Select Case m.Msg
            Case CNTM_SAMPLING_COUNT
                If m.WParam.ToInt32 = M_Id Then
                    btnMCount_Click(btnMCount, New System.EventArgs())
                Else
                    btnSCount_Click(btnSCount, New System.EventArgs())
                End If
            Case CNTM_SAMPLING_STOP
                If m.WParam.ToInt32 = M_Id Then
                    btnMCount_Click(btnMCount, New System.EventArgs())
                    txtMStatus.Text = "Stopped!"
                Else
                    btnSCount_Click(btnSCount, New System.EventArgs())
                    txtSStatus.Text = "Stopped!"
                End If
        End Select
        MyBase.WndProc(m)

    End Sub
End Class
