﻿Imports System.Text

Public Class Form1
    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Public Shared g_id As Short                        ' ID for device access
    Public Shared g_channel As Short                   ' Counter channel
    Dim g_event_count As Integer                       ' Comparison count match event occurrence count
    Public Shared g_compare_data_num As Integer = 1    ' Total number of comparison count data to be set
    Public Shared g_compare_repeat As Short = 0        ' Repeat setting of comparison count data array
    Public Shared g_compare_data_0 As Integer = 100    ' Comparison count data 0
    Public Shared g_compare_data_1 As Integer = 200    ' Comparison count data 1
    Public Shared g_compare_data_2 As Integer = 300    ' Comparison count data 2
    Public Shared g_compare_data_3 As Integer = 400    ' Comparison count data 3
    Public Shared g_compare_data_4 As Integer = 500    ' Comparison count data 4
    Public Shared g_preset_data_num As Integer = 1     ' Total number of the preset data to be set
    Public Shared g_preset_repeat As Short = 0         ' Repeat setting of preset data array
    Public Shared g_preset_data_0 As Integer = 0       ' Counter preset data 0
    Public Shared g_preset_data_1 As Integer = 100     ' Counter preset data 1
    Public Shared g_preset_data_2 As Integer = 200     ' Counter preset data 2
    Public Shared g_preset_data_3 As Integer = 300     ' Counter preset data 3
    Public Shared g_preset_data_4 As Integer = 400     ' Counter preset data 4

    Private Sub Counter_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' Set the default button
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' Update the initial display of device name
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' Update the initial display of counter channel
        '----------------------------------------
        TextBox_ChannelNo.Text = "0"
        '----------------------------------------
        ' Update the initial display of counter preset value
        '----------------------------------------
        TextBox_PresetData.Text = "0"
        '----------------------------------------
        ' Update the initial display of current count value
        '----------------------------------------
        TextBox_CountData.Text = "0"
        '----------------------------------------
        ' Update the initial display of status
        '----------------------------------------
        TextBox_Status.Text = ""
        '----------------------------------------
        ' Updated the initial display of comparison count match event occurrence count
        '----------------------------------------
        TextBox_EventCount.Text = "0"
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
        g_channel = 0
        g_event_count = 0
    End Sub

    '================================================================================
    ' It is the process when the AioInit button is clicked
    ' Initialize the device
    '================================================================================
    Private Sub Button_AioInit_Click(sender As Object, e As EventArgs) Handles Button_AioInit.Click
        Dim device_name As String                         ' Device name
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Get device name from textbox
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' Device initialization
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioInit(device_name, g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioInit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioResetDevice button is clicked
    ' Reset the device
    '================================================================================
    Private Sub Button_AioResetDevice_Click(sender As Object, e As EventArgs) Handles Button_AioResetDevice.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Reset device
        '----------------------------------------
        ret = AioResetDevice(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioResetDevice = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the ChannelNo textbox is changed
    ' Set the counter channel
    '================================================================================
    Private Sub TextBox_ChannelNo_TextChanged(sender As Object, e As EventArgs) Handles TextBox_ChannelNo.TextChanged
        '----------------------------------------
        ' Get counter channel from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChannelNo.Text, g_channel) = False) Then
            g_channel = 0
        End If
    End Sub

    '================================================================================
    ' It is the process when the Set button is clicked
    ' Open the counter setting dialog
    '================================================================================
    Private Sub Button_Setting_Click(sender As Object, e As EventArgs) Handles Button_Setting.Click
        '----------------------------------------
        ' Open the condition setting dialog
        '----------------------------------------
        Dim setting_dlg As New Setting
        If (setting_dlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' Display the message that setting is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Set: Successful completion"
        End If
        setting_dlg.Dispose()
    End Sub

    '================================================================================
    ' It is the process when the AioPresetCnt button is clicked
    ' Preset the counter
    '================================================================================
    Private Sub Button_PresetCnt_Click(sender As Object, e As EventArgs) Handles Button_PresetCnt.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim preset_data As Integer                        ' Counter preset value
        Dim cnt_count As Integer                          ' Count value

        '----------------------------------------
        ' Get counter preset value from textbox
        '----------------------------------------
        If (Integer.TryParse(TextBox_PresetData.Text, preset_data) = False) Then
            preset_data = 0
        End If
        '----------------------------------------
        ' Preset the counter
        '----------------------------------------
        ret = AioPresetCnt(g_id, g_channel, preset_data)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioPresetCnt = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the count value preset function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' Get count value
            '--------------------
            ret = AioGetCntCount(g_id, g_channel, cnt_count)
            '--------------------
            ' Error process
            '--------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "AioGetCntCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '--------------------
            ' Update count value
            '--------------------
            TextBox_CountData.Text = cnt_count.ToString()
        End If
    End Sub

    '================================================================================
    ' It is the process when the Count Start button is clicked
    ' Start the counter action
    '================================================================================
    Private Sub Button_Start_Click(sender As Object, e As EventArgs) Handles Button_Start.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim cnt_status As Integer                         ' Counter status
        Dim cnt_count As Integer                          ' Count value

        '----------------------------------------
        ' Set the event factor to 'Comparison count match event', 'Counter operation error'
        '----------------------------------------
        ret = AioSetCntEvent(g_id, g_channel, Handle, CNTE_DATA_NUM Or CNTE_ERR)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Start: AioSetCntEvent = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Reset status
        '----------------------------------------
        ret = AioResetCntStatus(g_id, g_channel, CNTS_DATA_NUM Or CNTS_ORERR)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Start: AioResetCntStatus = " + ret + " : " + error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Initialize comparison match count
        '----------------------------------------
        g_event_count = 0
        '----------------------------------------
        ' Start the counter action
        '----------------------------------------
        ret = AioStartCnt(g_id, g_channel)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Start: AioStartCnt = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the counter status
        '----------------------------------------
        ret = AioGetCntStatus(g_id, g_channel, cnt_status)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Start: AioGetCntStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display status
        '----------------------------------------
        text_string = String.Format("{0:x8}H (", cnt_status)
        '--------------------
        ' When status 'Counter in operation' is ON
        '--------------------
        If (cnt_status And CNTS_BUSY) = CNTS_BUSY Then
            text_string += "Operating"
        '--------------------
        ' When status 'Counter in operation' is OFF
        '--------------------
        Else
            text_string += "Stop"
        End If
        '--------------------
        ' When status 'Comparison count match' is ON
        '--------------------
        If (cnt_status And CNTS_DATA_NUM) = CNTS_DATA_NUM Then
            text_string += ", Comparison count match"
        End If
        '--------------------
        ' When status 'Overrun' is ON
        '--------------------
        If (cnt_status And CNTS_ORERR) = CNTS_ORERR Then
            text_string += ", Overrun"
        End If
        '--------------------
        ' When status 'Counter operation error' is ON
        '--------------------
        If (cnt_status And CNTS_ERR) = CNTS_ERR Then
            text_string += ", Counter operation error"
        End If
        text_string += ")"
        TextBox_Status.Text = text_string
        '----------------------------------------
        ' Get count value
        '----------------------------------------
        ret = AioGetCntCount(g_id, g_channel, cnt_count)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Start: AioGetCntCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display count value
        '----------------------------------------
        TextBox_CountData.Text = cnt_count.ToString()
        '----------------------------------------
        ' Update the comparison match count
        '----------------------------------------
        TextBox_EventCount.Text = "0"
        '----------------------------------------
        ' Display the message that count start is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Count Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioGetCntCount button is clicked
    ' Get the count and display it on the dialog
    '================================================================================
    Private Sub Button_GetCntCount_Click(sender As Object, e As EventArgs) Handles Button_GetCntCount.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim cnt_count As Integer                          ' Count value

        '----------------------------------------
        ' Get count value
        '----------------------------------------
        ret = AioGetCntCount(g_id, g_channel, cnt_count)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetCntCount = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the count value acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' Update count value
            '--------------------
            TextBox_CountData.Text = cnt_count.ToString()
        End If
    End Sub

    '================================================================================
    ' It is the process when the AioGetCntStatus button is clicked
    ' Get the counter status and display it on the dialog
    '================================================================================
    Private Sub Button_GetCntStatus_Click(sender As Object, e As EventArgs) Handles Button_GetCntStatus.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim cnt_status As Integer                         ' Status

        '----------------------------------------
        ' Get the counter status
        '----------------------------------------
        ret = AioGetCntStatus(g_id, g_channel, cnt_status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetCntStatus = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        '  When the status acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", cnt_status)
            '--------------------
            ' When status 'Counter in operation' is ON
            '--------------------
            If ((cnt_status And CNTS_BUSY) = CNTS_BUSY) Then
                text_string += "Operating"
            '--------------------
            ' When status 'Counter in operation' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Comparison count match' is ON
            '--------------------
            If ((cnt_status And CNTS_DATA_NUM) = CNTS_DATA_NUM) Then
                text_string += ", Comparison count match"
            End If
            '--------------------
            ' When status 'Overrun' is ON
            '--------------------
            If ((cnt_status And CNTS_ORERR) = CNTS_ORERR) Then
                text_string += ", Overrun"
            End If
            '--------------------
            ' When status 'Counter operation error' is ON
            '--------------------
            If ((cnt_status And CNTS_ERR) = CNTS_ERR) Then
                text_string += ", Counter operation error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
        End If
    End Sub

    '================================================================================
    ' It is the process when the Counter Stop button is clicked
    ' Stop the counter action
    '================================================================================
    Private Sub Button_Stop_Click(sender As Object, e As EventArgs) Handles Button_Stop.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim cnt_count As Integer                          ' Count value
        Dim cnt_status As Integer                         ' Status

        '----------------------------------------
        ' Stop the counter action
        '----------------------------------------
        ret = AioStopCnt(g_id, g_channel)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Stop: AioStopCnt = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get count value
        '----------------------------------------
        ret = AioGetCntCount(g_id, g_channel, cnt_count)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Stop: AioGetCntCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display count value
        '----------------------------------------
        TextBox_CountData.Text = cnt_count.ToString()
        '----------------------------------------
        ' Get the counter status
        '----------------------------------------
        ret = AioGetCntStatus(g_id, g_channel, cnt_status)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Count Stop: AioGetCntStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display status
        '----------------------------------------
        text_string = String.Format("{0:x8}H (", cnt_status)
        '--------------------
        ' When status 'Counter in operation' is ON
        '--------------------
        If ((cnt_status And CNTS_BUSY) = CNTS_BUSY) Then
            text_string += "Operating"
        '--------------------
        ' When status 'Counter in operation' is OFF
        '--------------------
        Else
            text_string += "Stop"
        End If
        '--------------------
        ' When status 'Comparison count match' is ON
        '--------------------
        If ((cnt_status And CNTS_DATA_NUM) = CNTS_DATA_NUM) Then
            text_string += ", Comparison count match"
        End If
        '--------------------
        ' When status 'Overrun' is ON
        '--------------------
        If ((cnt_status And CNTS_ORERR) = CNTS_ORERR) Then
            text_string += ", Overrun"
        End If
        '--------------------
        ' When status 'Counter operation error' is ON
        '--------------------
        If ((cnt_status And CNTS_ERR) = CNTS_ERR) Then
            text_string += ", Counter operation error"
        End If
        text_string += ")"
        TextBox_Status.Text = text_string
        '----------------------------------------
        ' Display the message that count stop is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Count Stop: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioExit button is clicked
    ' Perform the exit process of device
    '================================================================================
    Private Sub Button_AioExit_Click(sender As Object, e As EventArgs) Handles Button_AioExit.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Exit process
        '----------------------------------------
        ret = AioExit(g_id)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioExit = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Close button is clicked
    ' Perform the exit process of device and terminate the application
    '================================================================================
    Private Sub Button_Close_Click(sender As Object, e As EventArgs) Handles Button_Close.Click
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' Operation when event occurred
    ' Process for each event occurred
    '================================================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim cnt_count As Integer                          ' Count value
        Dim cnt_status As Integer                         ' Status
        Dim cnt_channel As Short                          ' Channel

        '----------------------------------------
        ' Comparison count match event
        ' Acquire and display data
        '----------------------------------------
        If (m.Msg = AIOM_CNTE_DATA_NUM) Then
            '----------------------------------------
            ' Get counter channel
            '----------------------------------------
            cnt_channel = (m.WParam.ToInt32 >> 16) And &HFFFF
            If (cnt_channel = g_channel) Then
                '----------------------------------------
                ' Get count value
                '----------------------------------------
                ret = AioGetCntCount(g_id, cnt_channel, cnt_count)
                '----------------------------------------
                ' Error process
                '----------------------------------------
                '--------------------
                ' Exit the process if not completed successfully
                '--------------------
                If (ret <> 0) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    TextBox_ReturnCode.Text = "Comparison Count Match Event Process: AioGetCntCount = " & ret & " : " & error_string.ToString()
                    Exit Sub
                End If
                '----------------------------------------
                ' Display count value
                '----------------------------------------
                TextBox_CountData.Text = cnt_count.ToString()
                '----------------------------------------
                ' Get the counter status
                '----------------------------------------
                ret = AioGetCntStatus(g_id, cnt_channel, cnt_status)
                '----------------------------------------
                ' Error process
                '----------------------------------------
                '--------------------
                ' Exit the process if not completed successfully
                '--------------------
                If (ret <> 0) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    TextBox_ReturnCode.Text = "Comparison Count Match Event Process: AioGetCntStatus = " & ret & " : " & error_string.ToString()
                    Exit Sub
                End If
                '----------------------------------------
                ' Display status
                '----------------------------------------
                text_string = String.Format("{0:x8}H (", cnt_status)
                '--------------------
                ' When status 'Counter in operation' is ON
                '--------------------
                If ((cnt_status And CNTS_BUSY) = CNTS_BUSY) Then
                    text_string += "Operating"
                '--------------------
                ' When status 'Counter in operation' is OFF
                '--------------------
                Else
                    text_string += "Stop"
                End If
                '--------------------
                ' When status 'Comparison count match' is ON
                '--------------------
                If ((cnt_status And CNTS_DATA_NUM) = CNTS_DATA_NUM) Then
                    text_string += ", Comparison count match"
                End If
                '--------------------
                ' When status 'Overrun' is ON
                '--------------------
                If ((cnt_status And CNTS_ORERR) = CNTS_ORERR) Then
                    text_string += ", Overrun"
                End If
                '--------------------
                ' When status 'Counter operation error' is ON
                '--------------------
                If ((cnt_status And CNTS_ERR) = CNTS_ERR) Then
                    text_string += ", Counter operation error"
                End If
                text_string += ")"
                TextBox_Status.Text = text_string
                '----------------------------------------
                ' Display the comparison count match event occurrence count
                '----------------------------------------
                g_event_count += 1
                TextBox_EventCount.Text = g_event_count.ToString()
                '----------------------------------------
                ' Reset status
                '----------------------------------------
                ret = AioResetCntStatus(g_id, cnt_channel, CNTS_DATA_NUM)
                '----------------------------------------
                ' Error process
                '----------------------------------------
                '--------------------
                ' Exit the process if not completed successfully
                '--------------------
                If (ret <> 0) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    TextBox_ReturnCode.Text = "Comparison Count Match Event Process: AioResetCntStatus = " & ret & " : " & error_string.ToString()
                    Exit Sub
                End If
                '----------------------------------------
                ' Display the message that event processing is completed
                '----------------------------------------
                TextBox_ReturnCode.Text = "Comparison Count Match Event Process: Successful completion"
            End If
            Exit Sub
        End If
        '----------------------------------------
        ' Counter operation error
        ' Display status
        '----------------------------------------
        If (m.Msg = AIOM_CNTE_ERR) Then
            '----------------------------------------
            ' Get counter channel
            '----------------------------------------
            cnt_channel = (m.WParam.ToInt32 >> 16) And &HFFFF
            If (cnt_channel = g_channel) Then
                '----------------------------------------
                ' Get the counter status
                '----------------------------------------
                ret = AioGetCntStatus(g_id, cnt_channel, cnt_status)
                '----------------------------------------
                ' Error process
                '----------------------------------------
                '--------------------
                ' Exit the process if not completed successfully
                '--------------------
                If (ret <> 0) Then
                    '--------------------
                    ' Get error string
                    '--------------------
                    AioGetErrorString(ret, error_string)
                    '--------------------
                    ' Display message
                    '--------------------
                    TextBox_ReturnCode.Text = "Counter Operation Error Occurrence Process: AioGetCntStatus = " & ret & " : " & error_string.ToString()
                    Exit Sub
                End If
                '----------------------------------------
                ' Display status
                '----------------------------------------
                text_string = String.Format("{0:x8}H (", cnt_status)
                '--------------------
                ' When status 'Counter in operation' is ON
                '--------------------
                If ((cnt_status And CNTS_BUSY) = CNTS_BUSY) Then
                    text_string += "Operating"
                '--------------------
                ' When status 'Counter in operation' is OFF
                '--------------------
                Else
                    text_string += "Stop"
                End If
                '--------------------
                ' When status 'Comparison count match' is ON
                '--------------------
                If ((cnt_status And CNTS_DATA_NUM) = CNTS_DATA_NUM) Then
                    text_string += ", Comparison count match"
                End If
                '--------------------
                ' When status 'Overrun' is ON
                '--------------------
                If ((cnt_status And CNTS_ORERR) = CNTS_ORERR) Then
                    text_string += ", Overrun"
                End If
                '--------------------
                ' When status 'Counter operation error' is ON
                '--------------------
                If ((cnt_status And CNTS_ERR) = CNTS_ERR) Then
                    text_string += ", Counter operation error"
                End If
                text_string += ")"
                TextBox_Status.Text = text_string
                '----------------------------------------
                ' Display the message that event processing is completed
                '----------------------------------------
                TextBox_ReturnCode.Text = "Counter Operation Error Occurrence Process: Successful completion"
            End If
            Exit Sub
        End If
        MyBase.WndProc(m)
    End Sub
End Class
