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

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_AI_RANGE_NUM As Integer = 32              ' Maximum number of elements in analog input range array
    Public Const DISPLAY_SAMPLING_COUNT As Integer = 1000      ' Maximum sampling count of sampling data displayed in listbox

    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Dim g_id As Short                                                         ' ID for device access
    Dim g_sampling_count As Long                                              ' Total sampling count
    Dim g_sampling_data() As Single = Nothing                                 ' Pointer to sampling data storage area
    Dim gCh As GCHandle                                                       ' GCHandle for callback routine
    Dim gCh_sampling_data As GCHandle                                         ' GCHandle for g_sampling_data array
    Dim pdelegate_func As PAICALLBACK                                         ' Delegate for event notification
    Dim pfunc As IntPtr                                                       ' IntPtr for callback routine
    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 AiSamplingOnceDevCallback_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 number of channels for analog input
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' 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 sampling clock
        '----------------------------------------
        TextBox_SamplingClock.Text = "1000"
        '----------------------------------------
        ' Update the initial display of conversion speed
        '----------------------------------------
        TextBox_ScanClock.Text = "10"
        '----------------------------------------
        ' Update the initial display of sampling stop times
        '----------------------------------------
        TextBox_StopTimes.Text = "1000"
        '----------------------------------------
        ' 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
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
        g_sampling_count = 0
        '----------------------------------------
        ' Initialize the delegate for event notification
        '----------------------------------------
        pdelegate_func = New PAICALLBACK(AddressOf CallBackProc)
        '----------------------------------------
        ' Do not release the delegate
        '----------------------------------------
        If (gCh.IsAllocated = False) Then
            gCh = GCHandle.Alloc(pdelegate_func)
        End If
    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 AioSetAiSamplingClock button is clicked
    ' Set the internal clock conversion speed used for sampling
    '================================================================================
    Private Sub Button_SamplingClock_Click(sender As Object, e As EventArgs) Handles Button_SamplingClock.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim sampling_clock As Single                      ' Internal clock conversion speed

        '----------------------------------------
        ' Get the internal clock conversion speed from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_SamplingClock.Text, sampling_clock) = False) Then
            sampling_clock = 1000
        End If
        '----------------------------------------
        ' Set the internal clock conversion speed
        '----------------------------------------
        ret = AioSetAiSamplingClock(g_id, sampling_clock)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiSamplingClock = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSetAiScanClock button is clicked
    ' Set the channel conversion speed
    '================================================================================
    Private Sub Button_ScanClock_Click(sender As Object, e As EventArgs) Handles Button_ScanClock.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim scan_clock As Single                          ' Conversion speed

        '----------------------------------------
        ' Get the conversion speed from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_ScanClock.Text, scan_clock) = False) Then
            scan_clock = 10
        End If
        '----------------------------------------
        ' Set the conversion speed
        '----------------------------------------
        ret = AioSetAiScanClock(g_id, scan_clock)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiScanClock = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the AioSetAiStopTimes button is clicked
    ' Set the sampling stop times
    '================================================================================
    Private Sub Button_StopTimes_Click(sender As Object, e As EventArgs) Handles Button_StopTimes.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim stop_times As Integer                         ' Sampling stop times

        '----------------------------------------
        ' Get the sampling stop times from textbox
        '----------------------------------------
        If (Integer.TryParse(TextBox_StopTimes.Text, stop_times) = False) Then
            stop_times = 1000
        End If
        '----------------------------------------
        ' Set the sampling stop times
        '----------------------------------------
        ret = AioSetAiStopTimes(g_id, stop_times)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiStopTimes = " & ret & " : " & error_string.ToString()
    End Sub

    '================================================================================
    ' It is the process when the Basic Settings button is clicked
    ' Performs the basic settings required for sampling
    ' (Basic setting must be performed after initialization processing/reset processing and before starting the first sampling)
    '================================================================================
    Private Sub Button_SmpBasicSettings_Click(sender As Object, e As EventArgs) Handles Button_SmpBasicSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Set the analog input conversion data transfer mode to device buffer mode
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiTransferMode(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Basic Settings: AioSetAiTransferMode = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the data storage memory type to FIFO
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiMemoryType(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Basic Settings: AioSetAiMemoryType = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the sampling start condition to Software
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiStartTrigger(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Basic Settings: AioSetAiStartTrigger = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the clock type to Internal Clock
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiClockType(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Basic Settings: AioSetAiClockType = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the sampling stop condition to Stop conversion by the specified times
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiStopTrigger(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Basic Settings: AioSetAiStopTrigger = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the fixed pointer of the delegate for event notification
        '----------------------------------------
        pfunc = Marshal.GetFunctionPointerForDelegate(pdelegate_func)
        '----------------------------------------
        ' Set the callback routine: 'Event that device operation end', '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 = AioSetAiCallBackProc(g_id, pfunc, AIE_END Or AIE_OFERR Or AIE_SCERR Or AIE_ADERR, Nothing)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Basic Settings: AioSetAiCallBackProc = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' In the Z series, the bus master buffer may be limited to a maximum of 1MByte or less.
        ' In the real system, implement buffer size change processing as necessary.
        '----------------------------------------
        'ret = AioSetAiMemorySize(Id, 750 * 1024)
        '----------------------------------------
        ' Display the message that basic setting is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Basic Settings: Successful completion"
    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 memory
        '----------------------------------------
        ret = AioResetAiMemory(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sampling Start: AioResetAiMemory = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' 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 total sampling count
        '----------------------------------------
        g_sampling_count = 0
        '----------------------------------------
        ' 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 sampling count
        '----------------------------------------
        TextBox_SamplingCount.Text = "0"
        '----------------------------------------
        ' Clear the listbox for sampling data display
        '----------------------------------------
        ListBox_SamplingData.Items.Clear()
        '----------------------------------------
        ' Display the message that sampling start is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Sampling Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioGetAiSamplingCount button is clicked
    ' Get the sampling count and display it on the dialog
    '================================================================================
    Private Sub Button_GetSamplingCount_Click(sender As Object, e As EventArgs) Handles Button_GetSamplingCount.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ai_sampling_count As Integer                  ' Sampling count

        '----------------------------------------
        ' Get sampling count
        '----------------------------------------
        ret = AioGetAiSamplingCount(g_id, ai_sampling_count)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiSamplingCount = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the sampling count acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' Update the sampling count
            '--------------------
            TextBox_SamplingCount.Text = ai_sampling_count.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 text_string As String                         ' For updating the textbox
        Dim ret As Integer                                ' For return code
        Dim ai_sampling_count As Integer                  ' Sampling count
        Dim ai_status As Integer                          ' Status
        Dim i, j As Long                                  ' Loop count
        Dim loop_max_count                                ' Maximum loop count
        Dim top_address As Long                           ' Top address of buffer for sampling data display
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' 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
        '----------------------------------------
        ' 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 Stop: 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 current sampling count
        '----------------------------------------
        ret = AioGetAiSamplingCount(g_id, ai_sampling_count)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sampling Stop: AioGetAiSamplingCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display sampling count
        '----------------------------------------
        TextBox_SamplingCount.Text = ai_sampling_count.ToString()
        '----------------------------------------
        ' When the sampling count of the data acquired this time is 1 or more
        '----------------------------------------
        If (ai_sampling_count > 0) Then
            '----------------------------------------
            ' Get the number of channels
            '----------------------------------------
            ret = AioGetAiChannels(g_id, ai_channels)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Sampling Stop: AioGetAiChannels = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Get sampling data process
            '----------------------------------------
            '--------------------
            ' Allocate an array for storing data
            '--------------------
            If IsArray(g_sampling_data) = True Then
                '----------------------------------------
                ' GCHandle free
                '----------------------------------------
                If (gCh_sampling_data.IsAllocated = True) Then
                     gCh_sampling_data.Free()
                End If
                Erase g_sampling_data
            End If
            Try
                ReDim g_sampling_data(ai_sampling_count * ai_channels - 1)
            Catch ex As Exception
                TextBox_ReturnCode.Text = "Sampling Stop: Failed to allocate an array for storing data"
                Exit Sub
            End Try
            '----------------------------------------
            ' Specify Pinned handle to g_sampling_data
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = False) Then
                gCh_sampling_data = GCHandle.Alloc(g_sampling_data, GCHandleType.Pinned)
            End If
            '----------------------------------------
            ' Get sampling data
            '----------------------------------------
            ret = AioGetAiSamplingDataEx(g_id, ai_sampling_count, g_sampling_data)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Sampling Stop: AioGetAiSamplingDataEx = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Update the total sampling count
            '----------------------------------------
            g_sampling_count = ai_sampling_count
            '----------------------------------------
            ' Set the loop count for the sampling data display loop
            '----------------------------------------
            '--------------------
            ' When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
            '--------------------
            If (ai_sampling_count > DISPLAY_SAMPLING_COUNT) Then
                '--------------------
                ' Limit the maximum number of loops to the data display maximum sampling count
                '--------------------
                loop_max_count = DISPLAY_SAMPLING_COUNT
                '--------------------
                ' Set the top address of the buffer to be referenced to the address 
                ' before the maximum sampling count for data display from the end of the acquired data
                '--------------------
                top_address = ai_sampling_count - DISPLAY_SAMPLING_COUNT
            '--------------------
            ' When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
            '--------------------
            Else
                '--------------------
                ' Set the number of total sampling data to the maximum loop number
                '--------------------
                loop_max_count = ai_sampling_count
                '--------------------
                ' Set the top address of the referenced buffer to 0
                '--------------------
                top_address = 0
            End If
            '----------------------------------------
            ' Display sampling data
            ' (Display from the end of acquired data to the maximum number of data display sampling count)
            '----------------------------------------
            '--------------------
            ' Clear the listbox for sampling data display
            '--------------------
            ListBox_SamplingData.Items.Clear()
            '--------------------
            ' Repeat the process up to the maximum sampling count of the display data
            '--------------------
            For i = top_address To loop_max_count + top_address - 1
                '--------------------
                ' Clear display string storage variable
                '--------------------
                text_string = ""
                '--------------------
                ' Generate display string
                '--------------------
                text_string += String.Format("{0,5},  ", (i + 1))
                '--------------------
                ' Repeat the process for the specified number of channels
                '--------------------
                For j = 0 To ai_channels - 1
                    text_string += String.Format(" {0,10:F5}", g_sampling_data(i * ai_channels + j))
                Next j
                '----------------------------------------
                ' Display sampling start data
                '----------------------------------------
                ListBox_SamplingData.Items.Add(text_string)
            Next i
            '----------------------------------------
            ' Display the message that sampling stop is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Sampling Stop: Successful completion"
        '----------------------------------------
        ' When the sampling count of the data acquired this time is 0 or less
        '----------------------------------------
        Else
            '----------------------------------------
            ' Display the message that sampling stop is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Sampling Stop: Successful completion"
        End If
    End Sub

    '================================================================================
    ' It is the process when the Output Acquired Data to File button is clicked
    ' Output the globally held sampling data to a CSV file
    '================================================================================
    Private Sub Button_OutputSamplingData_Click(sender As Object, e As EventArgs) Handles Button_OutputSamplingData.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 file_name As String = "SamplingData.csv"      ' Sampling data output file name
        Dim fp As StreamWriter                            ' File pointer
        Dim i, j As Long                                  ' Loop count
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Open file
        '----------------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "Output Acquired Data to File: File.CreateText = file open error."
            Exit Sub
        End Try
        '----------------------------------------
        ' Get the number of channels
        '----------------------------------------
        ret = AioGetAiChannels(g_id, ai_channels)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Output Acquired Data to File: AioGetAiChannels = " & ret & " : " & error_string.ToString()
            '----------------------------------------
            ' Close file
            '----------------------------------------
            fp.Close()
            Exit Sub
        End If
        '----------------------------------------
        ' Create sampling data for file output
        '----------------------------------------
        '--------------------
        ' If the total sampling count is 1 or more and pointer to sampling data storage area is not NULL, output the file
        '--------------------
        If ((g_sampling_count > 0) And (IsArray(g_sampling_data) = True)) Then
            '--------------------
            ' Repeat the process up to the total sampling count
            '--------------------
            For i = 0 To g_sampling_count - 1
                text_string = ""
                '--------------------
                ' Generate display string
                '--------------------
                text_string += String.Format("{0,5},", (i + 1))
                '--------------------
                ' Repeat the process for the specified number of channels
                '--------------------
                For j = 0 To ai_channels - 1
                    text_string += String.Format(" {0,10:F5}", g_sampling_data(i * ai_channels + j))
                    '--------------------
                    ' Add newline character for the last channel
                    '--------------------
                    If (j = (ai_channels - 1)) Then
                        text_string += vbCrLf
                    '--------------------
                    ' Add comma for channels other than the last channel
                    '--------------------
                    Else
                        text_string += ","
                    End If
                Next j
                '----------------------------------------
                ' Write data
                '----------------------------------------
                Try
                    fp.Write(text_string)
                Catch ex As Exception
                    TextBox_ReturnCode.Text = "Output Acquired Data to File: fp.Write = file write error."
                    fp.Close()
                    Exit Sub
                End Try
            Next i
        End If
        '----------------------------------------
        ' Close file
        '----------------------------------------
        fp.Close()
        '----------------------------------------
        ' Display the message that file output is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Output Acquired Data to File: Save data to " & Chr(34) & file_name & Chr(34) & "."
    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

        '----------------------------------------
        ' Release the array for storing AD conversion data
        '----------------------------------------
        If IsArray(g_sampling_data) = True Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = True) Then
                 gCh_sampling_data.Free()
            End If
            Erase g_sampling_data
        End If
        '----------------------------------------
        ' Release the garbage collection handle of the delegate for event notification
        '----------------------------------------
        If (gCh.IsAllocated = True) Then
            gCh.Free()
        End If
        '----------------------------------------
        ' 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
        '----------------------------------------
        ' Release the array for storing AD conversion data
        '----------------------------------------
        If IsArray(g_sampling_data) = True Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = True) Then
                 gCh_sampling_data.Free()
            End If
            Erase g_sampling_data
        End If
        '----------------------------------------
        ' Release the garbage collection handle of the delegate for event notification
        '----------------------------------------
        If (gCh.IsAllocated = True) Then
            gCh.Free()
        End If
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' Callback routine
    '================================================================================
    Protected Sub CallBackProc(ByVal Id As Short, ByVal Message As Short, ByVal wParam As Integer, ByVal lParam As Integer, ByVal Param As IntPtr)
        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_sampling_count As Integer                  ' Sampling count
        Dim ai_status As Integer                          ' Status
        Dim i, j As Long                                  ' Loop count
        Dim loop_max_count                                ' Maximum loop count
        Dim top_address As Long                           ' Top address of buffer for sampling data display
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Event that device operation end
        ' Acquire and display data
        '----------------------------------------
        If (Message = 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
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' Get the current sampling count
            '----------------------------------------
            ret = AioGetAiSamplingCount(g_id, ai_sampling_count)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAiSamplingCount = " & ret & " : " & error_string.ToString()))
                Exit Sub
            End If
            '----------------------------------------
            ' Display sampling count
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_SamplingCount.Text = ai_sampling_count.ToString()))
            '----------------------------------------
            ' Get the number of channels
            '----------------------------------------
            ret = AioGetAiChannels(g_id, ai_channels)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAiChannels = " & ret & " : " & error_string.ToString()))
                Exit Sub
            End If
            '----------------------------------------
            ' Get sampling data process
            '----------------------------------------
            '--------------------
            ' Allocate an array for storing data
            '--------------------
            If IsArray(g_sampling_data) = True Then
                '----------------------------------------
                ' GCHandle free
                '----------------------------------------
                If (gCh_sampling_data.IsAllocated = True) Then
                     gCh_sampling_data.Free()
                End If
                Erase g_sampling_data
            End If
            Try
                ReDim g_sampling_data(ai_sampling_count * ai_channels - 1)
            Catch ex As Exception
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Device Operation End Event Process: Failed to allocate an array for storing data"))
                Exit Sub
            End Try
            '----------------------------------------
            ' Specify Pinned handle to g_sampling_data
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = False) Then
                gCh_sampling_data = GCHandle.Alloc(g_sampling_data, GCHandleType.Pinned)
            End If
            '----------------------------------------
            ' Get sampling data
            '----------------------------------------
            ret = AioGetAiSamplingDataEx(g_id, ai_sampling_count, g_sampling_data)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAiSamplingDataEx = " & ret & " : " & error_string.ToString()))
                Exit Sub
            End If
            '----------------------------------------
            ' Update the total sampling count
            '----------------------------------------
            g_sampling_count = ai_sampling_count
            '----------------------------------------
            ' Set the loop count for the sampling data display loop
            '----------------------------------------
            '--------------------
            ' When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
            '--------------------
            If (ai_sampling_count > DISPLAY_SAMPLING_COUNT) Then
                '--------------------
                ' Limit the maximum number of loops to the data display maximum sampling count
                '--------------------
                loop_max_count = DISPLAY_SAMPLING_COUNT
                '--------------------
                ' Set the top address of the buffer to be referenced to the address 
                ' before the maximum sampling count for data display from the end of the acquired data
                '--------------------
                top_address = ai_sampling_count - DISPLAY_SAMPLING_COUNT
            '--------------------
            ' When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
            '--------------------
            Else
                '--------------------
                ' Set the number of total sampling data to the maximum loop number
                '--------------------
                loop_max_count = ai_sampling_count
                '--------------------
                ' Set the top address of the referenced buffer to 0
                '--------------------
                top_address = 0
            End If
            '----------------------------------------
            ' Display sampling data
            ' (Display from the end of acquired data to the maximum number of data display sampling count)
            '----------------------------------------
            '--------------------
            ' When the sampling count of the data acquired this time is 1 or more
            '--------------------
            If (ai_sampling_count > 0) Then
                '--------------------
                ' Clear the listbox for sampling data display
                '--------------------
                Me.Invoke(New Action(Sub() ListBox_SamplingData.Items.Clear()))
                '--------------------
                ' Repeat the process up to the maximum sampling count of the display data
                '--------------------
                For i = top_address To loop_max_count + top_address - 1
                    '--------------------
                    ' Clear display string storage variable
                    '--------------------
                    text_string = ""
                    '--------------------
                    ' Generate display string
                    '--------------------
                    text_string += String.Format("{0,5},  ", (i + 1))
                    '--------------------
                    ' Repeat the process for the specified number of channels
                    '--------------------
                    For j = 0 To ai_channels - 1
                        text_string += String.Format(" {0,10:F5}", g_sampling_data(i * ai_channels + j))
                    Next j
                    '----------------------------------------
                    ' Display sampling start data
                    '----------------------------------------
                    Me.Invoke(New Action(Sub() ListBox_SamplingData.Items.Add(text_string)))
                Next i
            End If
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Device Operation End Event Process: Successful completion"))
        End If
        '----------------------------------------
        ' Event that overflow
        ' Display the status
        '----------------------------------------
        If (Message = 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
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Overflow Occurrence Process: Successful completion"))
        End If
        '----------------------------------------
        ' Event that sampling clock error
        ' Display the status
        '----------------------------------------
        If (Message = 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
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion"))
        End If
        '----------------------------------------
        ' Event that AD conversion error
        ' Display the status
        '----------------------------------------
        If (Message = 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
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "AD Conversion Error Occurrence Process: Successful completion"))
        End If
        Exit Sub
    End Sub
End Class
