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

Public Class Infinite
    Inherits System.Windows.Forms.Form

#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 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 btnCntInit As System.Windows.Forms.Button
    Friend WithEvents btnMode As System.Windows.Forms.Button
    Friend WithEvents btnCondition As System.Windows.Forms.Button
    Friend WithEvents btnStart As System.Windows.Forms.Button
    Friend WithEvents btnStop As System.Windows.Forms.Button
    Friend WithEvents btnData As System.Windows.Forms.Button
    Friend WithEvents btnCount As System.Windows.Forms.Button
    Friend WithEvents btnStatus As System.Windows.Forms.Button
    Friend WithEvents btnCntExit As System.Windows.Forms.Button
    Friend WithEvents btnExit As System.Windows.Forms.Button
    Friend WithEvents txtDeviceName As System.Windows.Forms.TextBox
    Friend WithEvents txtReturn As System.Windows.Forms.TextBox
    Friend WithEvents txtError As System.Windows.Forms.TextBox
    Friend WithEvents txtStatus As System.Windows.Forms.TextBox
    Friend WithEvents txtTransCount As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtDeviceName = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtReturn = New System.Windows.Forms.TextBox()
        Me.btnCntInit = New System.Windows.Forms.Button()
        Me.btnMode = New System.Windows.Forms.Button()
        Me.btnCondition = New System.Windows.Forms.Button()
        Me.btnStart = New System.Windows.Forms.Button()
        Me.btnStop = New System.Windows.Forms.Button()
        Me.btnData = New System.Windows.Forms.Button()
        Me.btnCount = New System.Windows.Forms.Button()
        Me.btnStatus = New System.Windows.Forms.Button()
        Me.btnCntExit = New System.Windows.Forms.Button()
        Me.btnExit = New System.Windows.Forms.Button()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.txtError = New System.Windows.Forms.TextBox()
        Me.txtStatus = New System.Windows.Forms.TextBox()
        Me.txtTransCount = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'txtDeviceName
        '
        Me.txtDeviceName.Location = New System.Drawing.Point(112, 16)
        Me.txtDeviceName.Name = "txtDeviceName"
        Me.txtDeviceName.Size = New System.Drawing.Size(128, 19)
        Me.txtDeviceName.TabIndex = 0
        Me.txtDeviceName.Text = "TextBox1"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 16)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "デバイス名："
        '
        'txtReturn
        '
        Me.txtReturn.Location = New System.Drawing.Point(112, 286)
        Me.txtReturn.Name = "txtReturn"
        Me.txtReturn.Size = New System.Drawing.Size(232, 19)
        Me.txtReturn.TabIndex = 2
        Me.txtReturn.Text = ""
        '
        'btnCntInit
        '
        Me.btnCntInit.Location = New System.Drawing.Point(384, 8)
        Me.btnCntInit.Name = "btnCntInit"
        Me.btnCntInit.Size = New System.Drawing.Size(104, 26)
        Me.btnCntInit.TabIndex = 3
        Me.btnCntInit.Text = "CntInit"
        '
        'btnMode
        '
        Me.btnMode.Location = New System.Drawing.Point(384, 40)
        Me.btnMode.Name = "btnMode"
        Me.btnMode.Size = New System.Drawing.Size(104, 26)
        Me.btnMode.TabIndex = 4
        Me.btnMode.Text = "カウンタモード"
        '
        'btnCondition
        '
        Me.btnCondition.Location = New System.Drawing.Point(384, 73)
        Me.btnCondition.Name = "btnCondition"
        Me.btnCondition.Size = New System.Drawing.Size(104, 26)
        Me.btnCondition.TabIndex = 5
        Me.btnCondition.Text = "条件設定..."
        '
        'btnStart
        '
        Me.btnStart.Location = New System.Drawing.Point(384, 106)
        Me.btnStart.Name = "btnStart"
        Me.btnStart.Size = New System.Drawing.Size(104, 26)
        Me.btnStart.TabIndex = 6
        Me.btnStart.Text = "転送スタート"
        '
        'btnStop
        '
        Me.btnStop.Location = New System.Drawing.Point(384, 140)
        Me.btnStop.Name = "btnStop"
        Me.btnStop.Size = New System.Drawing.Size(104, 26)
        Me.btnStop.TabIndex = 7
        Me.btnStop.Text = "転送ストップ"
        '
        'btnData
        '
        Me.btnData.Location = New System.Drawing.Point(384, 173)
        Me.btnData.Name = "btnData"
        Me.btnData.Size = New System.Drawing.Size(104, 28)
        Me.btnData.TabIndex = 8
        Me.btnData.Text = "取得データ..."
        '
        'btnCount
        '
        Me.btnCount.Location = New System.Drawing.Point(384, 208)
        Me.btnCount.Name = "btnCount"
        Me.btnCount.Size = New System.Drawing.Size(104, 26)
        Me.btnCount.TabIndex = 9
        Me.btnCount.Text = "転送カウント"
        '
        'btnStatus
        '
        Me.btnStatus.Location = New System.Drawing.Point(384, 241)
        Me.btnStatus.Name = "btnStatus"
        Me.btnStatus.Size = New System.Drawing.Size(104, 26)
        Me.btnStatus.TabIndex = 10
        Me.btnStatus.Text = "転送ステータス"
        '
        'btnCntExit
        '
        Me.btnCntExit.Location = New System.Drawing.Point(384, 274)
        Me.btnCntExit.Name = "btnCntExit"
        Me.btnCntExit.Size = New System.Drawing.Size(104, 26)
        Me.btnCntExit.TabIndex = 11
        Me.btnCntExit.Text = "CntExit"
        '
        'btnExit
        '
        Me.btnExit.Location = New System.Drawing.Point(384, 306)
        Me.btnExit.Name = "btnExit"
        Me.btnExit.Size = New System.Drawing.Size(104, 26)
        Me.btnExit.TabIndex = 12
        Me.btnExit.Text = "Exit"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 286)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(88, 16)
        Me.Label2.TabIndex = 13
        Me.Label2.Text = "戻り値："
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 260)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(88, 16)
        Me.Label3.TabIndex = 14
        Me.Label3.Text = "エラー："
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 234)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(88, 16)
        Me.Label4.TabIndex = 15
        Me.Label4.Text = "ステータス："
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 208)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(88, 16)
        Me.Label5.TabIndex = 16
        Me.Label5.Text = "転送カウント："
        '
        'txtError
        '
        Me.txtError.Location = New System.Drawing.Point(112, 260)
        Me.txtError.Name = "txtError"
        Me.txtError.Size = New System.Drawing.Size(232, 19)
        Me.txtError.TabIndex = 17
        Me.txtError.Text = ""
        '
        'txtStatus
        '
        Me.txtStatus.Location = New System.Drawing.Point(112, 234)
        Me.txtStatus.Name = "txtStatus"
        Me.txtStatus.Size = New System.Drawing.Size(232, 19)
        Me.txtStatus.TabIndex = 18
        Me.txtStatus.Text = ""
        '
        'txtTransCount
        '
        Me.txtTransCount.Location = New System.Drawing.Point(112, 208)
        Me.txtTransCount.Name = "txtTransCount"
        Me.txtTransCount.Size = New System.Drawing.Size(232, 19)
        Me.txtTransCount.TabIndex = 19
        Me.txtTransCount.Text = ""
        '
        'Infinite
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
        Me.ClientSize = New System.Drawing.Size(504, 342)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtTransCount, Me.txtStatus, Me.txtError, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.btnExit, Me.btnCntExit, Me.btnStatus, Me.btnCount, Me.btnData, Me.btnStop, Me.btnStart, Me.btnCondition, Me.btnMode, Me.btnCntInit, Me.txtReturn, Me.Label1, Me.txtDeviceName})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Infinite"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "無限サンプリング"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Shared Id As Short

    Dim SamplingData(DATA_NUM - 1, CHANNEL_NUM - 1) As UInt32 ' サンプリング用バッファ
    Dim NumberOfData As Integer
    Dim ReadIndex As Integer          'リードポインタ
    Dim WriteIndex As Integer         ' ライトポインタ
    Dim PreCount As Integer           '前回の転送カウント
    Dim PreCarry As Integer           '前回のキャリーカウント
    Dim FileName As String = "Infinite.csv"

    Dim gMode As New frmMode()
    Dim gCondition As New frmCondition()

    Dim gCh As GCHandle
    Dim pSamplingData As IntPtr

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

        '-----------------------------------
        '	画面表示の初期化
        '-----------------------------------
        txtDeviceName.Text = "CNT000"
        '-----------------------------------
        ' PinnedハンドルをSamplingDataに分配する
        '-----------------------------------
        If gCh.IsAllocated = False Then
            gCh = GCHandle.Alloc(SamplingData, GCHandleType.Pinned)
        End If

    End Sub

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

        '-----------------------------------
        '	デバイス名取得
        '-----------------------------------
        buf = txtDeviceName.Text
        '-----------------------------------
        '	初期化処理
        '-----------------------------------
        ret = CntInit(buf, Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntInit ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str

    End Sub

    '---------------------------------------------
    ' カウンタモード設定画面表示
    '---------------------------------------------
    Private Sub btnMode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMode.Click
        gMode.Show()
    End Sub

    '===================================================
    '	条件設定
    '===================================================
    Private Sub btnCondition_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCondition.Click
        gCondition.Show()
    End Sub

    '===================================================
    '	転送スタート
    '===================================================
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim ChNo(CHANNEL_NUM - 1) As Short
        Dim i As Short
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' メッセージ通知設定
        '-----------------------------------
        ret = CntNotifySamplingStop(Id, Me.Handle)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingStop ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        ret = CntNotifySamplingCount(Id, frmCondition.NumberOfData, Me.Handle)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntNotifySamplingCount ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '-----------------------------------
        '	FIFOのリセット(入力)
        '-----------------------------------
        ret = CntSamplingFifoReset(Id)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingFifoReset ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '-----------------------------------
        '	データバッファを設定
        '-----------------------------------
        Array.Clear(SamplingData, 0, SamplingData.Length)

        pSamplingData = gCh.AddrOfPinnedObject()
        ret = CntSamplingSetBuffer(Id, pSamplingData, CHANNEL_NUM, DATA_NUM, CNTS_WRITE_RING)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingSetBuffer ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '------------------------------
        ' パラメータのセット
        '------------------------------
        For i = 0 To CHANNEL_NUM - 1
            ChNo(i) = i
        Next i
        '------------------------------
        ' カウント開始処理
        '------------------------------
        ret = CntStartCount(Id, ChNo, CHANNEL_NUM)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntStartCount ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        '-----------------------------------
        '	カウント初期化
        '-----------------------------------
        ReadIndex = 0   ' リードポインタ
        WriteIndex = 0  ' ライトポインタ
        PreCount = 0    ' 前回の転送カウント
        PreCarry = 0    ' 前回のキャリーカウント
        '-----------------------------------
        '	データファイル削除
        '-----------------------------------
        FileOpen(1, FileName, OpenMode.Append)
        FileClose(1)
        Kill(FileName)
        '-----------------------------------
        '	転送スタート
        '-----------------------------------
        ret = CntSamplingStart(Id)
        If ret <> CNT_ERR_SUCCESS Then
            CntGetErrorString(ret, error_string)
            str = String.Format("CntSamplingStart ret = {0:d}: {1}", ret, error_string)
            txtReturn.Text = str
            Exit Sub
        End If
        txtTransCount.Text = ""
        txtStatus.Text = ""
        txtError.Text = ""
    End Sub

    '===================================================
    '	転送ストップ
    '===================================================
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        '	転送ストップ
        '-----------------------------------
        ret = CntSamplingStop(Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntSamplingStop ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str
    End Sub

    '===================================================
    '	データファイル表示
    '===================================================
    Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click
        '-----------------------------------
        ' notepad
        '-----------------------------------
        Process.Start("notepad.exe", FileName)
    End Sub

    '===================================================
    '	転送カウント
    '===================================================
    Private Sub btnCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCount.Click
        Dim count, carry As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        count = 0
        carry = 0
        '-----------------------------------
        ' 転送カウント取得
        '-----------------------------------
        ret = CntGetSamplingCount(Id, count, carry)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntGetSamplingCount ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str
        If ret = CNT_ERR_SUCCESS Then
            ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
            txtTransCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(count), 16))
        End If

    End Sub

    '===================================================
    '	転送ステータス
    '===================================================
    Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatus.Click
        Dim status, err As Integer
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' 転送ステータス取得
        '-----------------------------------
        ret = CntGetSamplingStatus(Id, status, err)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntGetSamplingStatus ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str
        If ret <> CNT_ERR_SUCCESS Then
            Return
        End If

        ' Status
        str = ""
        If (status And CNTS_STATUS_BMSTOP) Then
            str = str & "BmStop,"
        End If
        If (status And CNTS_STATUS_CNTSTART) Then
            str = str & "CntStart,"
        End If
        If (status And CNTS_STATUS_CNTSTOP) Then
            str = str & "CntStop,"
        End If
        If (status And CNTS_STATUS_TRGIN) Then
            str = str & "TrgIn,"
        End If
        If (status And CNTS_STATUS_OVERRUN) Then
            str = str & "Overrun"
        End If
        txtStatus.Text = str
        ' Error
        str = ""
        If (err And CNTS_STATUS_FIFOFULL) Then
            str = str & "FifoFull,"
        End If
        If (err And CNTS_STATUS_SGOVERIN) Then
            str = str & "S/GOverIn,"
        End If
        If (err And CNTS_STATUS_TRGERR) Then
            str = str & "TrgErr,"
        End If
        If (err And CNTS_STATUS_CLKERR) Then
            str = str & "ClkErr,"
        End If
        If (err And CNTS_STATUS_SLAVEHALT) Then
            str = str & "SlaveHalt,"
        End If
        If (err And CNTS_STATUS_MASTERHALT) Then
            str = str & "MasterHalt"
        End If

        txtError.Text = str

    End Sub
    '===================================================
    '	CntExit
    '===================================================
    Private Sub btnCntExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCntExit.Click
        Dim error_string As New StringBuilder("", 256)
        Dim str As String
        Dim ret As Integer

        '-----------------------------------
        ' CntExit
        '-----------------------------------
        ret = CntExit(Id)
        CntGetErrorString(ret, error_string)
        str = String.Format("CntExit ret = {0:d}: {1}", ret, error_string)
        txtReturn.Text = str


    End Sub

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

        '-----------------------------------
        ' GCHandleのフリー
        '-----------------------------------
        If gCh.IsAllocated = True Then
            gCh.Free()
        End If
        End
    End Sub

    '---------------------------------------------
    ' カウンタ イベント通知処理
    '---------------------------------------------
    Protected Overrides Sub WndProc(ByRef m As Message)

        Dim ret As Integer
        Dim count, carry As Integer
        Dim data_count, ch_no As Integer
        Dim Cnt As UInteger
        Dim buf As New String("", 100)
        Dim str As String

        Select Case m.Msg
            Case CNTM_SAMPLING_COUNT ' 指定個数転送完了
                '-----------------------------------
                ' データバッファ上における転送位置を取得
                '-----------------------------------
                ret = CntGetSamplingWritePointer(Id, WriteIndex, count, carry)
                '-----------------------------------
                ' オーバーランエラーをチェック
                '-----------------------------------
                If carry = PreCarry Then
                    If count > (PreCount + DATA_NUM) Then
                        ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
                        str = String.Format("Overrun Error!(count = {0:d})", Convert.ToUInt32(Hex(count), 16))
                        txtError.Text = str
                        Exit Sub
                    End If
                End If
                '-----------------------------------
                ' 前回と同じカウント値であれば終了
                '	(転送完了メッセージから呼ばれた場合のため)
                '-----------------------------------
                If (count = PreCount) And (carry = PreCarry) Then
                    Exit Sub
                End If
                '-----------------------------------
                ' ファイルに転送されたデータを書き込みます
                '-----------------------------------
                '-----------------------------------
                ' ファイルを開く
                '-----------------------------------
                Try
                    FileOpen(1, FileName, OpenMode.Append)
                Catch ex As Exception
                    txtReturn.Text = "fopen : file open error."
                    Exit Sub
                End Try
                '-----------------------------------
                ' データ書き込み
                '-----------------------------------
                If WriteIndex > ReadIndex Then
                    ' 転送カウント値を符号ありで設定するため、一度16進文字例変換を経由
                    If Convert.ToUInt32(Hex(count), 16) > (WriteIndex - ReadIndex) Then
                        Cnt = Convert.ToUInt32(Hex(Convert.ToUInt32(Hex(count), 16) - (WriteIndex - ReadIndex)), 16)
                    Else
                        Cnt = Convert.ToUInt32(Hex(count - (WriteIndex - ReadIndex)), 16)
                    End If
                    For data_count = ReadIndex To WriteIndex - 1
                        buf = String.Format("{0:d8}: ", Cnt)
                        For ch_no = 0 To CHANNEL_NUM - 2
                            buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        Next ch_no
                        buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        PrintLine(1, buf)

                        If Cnt = UInteger.MaxValue Then
                            Cnt = 0
                        Else
                            Cnt = Cnt + 1
                        End If
                    Next data_count
                Else
                    ' 転送カウント値を符号ありで設定するため、一度16進文字例変換を経由
                    If Convert.ToUInt32(Hex(count), 16) > (DATA_NUM - ReadIndex + WriteIndex) Then
                        Cnt = Convert.ToUInt32(Hex(Convert.ToUInt32(Hex(count), 16) - (DATA_NUM - ReadIndex + WriteIndex)), 16)
                    Else
                        Cnt = Convert.ToUInt32(Hex(count - (DATA_NUM - ReadIndex + WriteIndex)), 16)
                    End If
                    For data_count = ReadIndex To DATA_NUM - 1
                        buf = String.Format("{0:d8}: ", Cnt)
                        For ch_no = 0 To CHANNEL_NUM - 2
                            buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        Next ch_no
                        buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        PrintLine(1, buf)

                        If Cnt = UInteger.MaxValue Then
                            Cnt = 0
                        Else
                            Cnt = Cnt + 1
                        End If
                    Next data_count
                    For data_count = 0 To WriteIndex - 1
                        buf = String.Format("{0:d8}: ", Cnt)
                        For ch_no = 0 To CHANNEL_NUM - 2
                            buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        Next ch_no
                        buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        PrintLine(1, buf)

                        If Cnt = UInteger.MaxValue Then
                            Cnt = 0
                        Else
                            Cnt = Cnt + 1
                        End If
                    Next data_count
                End If
                '-----------------------------------
                ' ファイルを閉じる
                '-----------------------------------
                FileClose(1)
                '-----------------------------------
                ' リードポインタを設定
                '-----------------------------------
                ReadIndex = WriteIndex
                PreCount = count
                PreCarry = carry
                '-----------------------------------
                ' 転送カウント値を表示
                '-----------------------------------
                ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
                txtTransCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(count), 16))

            Case CNTM_SAMPLING_STOP ' 転送完了
                txtStatus.Text = "Stopped!"
                '-----------------------------------
                ' データバッファ上における転送位置を取得
                '-----------------------------------
                ret = CntGetSamplingWritePointer(Id, WriteIndex, count, carry)
                '-----------------------------------
                ' オーバーランエラーをチェック
                '-----------------------------------
                If carry = PreCarry Then
                    If count > (PreCount + DATA_NUM) Then
                        ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
                        str = String.Format("Overrun Error!(count = {0:d})", Convert.ToUInt32(Hex(count), 16))
                        txtError.Text = str
                        Exit Sub
                    End If
                End If
                '-----------------------------------
                ' 前回と同じカウント値であれば終了
                '	(転送完了メッセージから呼ばれた場合のため)
                '-----------------------------------
                If (count = PreCount) And (carry = PreCarry) Then
                    Exit Sub
                End If
                '-----------------------------------
                ' ファイルに転送されたデータを書き込みます
                '-----------------------------------
                '-----------------------------------
                ' ファイルを開く
                '-----------------------------------
                Try
                    FileOpen(1, FileName, OpenMode.Append)
                Catch ex As Exception
                    txtReturn.Text = "fopen : file open error."
                    Exit Sub
                End Try
                '-----------------------------------
                ' データ書き込み
                '-----------------------------------
                If WriteIndex > ReadIndex Then
                    ' 転送カウント値を符号ありで設定するため、一度16進文字例変換を経由
                    If Convert.ToUInt32(Hex(count), 16) > (WriteIndex - ReadIndex) Then
                        Cnt = Convert.ToUInt32(Hex(Convert.ToUInt32(Hex(count), 16) - (WriteIndex - ReadIndex)), 16)
                    Else
                        Cnt = Convert.ToUInt32(Hex(count - (WriteIndex - ReadIndex)), 16)
                    End If
                    For data_count = ReadIndex To WriteIndex - 1
                        buf = String.Format("{0:d8}: ", Cnt)
                        For ch_no = 0 To CHANNEL_NUM - 2
                            buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        Next ch_no
                        buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        PrintLine(1, buf)

                        If Cnt = UInteger.MaxValue Then
                            Cnt = 0
                        Else
                            Cnt = Cnt + 1
                        End If
                    Next data_count
                Else
                    ' 転送カウント値を符号ありで設定するため、一度16進文字例変換を経由
                    If Convert.ToUInt32(Hex(count), 16) > (DATA_NUM - ReadIndex + WriteIndex) Then
                        Cnt = Convert.ToUInt32(Hex(Convert.ToUInt32(Hex(count), 16) - (DATA_NUM - ReadIndex + WriteIndex)), 16)
                    Else
                        Cnt = Convert.ToUInt32(Hex(count - (DATA_NUM - ReadIndex + WriteIndex)), 16)
                    End If
                    For data_count = ReadIndex To DATA_NUM - 1
                        buf = String.Format("{0:d8}: ", Cnt)
                        For ch_no = 0 To CHANNEL_NUM - 2
                            buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        Next ch_no
                        buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        PrintLine(1, buf)

                        If Cnt = UInteger.MaxValue Then
                            Cnt = 0
                        Else
                            Cnt = Cnt + 1
                        End If
                    Next data_count
                    For data_count = 0 To WriteIndex - 1
                        buf = String.Format("{0:d8}: ", Cnt)
                        For ch_no = 0 To CHANNEL_NUM - 2
                            buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        Next ch_no
                        buf = String.Format("{0}{1:X8}, ", buf, SamplingData(data_count, ch_no))
                        PrintLine(1, buf)

                        If Cnt = UInteger.MaxValue Then
                            Cnt = 0
                        Else
                            Cnt = Cnt + 1
                        End If
                    Next data_count
                End If
                '-----------------------------------
                ' ファイルを閉じる
                '-----------------------------------
                FileClose(1)
                '-----------------------------------
                ' リードポインタを設定
                '-----------------------------------
                ReadIndex = WriteIndex
                PreCount = count
                PreCarry = carry
                '-----------------------------------
                ' 転送カウント値を表示
                '-----------------------------------
                ' 転送カウント値を符号無しで表示するため、一度16進文字例変換を経由
                txtTransCount.Text = String.Format("{0:d}", Convert.ToUInt32(Hex(count), 16))
        End Select
        MyBase.WndProc(m)

    End Sub

End Class
