﻿Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_AI_RANGE_NUM As Integer = 32    ' Maximum number of elements in analog input range array

    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Public Shared g_id As Short                                               ' ID for device access
    Dim g_sampling_buffer() As Integer = Nothing                              ' User buffer
    Dim g_sampling_buffer_size As Integer                                     ' User buffer size
    Dim g_prev_count As Long                                                  ' Previous position
    Dim g_prev_lap As Long                                                    ' Previous overwrite count
    Dim g_file_name As String = "SamplingData.csv"                            ' Sampling data output file name
    Dim gCh As GCHandle                                                       ' GCHandle for g_sampling_buffer array
    Dim pBuffer As IntPtr                                                     ' IntPtr for g_sampling_buffer array
    Dim g_ai_range() As Short = New Short(MAX_AI_RANGE_NUM - 1) {PM10,        ' +/-10V
                                                                 PM5,         ' +/-5V
                                                                 PM25,        ' +/-2.5V
                                                                 PM125,       ' +/-1.25V
                                                                 PM1,         ' +/-1V
                                                                 PM0625,      ' +/-0.625V
                                                                 PM05,        ' +/-0.5V
                                                                 PM03125,     ' +/-0.3125V
                                                                 PM025,       ' +/-0.25V
                                                                 PM0125,      ' +/-0.125V
                                                                 PM01,        ' +/-0.1V
                                                                 PM005,       ' +/-0.05V
                                                                 PM0025,      ' +/-0.025V
                                                                 PM00125,     ' +/-0.0125V
                                                                 PM001,       ' +/-0.01V
                                                                 P10,         ' 0 ~ 10V
                                                                 P5,          ' 0 ~ 5V
                                                                 P4095,       ' 0 ~ 4.095V
                                                                 P25,         ' 0 ~ 2.5V
                                                                 P125,        ' 0 ~ 1.25V
                                                                 P1,          ' 0 ~ 1V
                                                                 P05,         ' 0 ~ 0.5V
                                                                 P025,        ' 0 ~ 0.25V
                                                                 P01,         ' 0 ~ 0.1V
                                                                 P005,        ' 0 ~ 0.05V
                                                                 P0025,       ' 0 ~ 0.025V
                                                                 P00125,      ' 0 ~ 0.0125V
                                                                 P001,        ' 0 ~ 0.01V
                                                                 P20MA,       ' 0 ~ 20mA
                                                                 P4TO20MA,    ' 4 ~ 20mA
                                                                 PM20MA,      ' +/-20mA
                                                                 P1TO5}       ' 1 ~ 5V

    Private Sub AiSamplingInfiniteUser_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"
        '----------------------------------------
        ' Create the combobox for analog input range
        '----------------------------------------
        ComboBox_RangeValue.Items.Add("-10 - +10V")
        ComboBox_RangeValue.Items.Add("-5 - +5V")
        ComboBox_RangeValue.Items.Add("-2.5 - +2.5V")
        ComboBox_RangeValue.Items.Add("-1.25 - +1.25V")
        ComboBox_RangeValue.Items.Add("-1 - +1V")
        ComboBox_RangeValue.Items.Add("-0.625 - +0.625V")
        ComboBox_RangeValue.Items.Add("-0.5 - +0.5V")
        ComboBox_RangeValue.Items.Add("-0.3125 - +0.3125V")
        ComboBox_RangeValue.Items.Add("-0.25 - +0.25V")
        ComboBox_RangeValue.Items.Add("-0.125 - +0.125V")
        ComboBox_RangeValue.Items.Add("-0.1 - +0.1V")
        ComboBox_RangeValue.Items.Add("-0.05 - +0.05V")
        ComboBox_RangeValue.Items.Add("-0.025 - +0.025V")
        ComboBox_RangeValue.Items.Add("-0.0125 - +0.0125V")
        ComboBox_RangeValue.Items.Add("-0.01 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +10V")
        ComboBox_RangeValue.Items.Add("0 - +5V")
        ComboBox_RangeValue.Items.Add("0 - +4.095V")
        ComboBox_RangeValue.Items.Add("0 - +2.5V")
        ComboBox_RangeValue.Items.Add("0 - +1.25V")
        ComboBox_RangeValue.Items.Add("0 - +1V")
        ComboBox_RangeValue.Items.Add("0 - +0.5V")
        ComboBox_RangeValue.Items.Add("0 - +0.25V")
        ComboBox_RangeValue.Items.Add("0 - +0.1V")
        ComboBox_RangeValue.Items.Add("0 - +0.05V")
        ComboBox_RangeValue.Items.Add("0 - +0.025V")
        ComboBox_RangeValue.Items.Add("0 - +0.0125V")
        ComboBox_RangeValue.Items.Add("0 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +20mA")
        ComboBox_RangeValue.Items.Add("4 - +20mA")
        ComboBox_RangeValue.Items.Add("-20mA - +20mA")
        ComboBox_RangeValue.Items.Add("1 - +5V")
        '----------------------------------------
        ' Change the selection of analog input range combobox to 0
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 0
        '----------------------------------------
        ' Create the combobox for analog input method
        '----------------------------------------
        ComboBox_InpMethod.Items.Add("Single-end input")
        ComboBox_InpMethod.Items.Add("Differential input")
        '----------------------------------------
        ' Change the selection of analog input method combobox to 0
        '----------------------------------------
        ComboBox_InpMethod.SelectedIndex = 0
        '----------------------------------------
        ' Update the initial display of number of channels for analog input
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' Update the initial display of the sampling times
        '----------------------------------------
        TextBox_SamplingTimes.Text = "2000"
        '----------------------------------------
        ' Update the initial display of the data transfer times
        '----------------------------------------
        TextBox_TransferTimes.Text = "1000"
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
        g_sampling_buffer_size = 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_AioResetDev_Click(sender As Object, e As EventArgs) Handles Button_AioResetDev.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 AioSetAiRangeAll button is clicked
    ' Set the input range of analog input
    '================================================================================
    Private Sub Button_SetAiRangeAll_Click(sender As Object, e As EventArgs) Handles Button_SetAiRangeAll.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim index As Integer                              ' Index of combobox

        '----------------------------------------
        ' Update GUI information
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' Get the index of range setting value from combobox
        '----------------------------------------
        index = ComboBox_RangeValue.SelectedIndex
        '----------------------------------------
        ' Set the analog input range (all channels)
        '----------------------------------------
        ret = AioSetAiRangeAll(g_id, g_ai_range(index))
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiRangeAll = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSetAiInputMethod button is clicked
    ' Set the input method of analog input
    '================================================================================
    Private Sub Button_SetAiInpMethod_Click(sender As Object, e As EventArgs) Handles Button_SetAiInpMethod.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim input_method As Integer                       ' Input method

        '----------------------------------------
        ' Update GUI information
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' Get the index of input method setting value from combobox
        '----------------------------------------
        input_method = ComboBox_InpMethod.SelectedIndex
        '----------------------------------------
        ' Set the analog input method
        '----------------------------------------
        ret = AioSetAiInputMethod(g_id, input_method)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiInputMethod = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSetAiChannels button is clicked
    ' Set the number of analog input channels used for sampling
    '================================================================================
    Private Sub Button_SetAiChannels_Click(sender As Object, e As EventArgs) Handles Button_SetAiChannels.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Get the number of channels from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.Text, ai_channels) = False) Then
            ai_channels = 1
        End If
        '----------------------------------------
        ' Set the number of channels for the analog input conversion.
        '----------------------------------------
        ret = AioSetAiChannels(g_id, ai_channels)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiChannels = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Condition Set button is clicked
    ' Open the condition setting dialog
    '================================================================================
    Private Sub Button_ConditionSettings_Click(sender As Object, e As EventArgs) Handles Button_ConditionSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Open the condition setting dialog
        '----------------------------------------
        Dim condition_dlg As New Condition
        If (condition_dlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' Set the event factor to 'Event that AD conversion start', 'Event that device operation end',
            ' 'Event that the specified number of data are transferred', 'Event that overflow',
            ' 'Event that sampling clock error', and 'Event that AD conversion error'.
            ' Caution for modify this function (if modified, the application may not work)
            '----------------------------------------
            ret = AioSetAiEvent(g_id, Handle, AIE_START Or AIE_END Or AIE_DATA_TSF Or AIE_OFERR Or AIE_SCERR Or AIE_ADERR)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Condition Set: AioSetAiEvent = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display the message that condition setting is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Condition Set: Successful completion"
        End If
        condition_dlg.Dispose()
    End Sub

    '================================================================================
    ' It is the process when the User Buffer Size Set button is clicked
    ' Set the user buffer that stores the conversion data
    '================================================================================
    Private Sub Button_UserBufferSettings_Click(sender As Object, e As EventArgs) Handles Button_UserBufferSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim data_size As Short                            ' Data size (Number of packets)
        Dim sampling_times As Long                        ' Sampling times

        '----------------------------------------
        ' Set the analog input conversion data transfer mode to user buffer mode
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiTransferMode(g_id, 1)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "User Buffer Size Set: AioSetAiTransferMode = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' When the user buffer mode is used to transfer data, retrieves data size that is necessary for one sampling
        '----------------------------------------
        ret = AioGetAiSamplingDataSize(g_id, data_size)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "User Buffer Size Set: AioGetAiSamplingDataSize = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the sampling times from textbox
        '----------------------------------------
        If (Long.TryParse(TextBox_SamplingTimes.Text, sampling_times) = False) Then
            sampling_times = 2000
        End If
        '----------------------------------------
        ' Allocate the user buffer for storing conversion data
        '----------------------------------------
        If IsArray(g_sampling_buffer) = True Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_sampling_buffer
        End If
        '--------------------
        ' User buffer size: number of packets that is necessary for one sampling x sampling times
        '--------------------
        g_sampling_buffer_size = data_size * sampling_times
        Try
            ReDim g_sampling_buffer(g_sampling_buffer_size)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "User Buffer Size Set: Failed to allocate the user buffer"
            Exit Sub
        End Try
        '----------------------------------------
        ' Specify Pinned handle to g_sampling_buffer
        '----------------------------------------
        If (gCh.IsAllocated = False) Then
            gCh = GCHandle.Alloc(g_sampling_buffer, GCHandleType.Pinned)
        End If
        '----------------------------------------
        ' Get address object by Pinned handle
        '----------------------------------------
        pBuffer = gCh.AddrOfPinnedObject()
        '----------------------------------------
        ' Set user buffer
        '----------------------------------------
        ret = AioSetAiTransferData(g_id, g_sampling_buffer_size, pBuffer)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "User Buffer Size Set: AioSetAiTransferData = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that user buffer setting is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "User Buffer Size Set: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioSetAiEventTransferTimes button is clicked
    ' Set the sampling times when "Event that the specified number of data are transferred" is used
    '================================================================================
    Private Sub Button_SetAiEventTransferTimes_Click(sender As Object, e As EventArgs) Handles Button_SetAiEventTransferTimes.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim transfer_times As Integer                     ' Data transfer times

        '----------------------------------------
        ' Get the data trasfer times from textbox
        '----------------------------------------
        If (Integer.TryParse(TextBox_TransferTimes.Text, transfer_times) = False) Then
            transfer_times = 1000
        End If
        '----------------------------------------
        ' Set the sampling times when "Event that the specified number of data are transferred" is used
        '----------------------------------------
        ret = AioSetAiEventTransferTimes(g_id, transfer_times)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiEventTransferTimes = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Sampling Start button is clicked
    ' After clearing the memory and status, start sampling.
    '================================================================================
    Private Sub Button_SamplingStart_Click(sender As Object, e As EventArgs) Handles Button_SamplingStart.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 ai_status As Integer                          ' Analog input status

        '----------------------------------------
        ' Clear status
        '----------------------------------------
        ret = AioResetAiStatus(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sampling Start: AioResetAiStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Initialize the transfer count
        '----------------------------------------
        g_prev_count = 0
        g_prev_lap = 0
        '----------------------------------------
        ' Delete the data file
        '----------------------------------------
        File.Delete(g_file_name)
        '----------------------------------------
        ' Start sampling
        '----------------------------------------
        ret = AioStartAi(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sampling Start: AioStartAi = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get status
        '----------------------------------------
        ret = AioGetAiStatus(g_id, ai_status)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sampling Start: AioGetAiStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display status
        '----------------------------------------
        text_string = String.Format("{0:x8}H (", ai_status)
        '--------------------
        ' When status 'Device is running' is ON
        '--------------------
        If (ai_status And AIS_BUSY) = AIS_BUSY Then
            text_string += "Operating"
        '--------------------
        ' When status 'Device is running' is OFF
        '--------------------
        Else
            text_string += "Stop"
        End If
        '--------------------
        ' When status 'Wait the start trigger' is ON
        '--------------------
        If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
            text_string += ", Wait the start trigger"
        End If
        '--------------------
        ' When status 'The specified number of data are stored' is ON
        '--------------------
        If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
            text_string += ", The specified number of data are stored"
        End If
        '--------------------
        ' When status 'Overflow' is ON
        '--------------------
        If (ai_status And AIS_OFERR) = AIS_OFERR Then
            text_string += ", Overflow"
        End If
        '--------------------
        ' When status 'Sampling clock error' is ON
        '--------------------
        If (ai_status And AIS_SCERR) = AIS_SCERR Then
            text_string += ", Sampling clock error"
        End If
        '--------------------
        ' When status 'AD conversion error' is ON
        '--------------------
        If (ai_status And AIS_AIERR) = AIS_AIERR Then
            text_string += ", AD conversion error"
        End If
        '--------------------
        ' When status 'Driver spec error' is ON
        '--------------------
        If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
            text_string += ", Driver spec error"
        End If
        text_string += ")"
        TextBox_Status.Text = text_string
        '----------------------------------------
        ' Update the transfer count
        '----------------------------------------
        TextBox_TotalSamplingPosition.Text = "0"
        '----------------------------------------
        ' Update the overwrite count
        '----------------------------------------
        TextBox_OverwriteCount.Text = "0"
        '----------------------------------------
        ' Display the message that sampling start is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Sampling Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioGetAiTransferCount button is clicked
    ' Get the transfer count and display it on the dialog
    '================================================================================
    Private Sub Button_GetAiTransferCount_Click(sender As Object, e As EventArgs) Handles Button_GetAiTransferCount.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ai_transfer_count As Integer                  ' Transfer count

        '----------------------------------------
        ' Get the number of data that are transferred from the user memory
        '----------------------------------------
        ret = AioGetAiTransferCount(g_id, ai_transfer_count)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiTransferCount = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the transfer count acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' Update the transfer count
            '--------------------
            TextBox_TotalSamplingPosition.Text = ai_transfer_count.ToString()
        End If
    End Sub

    '================================================================================
    ' It is the process when the AioGetAiTransferLap button is clicked
    ' Get the overwrite count and display it on the dialog
    '================================================================================
    Private Sub Button_GetAiTransferLap_Click(sender As Object, e As EventArgs) Handles Button_GetAiTransferLap.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ai_transfer_lap As Integer                    ' Overwrite count

        '----------------------------------------
        ' Get the number of overwrite if the user buffer is used in "Overwrite the memory"
        '----------------------------------------
        ret = AioGetAiTransferLap(g_id, ai_transfer_lap)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiTransferLap = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the overwrite count acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' Update the overwrite count
            '--------------------
            TextBox_OverwriteCount.Text = ai_transfer_lap.ToString()
        End If
    End Sub

    '================================================================================
    ' It is the process when the AioGetAiStatus button is clicked
    ' Get the analog input status and display it on the dialog
    '================================================================================
    Private Sub Button_GetAiStatus_Click(sender As Object, e As EventArgs) Handles Button_GetAiStatus.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 ai_status As Integer                          ' Analog input status

        '----------------------------------------
        ' Get status
        '----------------------------------------
        ret = AioGetAiStatus(g_id, ai_status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiStatus = " & 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 (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
        End If
    End Sub

    '================================================================================
    ' It is the process when the Sampling Stop button is clicked
    ' Stop sampling
    '================================================================================
    Private Sub Button_SamplingStop_Click(sender As Object, e As EventArgs) Handles Button_SamplingStop.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Stop sampling
        '----------------------------------------
        ret = AioStopAi(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sampling Stop: AioStopAi = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that sampling stop is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Sampling 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()
        '----------------------------------------
        ' Release the user buffer for storing conversion data
        '----------------------------------------
        If IsArray(g_sampling_buffer) = True Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_sampling_buffer
        End If
    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)
        '----------------------------------------
        ' Release the user buffer for storing conversion data
        '----------------------------------------
        If IsArray(g_sampling_buffer) = True Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_sampling_buffer
        End If
        '----------------------------------------
        ' 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 ai_transfer_count As Integer                  ' Transfer count
        Dim ai_transfer_lap As Integer                    ' Overwrite count
        Dim ai_status As Integer                          ' Status
        Dim fp As StreamWriter                            ' File pointer
        Dim i As Long                                     ' Loop count

        '----------------------------------------
        ' Event that AD conversion start
        ' Display the status
        '----------------------------------------
        If (m.Msg = AIOM_AIE_START) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Event that AD conversion start Process: AioGetAiStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Event that AD conversion start Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that the specified number of data are transferred
        ' Acquire and display data
        '----------------------------------------
        If (m.Msg = AIOM_AIE_DATA_TSF) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: AioGetAiStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' Get the number of data that are transferred from the user memory
            '----------------------------------------
            ret = AioGetAiTransferCount(g_id, ai_transfer_count)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: AioGetAiTransferCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Update the transfer count
            '----------------------------------------
            TextBox_TotalSamplingPosition.Text = ai_transfer_count.ToString()
            '----------------------------------------
            ' Get the number of overwrite if the user buffer is used in "Overwrite the memory"
            '----------------------------------------
            ret = AioGetAiTransferLap(g_id, ai_transfer_lap)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: AioGetAiTransferLap = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Update the overwrite count
            '----------------------------------------
            TextBox_OverwriteCount.Text = ai_transfer_lap.ToString()
            '----------------------------------------
            ' Check overrun error
            '----------------------------------------
            If ((ai_transfer_lap - g_prev_lap >= 2) Or ((ai_transfer_lap - g_prev_lap = 1) And (ai_transfer_count >= g_prev_count))) Then
                TextBox_ReturnCode.Text = String.Format("Event that the specified number of data are transferred Process: Overrun Error!(count = {0:D})", (g_sampling_buffer_size * ai_transfer_lap) + ai_transfer_count)
                Exit Sub
            End If
            '----------------------------------------
            ' Output sampling data to file
            '----------------------------------------
            '--------------------
            ' Open file
            '--------------------
            Try
                fp = File.AppendText(g_file_name)
            Catch ex As Exception
                TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: File.AppendText = file open error."
                Exit Sub
            End Try
            '--------------------
            ' If the transfer position is greater than the previous position
            '--------------------
            If (ai_transfer_count > g_prev_count) Then
                '--------------------
                ' Repeat the process from the previous position to the transfer position
                '--------------------
                For i = g_prev_count To ai_transfer_count - 1
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap))
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i))
                    text_string += vbCrLf
                    '--------------------
                    ' Write data
                    '--------------------
                    Try
                        fp.Write(text_string)
                    Catch ex As Exception
                        TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: fp.Write = file write error."
                        fp.Close()
                        Exit Sub
                    End Try
                Next i
            '--------------------
            ' If the transfer position is smaller than the previous position
            '--------------------
            Else
                '--------------------
                ' Repeat process from previous position to user buffer size
                '--------------------
                For i = g_prev_count To g_sampling_buffer_size - 1
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * g_prev_lap))
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i))
                    text_string += vbCrLf
                    '--------------------
                    ' Write data
                    '--------------------
                    Try
                        fp.Write(text_string)
                    Catch ex As Exception
                        TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: fp.Write = file write error."
                        fp.Close()
                        Exit Sub
                    End Try
                Next i
                '--------------------
                ' Repeat the process from 0 to transfer position
                '--------------------
                For i = 0 To ai_transfer_count - 1
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap))
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i))
                    text_string += vbCrLf
                    '--------------------
                    ' Write data
                    '--------------------
                    Try
                        fp.Write(text_string)
                    Catch ex As Exception
                        TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: fp.Write = file write error."
                        fp.Close()
                        Exit Sub
                    End Try
                Next i
            End If
            '--------------------
            ' Close file
            '--------------------
            fp.Close()
            '----------------------------------------
            ' Update previous position
            '----------------------------------------
            g_prev_count = ai_transfer_count
            g_prev_lap = ai_transfer_lap
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that device operation end
        ' Acquire and display data
        '----------------------------------------
        If (m.Msg = AIOM_AIE_END) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAiStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' Get the number of data that are transferred from the user memory
            '----------------------------------------
            ret = AioGetAiTransferCount(g_id, ai_transfer_count)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAiTransferCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Update the transfer count
            '----------------------------------------
            TextBox_TotalSamplingPosition.Text = ai_transfer_count.ToString()
            '----------------------------------------
            ' Get the number of overwrite if the user buffer is used in "Overwrite the memory"
            '----------------------------------------
            ret = AioGetAiTransferLap(g_id, ai_transfer_lap)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAiTransferLap = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Update the overwrite count
            '----------------------------------------
            TextBox_OverwriteCount.Text = ai_transfer_lap.ToString()
            '----------------------------------------
            ' Check overrun error
            '----------------------------------------
            If ((ai_transfer_lap - g_prev_lap >= 2) Or ((ai_transfer_lap - g_prev_lap = 1) And (ai_transfer_count >= g_prev_count))) Then
                TextBox_ReturnCode.Text = String.Format("Device Operation End Event Process: Overrun Error!(count = {0:D})", (g_sampling_buffer_size * ai_transfer_lap) + ai_transfer_count)
                Exit Sub
            End If
            '----------------------------------------
            ' Output sampling data to file
            '----------------------------------------
            '--------------------
            ' Open file
            '--------------------
            Try
                fp = File.AppendText(g_file_name)
            Catch ex As Exception
                TextBox_ReturnCode.Text = "Device Operation End Event Process: File.AppendText = file open error."
                Exit Sub
            End Try
            '--------------------
            ' If the transfer position is greater than the previous position
            '--------------------
            If (ai_transfer_count > g_prev_count) Then
                '--------------------
                ' Repeat the process from the previous position to the transfer position
                '--------------------
                For i = g_prev_count To ai_transfer_count - 1
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap))
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i))
                    text_string += vbCrLf
                    '--------------------
                    ' Write data
                    '--------------------
                    Try
                        fp.Write(text_string)
                    Catch ex As Exception
                        TextBox_ReturnCode.Text = "Device Operation End Event Process: fp.Write = file write error."
                        fp.Close()
                        Exit Sub
                    End Try
                Next i
            '--------------------
            ' If the transfer position is smaller than the previous position
            '--------------------
            Else
                '--------------------
                ' Repeat process from previous position to user buffer size
                '--------------------
                For i = g_prev_count To g_sampling_buffer_size - 1
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * g_prev_lap))
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i))
                    text_string += vbCrLf
                    '--------------------
                    ' Write data
                    '--------------------
                    Try
                        fp.Write(text_string)
                    Catch ex As Exception
                        TextBox_ReturnCode.Text = "Device Operation End Event Process: fp.Write = file write error."
                        fp.Close()
                        Exit Sub
                    End Try
                Next i
                '--------------------
                ' Repeat the process from 0 to transfer position
                '--------------------
                For i = 0 To ai_transfer_count - 1
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap))
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i))
                    text_string += vbCrLf
                    '--------------------
                    ' Write data
                    '--------------------
                    Try
                        fp.Write(text_string)
                    Catch ex As Exception
                        TextBox_ReturnCode.Text = "Device Operation End Event Process: fp.Write = file write error."
                        fp.Close()
                        Exit Sub
                    End Try
                Next i
            End If
            '--------------------
            ' Close file
            '--------------------
            fp.Close()
            '----------------------------------------
            ' Update previous position
            '----------------------------------------
            g_prev_count = ai_transfer_count
            g_prev_lap = ai_transfer_lap
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Device Operation End Event Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that overflow
        ' Display the status
        '----------------------------------------
        If (m.Msg = AIOM_AIE_OFERR) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Overflow Occurrence Process: AioGetAiStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Overflow Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that sampling clock error
        ' Display the status
        '----------------------------------------
        If (m.Msg = AIOM_AIE_SCERR) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: AioGetAiStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that AD conversion error
        ' Display the status
        '----------------------------------------
        If (m.Msg = AIOM_AIE_ADERR) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "AD Conversion Error Occurrence Process: AioGetAiStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "Operating"
            '--------------------
            ' When status 'Device is running' is OFF
            '--------------------
            Else
                text_string += "Stop"
            End If
            '--------------------
            ' When status 'Wait the start trigger' is ON
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are stored' is ON
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", The specified number of data are stored"
            End If
            '--------------------
            ' When status 'Overflow' is ON
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", Overflow"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'AD conversion error' is ON
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "AD Conversion Error Occurrence Process: Successful completion"
        End If
        MyBase.WndProc(m)
    End Sub
End Class
