﻿Option Strict Off
Option Explicit On 
Imports System.Text

Friend Class Infinite
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

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

    End Sub

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

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

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents 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 Edit_DevName As System.Windows.Forms.TextBox
    Friend WithEvents Edit_TransCount As System.Windows.Forms.TextBox
    Friend WithEvents Edit_Status As System.Windows.Forms.TextBox
    Friend WithEvents Edit_Error As System.Windows.Forms.TextBox
    Friend WithEvents Edit_Return As System.Windows.Forms.TextBox
    Friend WithEvents Button_DioInit As System.Windows.Forms.Button
    Friend WithEvents Button_Condition As System.Windows.Forms.Button
    Friend WithEvents Button_Start As System.Windows.Forms.Button
    Friend WithEvents Button_Stop As System.Windows.Forms.Button
    Friend WithEvents Button_Data As System.Windows.Forms.Button
    Friend WithEvents Button_Count As System.Windows.Forms.Button
    Friend WithEvents Button_Status As System.Windows.Forms.Button
    Friend WithEvents Button_DioExit As System.Windows.Forms.Button
    Friend WithEvents Button_Exit As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Edit_DevName = New System.Windows.Forms.TextBox()
        Me.Edit_TransCount = New System.Windows.Forms.TextBox()
        Me.Edit_Status = New System.Windows.Forms.TextBox()
        Me.Edit_Error = New System.Windows.Forms.TextBox()
        Me.Edit_Return = New System.Windows.Forms.TextBox()
        Me.Button_DioInit = New System.Windows.Forms.Button()
        Me.Button_Condition = New System.Windows.Forms.Button()
        Me.Button_Start = New System.Windows.Forms.Button()
        Me.Button_Stop = New System.Windows.Forms.Button()
        Me.Button_Data = New System.Windows.Forms.Button()
        Me.Button_Count = New System.Windows.Forms.Button()
        Me.Button_Status = New System.Windows.Forms.Button()
        Me.Button_DioExit = New System.Windows.Forms.Button()
        Me.Button_Exit = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 20)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(80, 19)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Device Name:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(7, 211)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(89, 16)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "Transfer Count:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 249)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(43, 16)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "Status:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(7, 287)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(35, 16)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "Error:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(7, 325)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(72, 16)
        Me.Label5.TabIndex = 0
        Me.Label5.Text = "Return Code:"
        '
        'Edit_DevName
        '
        Me.Edit_DevName.Location = New System.Drawing.Point(96, 18)
        Me.Edit_DevName.Name = "Edit_DevName"
        Me.Edit_DevName.Size = New System.Drawing.Size(128, 20)
        Me.Edit_DevName.TabIndex = 1
        Me.Edit_DevName.Text = "TextBox1"
        '
        'Edit_TransCount
        '
        Me.Edit_TransCount.Location = New System.Drawing.Point(96, 208)
        Me.Edit_TransCount.Name = "Edit_TransCount"
        Me.Edit_TransCount.Size = New System.Drawing.Size(240, 20)
        Me.Edit_TransCount.TabIndex = 1
        Me.Edit_TransCount.Text = "TextBox1"
        '
        'Edit_Status
        '
        Me.Edit_Status.Location = New System.Drawing.Point(96, 246)
        Me.Edit_Status.Name = "Edit_Status"
        Me.Edit_Status.Size = New System.Drawing.Size(240, 20)
        Me.Edit_Status.TabIndex = 1
        Me.Edit_Status.Text = "TextBox1"
        '
        'Edit_Error
        '
        Me.Edit_Error.Location = New System.Drawing.Point(96, 284)
        Me.Edit_Error.Name = "Edit_Error"
        Me.Edit_Error.Size = New System.Drawing.Size(240, 20)
        Me.Edit_Error.TabIndex = 1
        Me.Edit_Error.Text = "TextBox1"
        '
        'Edit_Return
        '
        Me.Edit_Return.Location = New System.Drawing.Point(96, 322)
        Me.Edit_Return.Name = "Edit_Return"
        Me.Edit_Return.Size = New System.Drawing.Size(240, 20)
        Me.Edit_Return.TabIndex = 1
        Me.Edit_Return.Text = "TextBox1"
        '
        'Button_DioInit
        '
        Me.Button_DioInit.Location = New System.Drawing.Point(346, 16)
        Me.Button_DioInit.Name = "Button_DioInit"
        Me.Button_DioInit.Size = New System.Drawing.Size(104, 28)
        Me.Button_DioInit.TabIndex = 2
        Me.Button_DioInit.Text = "DioInit"
        '
        'Button_Condition
        '
        Me.Button_Condition.Location = New System.Drawing.Point(346, 54)
        Me.Button_Condition.Name = "Button_Condition"
        Me.Button_Condition.Size = New System.Drawing.Size(104, 28)
        Me.Button_Condition.TabIndex = 2
        Me.Button_Condition.Text = "Set Conditions..."
        '
        'Button_Start
        '
        Me.Button_Start.Location = New System.Drawing.Point(346, 92)
        Me.Button_Start.Name = "Button_Start"
        Me.Button_Start.Size = New System.Drawing.Size(104, 28)
        Me.Button_Start.TabIndex = 2
        Me.Button_Start.Text = "Start Transfer"
        '
        'Button_Stop
        '
        Me.Button_Stop.Location = New System.Drawing.Point(346, 130)
        Me.Button_Stop.Name = "Button_Stop"
        Me.Button_Stop.Size = New System.Drawing.Size(104, 28)
        Me.Button_Stop.TabIndex = 2
        Me.Button_Stop.Text = "Stop Transfer"
        '
        'Button_Data
        '
        Me.Button_Data.Location = New System.Drawing.Point(346, 168)
        Me.Button_Data.Name = "Button_Data"
        Me.Button_Data.Size = New System.Drawing.Size(104, 28)
        Me.Button_Data.TabIndex = 2
        Me.Button_Data.Text = "Get Data..."
        '
        'Button_Count
        '
        Me.Button_Count.Location = New System.Drawing.Point(346, 206)
        Me.Button_Count.Name = "Button_Count"
        Me.Button_Count.Size = New System.Drawing.Size(104, 28)
        Me.Button_Count.TabIndex = 2
        Me.Button_Count.Text = "Transfer Count"
        '
        'Button_Status
        '
        Me.Button_Status.Location = New System.Drawing.Point(346, 244)
        Me.Button_Status.Name = "Button_Status"
        Me.Button_Status.Size = New System.Drawing.Size(104, 28)
        Me.Button_Status.TabIndex = 2
        Me.Button_Status.Text = "Transfer Status"
        '
        'Button_DioExit
        '
        Me.Button_DioExit.Location = New System.Drawing.Point(346, 282)
        Me.Button_DioExit.Name = "Button_DioExit"
        Me.Button_DioExit.Size = New System.Drawing.Size(104, 28)
        Me.Button_DioExit.TabIndex = 2
        Me.Button_DioExit.Text = "DioExit"
        '
        'Button_Exit
        '
        Me.Button_Exit.Location = New System.Drawing.Point(346, 320)
        Me.Button_Exit.Name = "Button_Exit"
        Me.Button_Exit.Size = New System.Drawing.Size(104, 28)
        Me.Button_Exit.TabIndex = 2
        Me.Button_Exit.Text = "Exit"
        '
        'Infinite
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(472, 366)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_DioInit, Me.Edit_DevName, Me.Label1, Me.Label2, Me.Label3, Me.Label4, Me.Label5, Me.Edit_TransCount, Me.Edit_Status, Me.Edit_Error, Me.Edit_Return, Me.Button_Condition, Me.Button_Start, Me.Button_Stop, Me.Button_Data, Me.Button_Count, Me.Button_Status, Me.Button_DioExit, Me.Button_Exit})
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "Infinite"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Infinite Sampling"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Shared Id As Short           'Device Id
    Dim gCh As GCHandle
    Dim pBmBuf As IntPtr
    Dim Ret As Integer                  'Return code
    Dim szError As New StringBuilder("", 256)
    Const DATA_SIZE As Short = 1000     'Data size
    Dim BmBuffer(DATA_SIZE) As Integer  'Data buffer
    Dim DataFileName As String          'File name
    Dim ReadIndex As Integer            'Read pointer
    Dim WriteIndex As Integer           'Write pointer
    Dim PreCount As Integer             'Counter save
    Dim PreCarry As Integer             'Carry save
    Dim gConditionDlg As New ConditionDlg()

    '=========================================================
    ' Dialog initialization
    '=========================================================
    Private Sub Infinite_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Me.Left = (Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.Bounds.Height - Me.Height) / 2

        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_DevName.Text = "DIO000"
        Edit_TransCount.Text = ""
        Edit_Status.Text = ""
        Edit_Error.Text = ""
        Edit_Return.Text = ""
        DataFileName = Application.StartupPath & "\infinite.csv"
        '--------------------------------
        ' Pinned type handle is allocated to BmBuffer
        '--------------------------------
        If gCh.IsAllocated = False Then
            gCh = GCHandle.Alloc(BmBuffer, GCHandleType.Pinned)
        End If
    End Sub

    '=========================================================
    ' DioInit()
    '=========================================================
    Private Sub IDC_BUTTON_DIO_INIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DioInit.Click
        '--------------------------------
        ' Execute initialization function and get ID
        '--------------------------------
        Ret = DioInit(Edit_DevName.Text, Id)
        '--------------------------------
        ' Error process
        '--------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioInit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Show ConditionDlg Window
    '=========================================================
    Private Sub BUTTON_CONDITION_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Condition.Click
        gConditionDlg.Show()
    End Sub

    '=========================================================
    ' Start transfer
    '=========================================================
    Private Sub BUTTON_START_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Start.Click
        '--------------------------------
        ' Screen item initialization
        '--------------------------------
        Edit_TransCount.Text = ""
        Edit_Status.Text = ""
        Edit_Error.Text = ""
        '--------------------------------
        ' Count initialization
        '--------------------------------
        ReadIndex = 0                   'Read pointer
        WriteIndex = 0                  'Write pointer
        PreCount = 0                    'Counter save
        PreCarry = 0                    'Carry save
        '--------------------------------
        ' Set stop message
        '--------------------------------
        Ret = DioDmSetStopEvent(Id, DIODM_DIR_IN, Me.Handle)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmSetStopEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Set notification of message
        '--------------------------------
        Ret = DioDmSetCountEvent(Id, DIODM_DIR_IN, ConditionDlg.NumberOfData, Me.Handle)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmSetCountEvent Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Reset FIFO (input)
        '--------------------------------
        Ret = DioDmFifoReset(Id, DIODM_RESET_FIFO_IN)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmFifoReset Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' BmBuffer is initialized by 0
        '--------------------------------
        System.Array.Clear(BmBuffer, 0, BmBuffer.Length)
        '--------------------------------
        ' The fixed pointer to an object is acquired
        '--------------------------------
        pBmBuf = gCh.AddrOfPinnedObject()
        '--------------------------------
        ' Set data buffer
        '--------------------------------
        Ret = DioDmSetBuffer(Id, DIODM_DIR_IN, pBmBuf, DATA_SIZE, DIODM_WRITE_RING)
        If Ret <> DIO_ERR_SUCCESS Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmSetBuffer Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
        '--------------------------------
        ' Create outdata file
        '--------------------------------
        If System.IO.File.Exists(DataFileName) = True Then
            '--------------------------------
            ' Delete outdata file
            '--------------------------------
            Kill(DataFileName)
        End If
        '--------------------------------
        ' Start transfer
        '--------------------------------
        Ret = DioDmTransferStart(Id, DIODM_DIR_IN)
        If (Ret <> DIO_ERR_SUCCESS) Then
            Call DioGetErrorString(Ret, szError)
            Edit_Return.Text = "DioDmTransferStart Ret = " & Ret & ":" & szError.ToString()
            Exit Sub
        End If
    End Sub

    '=========================================================
    ' Stop transfer
    '=========================================================
    Private Sub BUTTON_STOP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Stop.Click
        '--------------------------------
        ' Stop transfer
        '--------------------------------
        Ret = DioDmTransferStop(Id, DIODM_DIR_IN)
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioDmTransferStop Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Get data
    '=========================================================
    Private Sub BUTTON_DATA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Data.Click
        Shell("notepad.exe " & DataFileName, AppWinStyle.NormalFocus)
    End Sub

    '=========================================================
    ' Transfer count
    '=========================================================
    Private Sub BUTTON_COUNT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Count.Click
        Dim count As Integer = 0
        Dim carry As Integer = 0

        '--------------------------------
        ' Get transfer count
        '--------------------------------
        Ret = DioDmGetCount(Id, DIODM_DIR_IN, count, carry)
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioDmGetCount Ret = " & Ret & ":" & szError.ToString()
        If Ret = DIO_ERR_SUCCESS Then
            Edit_TransCount.Text = CStr(count)
        Else
            Edit_TransCount.Text = ""
        End If
    End Sub

    '=========================================================
    ' Transfer status
    '=========================================================
    Private Sub BUTTON_STATUS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Status.Click
        Dim status As Integer
        Dim err As Integer
		Dim buf As String = String.Empty

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

    '=========================================================
    ' DioExit
    '=========================================================
    Private Sub BUTTON_DIO_EXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DioExit.Click
        '--------------------------------
        ' DioExit
        '--------------------------------
        Ret = DioExit(Id)
        '--------------------------------
        ' Error process
        '--------------------------------
        Call DioGetErrorString(Ret, szError)
        Edit_Return.Text = "DioExit Ret = " & Ret & ":" & szError.ToString()
    End Sub

    '=========================================================
    ' Dialog exit
    '=========================================================
    Private Sub BUTTON_EXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
        '--------------------------------
        ' DioExit
        '--------------------------------
        Ret = DioExit(Id)
        '--------------------------------
        ' GCHandle is freed
        '--------------------------------
        If gCh.IsAllocated = True Then
            gCh.Free()
        End If
        End
    End Sub

    '=========================================================
    ' Message Handler
    '=========================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        Dim i As Integer
        Dim Cnt As Integer
        Dim count As Integer
        Dim carry As Integer
        Dim buf As String

        '--------------------------------
        ' Stop message
        '--------------------------------
        If m.Msg = DIO_DMM_STOP Then
            Edit_Status.Text = "Stopped!"
            '--------------------------------
            ' Get write pointer
            '--------------------------------
            Ret = DioDmGetWritePointer(Id, DIODM_DIR_IN, WriteIndex, count, carry)
            '--------------------------------
            ' Overrun error check
            '--------------------------------
            If PreCarry = carry Then
                If count > PreCount + DATA_SIZE Then
                    Call DioGetErrorString(Ret, szError)
                    Edit_Error.Text = "Overrun Error! (count = " & count & ")"
                    Exit Sub
                End If
            End If
            '-----------------------------------
            ' If count is same as the precount,then return
            '-----------------------------------
            If (count = PreCount) And (carry = PreCarry) Then
                Exit Sub
            End If
            '--------------------------------
            ' Open file
            '--------------------------------
            FileOpen(1, DataFileName, OpenMode.Append)
            '--------------------------------
            ' Write file
            '--------------------------------
            If WriteIndex > ReadIndex Then
                Cnt = count - (WriteIndex - ReadIndex)
                For i = ReadIndex To WriteIndex - 1
                    buf = String.Format("{0:d8}: ,{1:X8}", Cnt, BmBuffer(i))
                    PrintLine(1, buf)
                    Cnt = Cnt + 1
                Next i
            Else
                Cnt = count - (DATA_SIZE - ReadIndex + WriteIndex)
                For i = ReadIndex To DATA_SIZE - 1
                    buf = String.Format("{0:d8}: ,{1:X8}", Cnt, BmBuffer(i))
                    PrintLine(1, buf)
                    Cnt = Cnt + 1
                Next i
                For i = 0 To WriteIndex - 1
                    buf = String.Format("{0:d8}: ,{1:X8}", Cnt, BmBuffer(i))
                    PrintLine(1, buf)
                    Cnt = Cnt + 1
                Next i
            End If
            '--------------------------------
            ' Close file
            '--------------------------------
            FileClose(1)
            '--------------------------------
            ' Show count
            '--------------------------------
            Edit_TransCount.Text = CStr(count)
            '--------------------------------
            ' Set read pointer
            '--------------------------------
            ReadIndex = WriteIndex
            PreCount = count
            PreCarry = carry
        '--------------------------------
        ' Transfer count message
        '--------------------------------
        Else If m.Msg = DIO_DMM_COUNT Then
            '--------------------------------
            ' Get write pointer
            '--------------------------------
            Ret = DioDmGetWritePointer(Id, DIODM_DIR_IN, WriteIndex, count, carry)
            '--------------------------------
            ' Overrun error check
            '--------------------------------
            If PreCarry = carry Then
                If count > PreCount + DATA_SIZE Then
                    Call DioGetErrorString(Ret, szError)
                    Edit_Error.Text = "Overrun Error! (count = " & count & ")"
                    Exit Sub
                End If
            End If
            '--------------------------------
            ' Open file
            '--------------------------------
            FileOpen(1, DataFileName, OpenMode.Append)
            '--------------------------------
            ' Write file
            '--------------------------------
            If WriteIndex > ReadIndex Then
                Cnt = count - (WriteIndex - ReadIndex)
                For i = ReadIndex To WriteIndex - 1
                    buf = String.Format("{0:d8}: ,{1:X8}", Cnt, BmBuffer(i))
                    PrintLine(1, buf)
                    Cnt = Cnt + 1
                Next i
            Else
                Cnt = count - (DATA_SIZE - ReadIndex + WriteIndex)
                For i = ReadIndex To DATA_SIZE - 1
                    buf = String.Format("{0:d8}: ,{1:X8}", Cnt, BmBuffer(i))
                    PrintLine(1, buf)
                    Cnt = Cnt + 1
                Next i
                For i = 0 To WriteIndex - 1
                    buf = String.Format("{0:d8}: ,{1:X8}", Cnt, BmBuffer(i))
                    PrintLine(1, buf)
                    Cnt = Cnt + 1
                Next i
            End If
            '--------------------------------
            ' Close file
            '--------------------------------
            FileClose(1)
            '--------------------------------
            ' Show count
            '--------------------------------
            Edit_TransCount.Text = CStr(count)
            '--------------------------------
            ' Set read pointer
            '--------------------------------
            ReadIndex = WriteIndex
            PreCount = count
            PreCarry = carry
        End If

        MyBase.WndProc(m)
    End Sub

End Class
