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

Public Class Sync
    Inherits System.Windows.Forms.Form

    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 ' サンプリング用バッファ
    Dim S_SampData(DATA_NUM - 1, CHANNEL_NUM - 1) As UInt32 ' サンプリング用バッファ

    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 As IntPtr
    Dim pS_SampData As IntPtr


#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は dispose をオーバーライドしてコンポーネント一覧を消去します。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    ' Windows フォーム デザイナを使って変更してください。  
    ' コード エディタは使用しないでください。
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents GroupBox2 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox3 As System.Windows.Forms.GroupBox
    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 GroupBox4 As System.Windows.Forms.GroupBox
    Friend WithEvents GroupBox5 As System.Windows.Forms.GroupBox
    Friend WithEvents txtMDeviceName As System.Windows.Forms.TextBox
    Friend WithEvents txtSDeviceName As System.Windows.Forms.TextBox
    Friend WithEvents txtMStatus As System.Windows.Forms.TextBox
    Friend WithEvents txtMError As System.Windows.Forms.TextBox
    Friend WithEvents txtMRet As System.Windows.Forms.TextBox
    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 btnMInit As System.Windows.Forms.Button
    Friend WithEvents btnMCntMode As System.Windows.Forms.Button
    Friend WithEvents btnMSmpMode As System.Windows.Forms.Button
    Friend WithEvents btnMStart As System.Windows.Forms.Button
    Friend WithEvents btnMStop As System.Windows.Forms.Button
    Friend WithEvents btnMData As System.Windows.Forms.Button
    Friend WithEvents btnMCount As System.Windows.Forms.Button
    Friend WithEvents btnMStatus As System.Windows.Forms.Button
    Friend WithEvents btnMCntExit As System.Windows.Forms.Button
    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 txtMCount As System.Windows.Forms.TextBox
    Friend WithEvents txtSCount As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        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.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.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.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.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.Label2, Me.Label1, Me.txtSDeviceName, Me.txtMDeviceName})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 16)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(376, 48)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "デバイス名"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(216, 20)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(56, 16)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "スレーブ："
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 20)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 16)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "マスター："
        '
        'txtSDeviceName
        '
        Me.txtSDeviceName.Location = New System.Drawing.Point(280, 20)
        Me.txtSDeviceName.Name = "txtSDeviceName"
        Me.txtSDeviceName.Size = New System.Drawing.Size(72, 19)
        Me.txtSDeviceName.TabIndex = 1
        Me.txtSDeviceName.Text = "TextBox2"
        '
        'txtMDeviceName
        '
        Me.txtMDeviceName.Location = New System.Drawing.Point(80, 20)
        Me.txtMDeviceName.Name = "txtMDeviceName"
        Me.txtMDeviceName.Size = New System.Drawing.Size(88, 19)
        Me.txtMDeviceName.TabIndex = 0
        Me.txtMDeviceName.Text = "TextBox1"
        '
        '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, 72)
        Me.GroupBox2.Name = "GroupBox2"
        Me.GroupBox2.Size = New System.Drawing.Size(376, 120)
        Me.GroupBox2.TabIndex = 1
        Me.GroupBox2.TabStop = False
        Me.GroupBox2.Text = "マスター"
        '
        'txtMRet
        '
        Me.txtMRet.Location = New System.Drawing.Point(104, 90)
        Me.txtMRet.Name = "txtMRet"
        Me.txtMRet.Size = New System.Drawing.Size(256, 19)
        Me.txtMRet.TabIndex = 7
        Me.txtMRet.Text = ""
        '
        'txtMError
        '
        Me.txtMError.Location = New System.Drawing.Point(104, 66)
        Me.txtMError.Name = "txtMError"
        Me.txtMError.Size = New System.Drawing.Size(256, 19)
        Me.txtMError.TabIndex = 6
        Me.txtMError.Text = ""
        '
        'txtMStatus
        '
        Me.txtMStatus.Location = New System.Drawing.Point(104, 42)
        Me.txtMStatus.Name = "txtMStatus"
        Me.txtMStatus.Size = New System.Drawing.Size(256, 19)
        Me.txtMStatus.TabIndex = 5
        Me.txtMStatus.Text = ""
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 90)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(72, 16)
        Me.Label6.TabIndex = 4
        Me.Label6.Text = "戻り値："
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 66)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(72, 16)
        Me.Label5.TabIndex = 3
        Me.Label5.Text = "エラー："
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 42)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(72, 16)
        Me.Label4.TabIndex = 2
        Me.Label4.Text = "ステータス："
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 18)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 16)
        Me.Label3.TabIndex = 1
        Me.Label3.Text = "カウント："
        '
        'txtMCount
        '
        Me.txtMCount.Location = New System.Drawing.Point(104, 18)
        Me.txtMCount.Name = "txtMCount"
        Me.txtMCount.Size = New System.Drawing.Size(256, 19)
        Me.txtMCount.TabIndex = 0
        Me.txtMCount.Text = ""
        '
        '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, 200)
        Me.GroupBox3.Name = "GroupBox3"
        Me.GroupBox3.Size = New System.Drawing.Size(376, 128)
        Me.GroupBox3.TabIndex = 2
        Me.GroupBox3.TabStop = False
        Me.GroupBox3.Text = "スレーブ"
        '
        'txtSRet
        '
        Me.txtSRet.Location = New System.Drawing.Point(104, 90)
        Me.txtSRet.Name = "txtSRet"
        Me.txtSRet.Size = New System.Drawing.Size(256, 19)
        Me.txtSRet.TabIndex = 15
        Me.txtSRet.Text = ""
        '
        'txtSError
        '
        Me.txtSError.Location = New System.Drawing.Point(104, 66)
        Me.txtSError.Name = "txtSError"
        Me.txtSError.Size = New System.Drawing.Size(256, 19)
        Me.txtSError.TabIndex = 14
        Me.txtSError.Text = ""
        '
        'txtSStatus
        '
        Me.txtSStatus.Location = New System.Drawing.Point(104, 42)
        Me.txtSStatus.Name = "txtSStatus"
        Me.txtSStatus.Size = New System.Drawing.Size(256, 19)
        Me.txtSStatus.TabIndex = 13
        Me.txtSStatus.Text = ""
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(16, 90)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(72, 16)
        Me.Label7.TabIndex = 12
        Me.Label7.Text = "戻り値："
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(16, 66)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(72, 16)
        Me.Label8.TabIndex = 11
        Me.Label8.Text = "エラー："
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(16, 42)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(72, 16)
        Me.Label9.TabIndex = 10
        Me.Label9.Text = "ステータス："
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(16, 18)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(72, 16)
        Me.Label10.TabIndex = 9
        Me.Label10.Text = "カウント："
        '
        'txtSCount
        '
        Me.txtSCount.Location = New System.Drawing.Point(104, 18)
        Me.txtSCount.Name = "txtSCount"
        Me.txtSCount.Size = New System.Drawing.Size(256, 19)
        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, 16)
        Me.GroupBox4.Name = "GroupBox4"
        Me.GroupBox4.Size = New System.Drawing.Size(144, 310)
        Me.GroupBox4.TabIndex = 3
        Me.GroupBox4.TabStop = False
        Me.GroupBox4.Text = "マスター"
        '
        'btnMCntExit
        '
        Me.btnMCntExit.Location = New System.Drawing.Point(16, 274)
        Me.btnMCntExit.Name = "btnMCntExit"
        Me.btnMCntExit.Size = New System.Drawing.Size(112, 24)
        Me.btnMCntExit.TabIndex = 8
        Me.btnMCntExit.Text = "CntExit"
        '
        'btnMStatus
        '
        Me.btnMStatus.Location = New System.Drawing.Point(16, 242)
        Me.btnMStatus.Name = "btnMStatus"
        Me.btnMStatus.Size = New System.Drawing.Size(112, 24)
        Me.btnMStatus.TabIndex = 7
        Me.btnMStatus.Text = "転送ステータス"
        '
        'btnMCount
        '
        Me.btnMCount.Location = New System.Drawing.Point(16, 210)
        Me.btnMCount.Name = "btnMCount"
        Me.btnMCount.Size = New System.Drawing.Size(112, 24)
        Me.btnMCount.TabIndex = 6
        Me.btnMCount.Text = "転送カウント"
        '
        'btnMData
        '
        Me.btnMData.Location = New System.Drawing.Point(16, 178)
        Me.btnMData.Name = "btnMData"
        Me.btnMData.Size = New System.Drawing.Size(112, 24)
        Me.btnMData.TabIndex = 5
        Me.btnMData.Text = "取得データ..."
        '
        'btnMStop
        '
        Me.btnMStop.Location = New System.Drawing.Point(16, 146)
        Me.btnMStop.Name = "btnMStop"
        Me.btnMStop.Size = New System.Drawing.Size(112, 24)
        Me.btnMStop.TabIndex = 4
        Me.btnMStop.Text = "転送ストップ"
        '
        'btnMStart
        '
        Me.btnMStart.Location = New System.Drawing.Point(16, 114)
        Me.btnMStart.Name = "btnMStart"
        Me.btnMStart.Size = New System.Drawing.Size(112, 24)
        Me.btnMStart.TabIndex = 3
        Me.btnMStart.Text = "転送スタート"
        '
        'btnMSmpMode
        '
        Me.btnMSmpMode.Location = New System.Drawing.Point(16, 82)
        Me.btnMSmpMode.Name = "btnMSmpMode"
        Me.btnMSmpMode.Size = New System.Drawing.Size(112, 24)
        Me.btnMSmpMode.TabIndex = 2
        Me.btnMSmpMode.Text = "サンプリングモード..."
        '
        'btnMCntMode
        '
        Me.btnMCntMode.Location = New System.Drawing.Point(16, 50)
        Me.btnMCntMode.Name = "btnMCntMode"
        Me.btnMCntMode.Size = New System.Drawing.Size(112, 24)
        Me.btnMCntMode.TabIndex = 1
        Me.btnMCntMode.Text = "モード..."
        '
        'btnMInit
        '
        Me.btnMInit.Location = New System.Drawing.Point(16, 18)
        Me.btnMInit.Name = "btnMInit"
        Me.btnMInit.Size = New System.Drawing.Size(112, 24)
        Me.btnMInit.TabIndex = 0
        Me.btnMInit.Text = "CntInit"
        '
        '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, 16)
        Me.GroupBox5.Name = "GroupBox5"
        Me.GroupBox5.Size = New System.Drawing.Size(144, 310)
        Me.GroupBox5.TabIndex = 9
        Me.GroupBox5.TabStop = False
        Me.GroupBox5.Text = "スレーブ"
        '
        'btnSCntExit
        '
        Me.btnSCntExit.Location = New System.Drawing.Point(16, 274)
        Me.btnSCntExit.Name = "btnSCntExit"
        Me.btnSCntExit.Size = New System.Drawing.Size(112, 24)
        Me.btnSCntExit.TabIndex = 8
        Me.btnSCntExit.Text = "CntExit"
        '
        'btnSStatus
        '
        Me.btnSStatus.Location = New System.Drawing.Point(16, 242)
        Me.btnSStatus.Name = "btnSStatus"
        Me.btnSStatus.Size = New System.Drawing.Size(112, 24)
        Me.btnSStatus.TabIndex = 7
        Me.btnSStatus.Text = "転送ステータス"
        '
        'btnSCount
        '
        Me.btnSCount.Location = New System.Drawing.Point(16, 210)
        Me.btnSCount.Name = "btnSCount"
        Me.btnSCount.Size = New System.Drawing.Size(112, 24)
        Me.btnSCount.TabIndex = 6
        Me.btnSCount.Text = "転送カウント"
        '
        'btnSData
        '
        Me.btnSData.Location = New System.Drawing.Point(16, 178)
        Me.btnSData.Name = "btnSData"
        Me.btnSData.Size = New System.Drawing.Size(112, 24)
        Me.btnSData.TabIndex = 5
        Me.btnSData.Text = "取得データ..."
        '
        'btnSStop
        '
        Me.btnSStop.Location = New System.Drawing.Point(16, 146)
        Me.btnSStop.Name = "btnSStop"
        Me.btnSStop.Size = New System.Drawing.Size(112, 24)
        Me.btnSStop.TabIndex = 4
        Me.btnSStop.Text = "転送ストップ"
        '
        'btnSStart
        '
        Me.btnSStart.Location = New System.Drawing.Point(16, 114)
        Me.btnSStart.Name = "btnSStart"
        Me.btnSStart.Size = New System.Drawing.Size(112, 24)
        Me.btnSStart.TabIndex = 3
        Me.btnSStart.Text = "転送スタート"
        '
        'btnSSampMode
        '
        Me.btnSSampMode.Location = New System.Drawing.Point(16, 82)
        Me.btnSSampMode.Name = "btnSSampMode"
        Me.btnSSampMode.Size = New System.Drawing.Size(112, 24)
        Me.btnSSampMode.TabIndex = 2
        Me.btnSSampMode.Text = "サンプリングモード..."
        '
        'btnSCntMode
        '
        Me.btnSCntMode.Location = New System.Drawing.Point(16, 50)
        Me.btnSCntMode.Name = "btnSCntMode"
        Me.btnSCntMode.Size = New System.Drawing.Size(112, 24)
        Me.btnSCntMode.TabIndex = 1
        Me.btnSCntMode.Text = "モード..."
        '
        'btnSInit
        '
        Me.btnSInit.Location = New System.Drawing.Point(16, 18)
        Me.btnSInit.Name = "btnSInit"
        Me.btnSInit.Size = New System.Drawing.Size(112, 24)
        Me.btnSInit.TabIndex = 0
        Me.btnSInit.Text = "CntInit"
        '
        'btnExit
        '
        Me.btnExit.Location = New System.Drawing.Point(488, 334)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.Size = New System.Drawing.Size(112, 24)
        Me.btnExit.TabIndex = 10
        Me.btnExit.Text = "Exit"
        '
        'Sync
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(696, 366)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnExit, Me.GroupBox4, Me.GroupBox3, Me.GroupBox2, Me.GroupBox1, Me.GroupBox5})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Sync"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "同期サンプリング"
        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

    '===================================================
    '	ダイアログ初期化
    '===================================================
    Private Sub Sync_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '-----------------------------------
        ' PinnedハンドルをM_SampDataに分配する
        '-----------------------------------
        If M_gCh.IsAllocated = False Then
            M_gCh = GCHandle.Alloc(M_SampData, GCHandleType.Pinned)
        End If

        '-----------------------------------
        ' PinnedハンドルをS_SampDataに分配する
        '-----------------------------------
        If S_gCh.IsAllocated = False Then
            S_gCh = GCHandle.Alloc(S_SampData, GCHandleType.Pinned)
        End If


        '-----------------------------------
        '	画面表示の初期化
        '-----------------------------------
        txtMDeviceName.Text = "CNT000"
        txtSDeviceName.Text = "CNT001"

    End Sub

    '===================================================
    '	マスター 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

        '-----------------------------------
        '	デバイス名取得
        '-----------------------------------
        buf = txtMDeviceName.Text

        '-----------------------------------
        '	初期化処理
        '-----------------------------------
        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

    '===================================================
    '	スレーブ 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

        '-----------------------------------
        '	デバイス名取得
        '-----------------------------------
        buf = txtSDeviceName.Text

        '-----------------------------------
        '	初期化処理
        '-----------------------------------
        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

    '===================================================
    '	マスター 条件設定
    '===================================================
    Private Sub btnMCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMCntMode.Click

        '-----------------------------------
        '	条件設定ダイアログを開く
        '-----------------------------------
        frmCntMode.Id = M_Id
        gCntMode.Show()

    End Sub

    '===================================================
    '	スレーブ 条件設定
    '===================================================
    Private Sub btnSCntMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSCntMode.Click

        '-----------------------------------
        '	条件設定ダイアログを開く
        '-----------------------------------
        frmCntMode.Id = S_Id
        gCntMode.Show()

    End Sub

    '===================================================
    '	マスター サンプリングモード設定
    '===================================================
    Private Sub btnMSmpMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMSmpMode.Click
        gMSmpMode.Show()
    End Sub

    '===================================================
    '	スレーブ サンプリングモード設定
    '===================================================
    Private Sub btnSSampMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSSampMode.Click
        gSSmpMode.Show()
    End Sub

    '===================================================
    '	マスター 開始
    '===================================================
    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

        '-----------------------------------
        ' 通知設定
        '-----------------------------------
        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
        '-----------------------------------
        ' 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
        '-----------------------------------
        ' バッファセット
        '-----------------------------------
        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
        '-----------------------------------
        ' カウンタ開始
        '-----------------------------------
        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

        '-----------------------------------
        ' サンプリング開始
        '-----------------------------------
        ret = CntSamplingStart(M_Id)
        csMsg = String.Format("CntStartSampling : RetCode = {0:X} H", ret)
        txtMRet.Text = csMsg
        txtMCount.Text = ""
        txtMStatus.Text = ""
        txtMError.Text = ""
    End Sub

    '===================================================
    '	スレーブ 開始
    '===================================================
    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

        '-----------------------------------
        ' 通知設定
        '-----------------------------------
        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
        '-----------------------------------
        ' 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
        '-----------------------------------
        ' バッファセット
        '-----------------------------------
        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
        '-----------------------------------
        ' カウンタ開始
        '-----------------------------------
        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
        '-----------------------------------
        ' サンプリング開始
        '-----------------------------------
        ret = CntSamplingStart(S_Id)
        csMsg = String.Format("CntStartSampling : RetCode = {0:X} H", ret)
        txtSRet.Text = csMsg
        txtSCount.Text = ""
        txtSStatus.Text = ""
        txtSError.Text = ""
    End Sub

    '===================================================
    '	マスター 停止
    '===================================================
    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

    '===================================================
    '	スレーブ 停止
    '===================================================
    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

    '===================================================
    '	マスター データ取得
    '===================================================
    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"

        '-----------------------------------
        ' ファイルオープン
        '-----------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            txtMRet.Text = "fopen : file open error."
            Exit Sub
        End Try
        '-----------------------------------
        ' ファイルへデータの書き込み
        '-----------------------------------
        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
        '-----------------------------------
        ' ファイルクローズ
        '-----------------------------------
        fp.Close()

        '-----------------------------------
        ' メモ帳
        '-----------------------------------
        Process.Start("notepad.exe", file_name)

    End Sub

    '===================================================
    '	スレーブ データ取得
    '===================================================
    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"

        '-----------------------------------
        ' ファイルオープン
        '-----------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            txtSRet.Text = "fopen : file open error."
            Exit Sub
        End Try
        '-----------------------------------
        ' ファイルへデータの書き込み
        '-----------------------------------
        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

        '-----------------------------------
        ' ファイルクローズ
        '-----------------------------------
        fp.Close()

        '-----------------------------------
        ' メモ帳
        '-----------------------------------
        Process.Start("notepad.exe", file_name)
    End Sub

    '===================================================
    '	マスター カウント値取得
    '===================================================
    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

        '-----------------------------------
        ' カウント値取得
        '-----------------------------------
        ret = CntGetSamplingCount(M_Id, Count, ScanCarry)
        csMsg = String.Format("CntGetSamplingCount : Ret = {0:X} H", ret)
        txtMRet.Text = csMsg
        ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
        txtMCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(Count), 16))

    End Sub

    '===================================================
    '	スレーブ カウント値取得
    '===================================================
    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

        '-----------------------------------
        ' カウント値取得
        '-----------------------------------
        ret = CntGetSamplingCount(S_Id, Count, ScanCarry)
        csMsg = String.Format("CntGetSamplingCount : Ret = {0:X} H", ret)
        txtSRet.Text = csMsg
        ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
        txtSCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(Count), 16))

    End Sub

    '===================================================
    '	マスター ステータス情報取得
    '===================================================
    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

        '-----------------------------------
        ' ステータス情報取得
        '-----------------------------------
        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

    '===================================================
    '	スレーブ ステータス情報取得
    '===================================================
    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

        '-----------------------------------
        ' ステータス情報取得
        '-----------------------------------
        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

    '===================================================
    '	マスター 終了処理
    '===================================================
    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
        '------------------------------
        ' 終了処理
        '------------------------------
        ret = CntExit(M_Id)
        szMsg = String.Format("CntClose : Ret = {0:X} H", ret)
        txtMRet.Text = szMsg

    End Sub

    '===================================================
    '	スレーブ 終了処理
    '===================================================
    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

        '------------------------------
        ' 終了処理
        '------------------------------
        ret = CntExit(S_Id)
        szMsg = String.Format("CntClose : Ret = {0:X} H", ret)
        txtSRet.Text = szMsg

    End Sub

    '===================================================
    ' プログラム終了
    '===================================================
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dim ret As Integer

        '-----------------------------------
        ' 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

    '===================================================
    ' カウンタ イベント通知処理
    '===================================================
    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
