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

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_AO_RANGE_NUM As Integer = 31            ' Maximum number of elements in analog output range array
    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_file_name As String = "GeneratingData.csv"                          ' Generating data output file name
    Dim g_generating_count As Long                                            ' Total generating count
    Dim g_generating_data() As Single = Nothing                               ' Output data
    Dim gCh_generating_data As GCHandle                                       ' GCHandle for g_generating_data array
    Dim g_ao_range() As Short = New Short(MAX_AO_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
                                                                 P1TO5}       ' 1 ~ 5V
    Dim g_sampling_count As Long                                              ' Total sampling count
    Dim g_sampling_data() As Single = Nothing                                 ' Pointer to sampling data storage area
    Dim gCh_sampling_data As GCHandle                                         ' GCHandle for g_sampling_data 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 InternalSyncGeneratingSampling_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 output
        '----------------------------------------
        TextBox_AoChNum.Text = "1"
        '----------------------------------------
        ' Update the initial display of generating clock
        '----------------------------------------
        TextBox_AoGeneratingClock.Text = "1000"
        '----------------------------------------
        ' Update the initial display of output data generating times
        '----------------------------------------
        TextBox_AoGeneratingTimes.Text = "1000"
        '----------------------------------------
        ' Create the combobox for analog output range
        '----------------------------------------
        ComboBox_AoRangeValue.Items.Add("-10 - +10V")
        ComboBox_AoRangeValue.Items.Add("-5 - +5V")
        ComboBox_AoRangeValue.Items.Add("-2.5 - +2.5V")
        ComboBox_AoRangeValue.Items.Add("-1.25 - +1.25V")
        ComboBox_AoRangeValue.Items.Add("-1 - +1V")
        ComboBox_AoRangeValue.Items.Add("-0.625 - +0.625V")
        ComboBox_AoRangeValue.Items.Add("-0.5 - +0.5V")
        ComboBox_AoRangeValue.Items.Add("-0.3125 - +0.3125V")
        ComboBox_AoRangeValue.Items.Add("-0.25 - +0.25V")
        ComboBox_AoRangeValue.Items.Add("-0.125 - +0.125V")
        ComboBox_AoRangeValue.Items.Add("-0.1 - +0.1V")
        ComboBox_AoRangeValue.Items.Add("-0.05 - +0.05V")
        ComboBox_AoRangeValue.Items.Add("-0.025 - +0.025V")
        ComboBox_AoRangeValue.Items.Add("-0.0125 - +0.0125V")
        ComboBox_AoRangeValue.Items.Add("-0.01 - +0.01V")
        ComboBox_AoRangeValue.Items.Add("0 - +10V")
        ComboBox_AoRangeValue.Items.Add("0 - +5V")
        ComboBox_AoRangeValue.Items.Add("0 - +4.095V")
        ComboBox_AoRangeValue.Items.Add("0 - +2.5V")
        ComboBox_AoRangeValue.Items.Add("0 - +1.25V")
        ComboBox_AoRangeValue.Items.Add("0 - +1V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.5V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.25V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.1V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.05V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.025V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.0125V")
        ComboBox_AoRangeValue.Items.Add("0 - +0.01V")
        ComboBox_AoRangeValue.Items.Add("0 - +20mA")
        ComboBox_AoRangeValue.Items.Add("4 - +20mA")
        ComboBox_AoRangeValue.Items.Add("1 - +5V")
        '----------------------------------------
        ' Change the selection of analog output range combobox to 0
        '----------------------------------------
        ComboBox_AoRangeValue.SelectedIndex = 0
        '----------------------------------------
        ' Update the initial display of number of channels for analog input
        '----------------------------------------
        TextBox_AiChNum.Text = "1"
        '----------------------------------------
        ' Create the combobox for analog input method
        '----------------------------------------
        ComboBox_AiInpMethod.Items.Add("Single-end input")
        ComboBox_AiInpMethod.Items.Add("Differential input")
        '----------------------------------------
        ' Change the selection of analog input method combobox to 0
        '----------------------------------------
        ComboBox_AiInpMethod.SelectedIndex = 0
        '----------------------------------------
        ' Create the combobox for analog input range
        '----------------------------------------
        ComboBox_AiRangeValue.Items.Add("-10 - +10V")
        ComboBox_AiRangeValue.Items.Add("-5 - +5V")
        ComboBox_AiRangeValue.Items.Add("-2.5 - +2.5V")
        ComboBox_AiRangeValue.Items.Add("-1.25 - +1.25V")
        ComboBox_AiRangeValue.Items.Add("-1 - +1V")
        ComboBox_AiRangeValue.Items.Add("-0.625 - +0.625V")
        ComboBox_AiRangeValue.Items.Add("-0.5 - +0.5V")
        ComboBox_AiRangeValue.Items.Add("-0.3125 - +0.3125V")
        ComboBox_AiRangeValue.Items.Add("-0.25 - +0.25V")
        ComboBox_AiRangeValue.Items.Add("-0.125 - +0.125V")
        ComboBox_AiRangeValue.Items.Add("-0.1 - +0.1V")
        ComboBox_AiRangeValue.Items.Add("-0.05 - +0.05V")
        ComboBox_AiRangeValue.Items.Add("-0.025 - +0.025V")
        ComboBox_AiRangeValue.Items.Add("-0.0125 - +0.0125V")
        ComboBox_AiRangeValue.Items.Add("-0.01 - +0.01V")
        ComboBox_AiRangeValue.Items.Add("0 - +10V")
        ComboBox_AiRangeValue.Items.Add("0 - +5V")
        ComboBox_AiRangeValue.Items.Add("0 - +4.095V")
        ComboBox_AiRangeValue.Items.Add("0 - +2.5V")
        ComboBox_AiRangeValue.Items.Add("0 - +1.25V")
        ComboBox_AiRangeValue.Items.Add("0 - +1V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.5V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.25V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.1V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.05V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.025V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.0125V")
        ComboBox_AiRangeValue.Items.Add("0 - +0.01V")
        ComboBox_AiRangeValue.Items.Add("0 - +20mA")
        ComboBox_AiRangeValue.Items.Add("4 - +20mA")
        ComboBox_AiRangeValue.Items.Add("-20mA - +20mA")
        ComboBox_AiRangeValue.Items.Add("1 - +5V")
        '----------------------------------------
        ' Change the selection of analog input range combobox to 0
        '----------------------------------------
        ComboBox_AiRangeValue.SelectedIndex = 0
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 0
        g_generating_count = 0
        g_sampling_count = 0
    End Sub

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

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

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

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

    '================================================================================
    ' It is the process when the Analog I/O Set button is clicked
    ' Set the analog I/O settings
    '================================================================================
    Private Sub Button_AiAoSetting_Click(sender As Object, e As EventArgs) Handles Button_AiAoSetting.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
        Dim input_method As Short                         ' Input method

        '----------------------------------------
        ' Update GUI information
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' Get the index of analog input range setting value from combobox
        '----------------------------------------
        index = ComboBox_AiRangeValue.SelectedIndex
        '----------------------------------------
        ' Set the analog input range (all channels)
        '----------------------------------------
        ret = AioSetAiRangeAll(g_id, g_ai_range(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Analog I/O Set: AioSetAiRangeAll = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the index of input method setting value from combobox
        '----------------------------------------
        input_method = ComboBox_AiInpMethod.SelectedIndex
        '----------------------------------------
        ' Set the analog input method
        '----------------------------------------
        ret = AioSetAiInputMethod(g_id, input_method)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Analog I/O Set: AioSetAiInputMethod = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the index of analog output range setting value from combobox
        '----------------------------------------
        index = ComboBox_AoRangeValue.SelectedIndex
        '----------------------------------------
        ' Set the analog output range (all channels)
        '----------------------------------------
        ret = AioSetAoRangeAll(g_id, g_ao_range(index))
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Analog I/O Set: AioSetAoRangeAll = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that analog input/output settings is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Analog I/O Set: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Generating/Sampling Set button is clicked
    ' Set the generating/sampling settings
    '================================================================================
    Private Sub Button_GeneratingSamplingSetting_Click(sender As Object, e As EventArgs) Handles Button_GeneratingSamplingSetting.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 analog input channels
        Dim ao_channels As Short                          ' Number of analog output channels
        Dim generating_clock As Single                    ' Internal clock conversion speed

        '----------------------------------------
        ' Get the number of analog input channels from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_AiChNum.Text, ai_channels) = False) Then
            ai_channels = 1
        End If
        '----------------------------------------
        ' Set the number of analog input channels used for conversion
        '----------------------------------------
        ret = AioSetAiChannels(g_id, ai_channels)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAiChannels = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' 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 = "Generating/Sampling Set: 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 = "Generating/Sampling Set: AioSetAiMemoryType = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the sampling start condition to Event controller output
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiStartTrigger(g_id, 10)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAiStartTrigger = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the clock type to Event controller output
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiClockType(g_id, 10)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAiClockType = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the sampling stop condition to Event controller output
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAiStopTrigger(g_id, 10)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAiStopTrigger = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the event factor to 'Event that AD conversion start', '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 = AioSetAiEvent(g_id, Handle, AIE_START Or AIE_END 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 = "Generating/Sampling Set: AioSetAiEvent = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the number of analog output channels from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_AoChNum.Text, ao_channels) = False) Then
            ao_channels = 1
        End If
        '----------------------------------------
        ' Set the number of analog output channels used for conversion
        '----------------------------------------
        ret = AioSetAoChannels(g_id, ao_channels)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoChannels = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the analog output conversion data transfer mode to device buffer mode
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAoTransferMode(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoTransferMode = " & 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 = AioSetAoMemoryType(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoMemoryType = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the generating start condition to Software
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAoStartTrigger(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoStartTrigger = " & 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 = AioSetAoClockType(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoClockType = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get the internal clock conversion speed from textbox
        '----------------------------------------
        If (Single.TryParse(TextBox_AoGeneratingClock.Text, generating_clock) = False) Then
            generating_clock = 1000
        End If
        '----------------------------------------
        ' Set the internal clock conversion speed
        '----------------------------------------
        ret = AioSetAoSamplingClock(g_id, generating_clock)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoSamplingClock = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the generating stop condition to Stop conversion by the specified times
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAoStopTrigger(g_id, 0)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoStopTrigger = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Set the event factor to 'Event that device operation end', 
        ' 'Event that sampling clock error', and 'Event that DA conversion error'.
        ' Caution for modify this function (if modified, the application may not work)
        '----------------------------------------
        ret = AioSetAoEvent(g_id, Handle, AOE_END Or AOE_SCERR Or AOE_DAERR)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Set: AioSetAoEvent = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that generating/sampling settings is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Generating/Sampling Set: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Sync Operation Set button is clicked
    ' Set the synchronous operation settings
    '================================================================================
    Private Sub Button_SyncSetting_Click(sender As Object, e As EventArgs) Handles Button_SyncSetting.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Event Controller Setting: Connect Analog output internal clock signal to Analog input sampling clock
        '----------------------------------------
        ret = AioSetEcuSignal(g_id, AIOECU_DEST_AI_CLK, AIOECU_SRC_AO_CLK)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sync Operation Set: AioSetEcuSignal = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Event Controller Setting: Connect Ao software start signal to Analog input converting start signal
        '----------------------------------------
        ret = AioSetEcuSignal(g_id, AIOECU_DEST_AI_START, AIOECU_SRC_START)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sync Operation Set: AioSetEcuSignal = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Event Controller Setting: Connect Analog output signal that data of the specified sampling times have been output (FIFO) to Analog input converting stop signal
        '----------------------------------------
        ret = AioSetEcuSignal(g_id, AIOECU_DEST_AI_STOP, AIOECU_SRC_AO_STOP_FIFO)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Sync Operation Set: AioSetEcuSignal = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that synchronous operation settings is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Sync Operation Set: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Output Data Set button is clicked
    ' Set the output data
    '================================================================================
    Private Sub Button_OutputDataSettings_Click(sender As Object, e As EventArgs) Handles Button_OutputDataSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim fp As StreamWriter                            ' File pointer
        Dim i, j As Long                                  ' Loop count
        Dim ret As Integer                                ' For return code
        Dim generating_times As Integer                   ' Generating times of output data
        Dim ao_channels As Short                          ' Number of channels
        Dim ao_range As Short                             ' Range
        Dim gain, offset As Single                        ' Coefficient of sine waveform

        '----------------------------------------
        ' Get the number of channels
        '----------------------------------------
        ret = AioGetAoChannels(g_id, ao_channels)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Output Data Set: AioGetAoChannels = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get range (channel 0 only)
        '----------------------------------------
        ret = AioGetAoRange(g_id, 0, ao_range)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Output Data Set: AioGetAoRange = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get generating times from textbox
        '----------------------------------------
        If (Integer.TryParse(TextBox_AoGeneratingTimes.Text, generating_times) = False) Then
            generating_times = 1000
        End If
        '----------------------------------------
        ' Output data process
        '----------------------------------------
        '--------------------
        ' Allocate an array for storing data
        '--------------------
        If (IsArray(g_generating_data) = True) Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh_generating_data.IsAllocated = True) Then
                 gCh_generating_data.Free()
            End If
            Erase g_generating_data
        End If
        Try
            ReDim g_generating_data(generating_times * ao_channels - 1)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "Output Data Set: Failed to allocate an array for storing data"
            Exit Sub
        End Try
        '----------------------------------------
        ' Specify Pinned handle to g_generating_data
        '----------------------------------------
        If (gCh_generating_data.IsAllocated = False) Then
            gCh_generating_data = GCHandle.Alloc(g_generating_data, GCHandleType.Pinned)
        End If
        '----------------------------------------
        '  Create analog output data: sine waveform
        '----------------------------------------
        Select Case ao_range
            Case PM10
                gain = 10
                offset = 0
            Case PM5
                gain = 5
                offset = 0
            Case PM25
                gain = 2.5F
                offset = 0
            Case PM125
                gain = 1.25F
                offset = 0
            Case PM1
                gain = 1
                offset = 0
            Case PM0625
                gain = 0.625F
                offset = 0
            Case PM05
                gain = 0.5F
                offset = 0
            Case PM03125
                gain = 0.3125F
                offset = 0
            Case PM025
                gain = 0.25F
                offset = 0
            Case PM0125
                gain = 0.125F
                offset = 0
            Case PM01
                gain = 0.1F
                offset = 0
            Case PM005
                gain = 0.05F
                offset = 0
            Case PM0025
                gain = 0.025F
                offset = 0
            Case PM00125
                gain = 0.0125F
                offset = 0
            Case PM001
                gain = 0.01F
                offset = 0
            Case P10
                gain = 5
                offset = 5
            Case P5
                gain = 2.5F
                offset = 2.5F
            Case P4095
                gain = 2.0475F
                offset = 2.0475F
            Case P25
                gain = 1.25F
                offset = 1.25F
            Case P125
                gain = 0.625F
                offset = 0.625F
            Case P1
                gain = 0.5F
                offset = 0.5F
            Case P05
                gain = 0.25F
                offset = 0.25F
            Case P025
                gain = 0.125F
                offset = 0.125F
            Case P01
                gain = 0.05F
                offset = 0.05F
            Case P005
                gain = 0.025F
                offset = 0.025F
            Case P0025
                gain = 0.0125F
                offset = 0.0125F
            Case P00125
                gain = 0.00625F
                offset = 0.00625F
            Case P001
                gain = 0.005F
                offset = 0.005F
            Case P20MA
                gain = 10
                offset = 10
            Case P4TO20MA
                gain = 8
                offset = 12
            Case P1TO5
                gain = 2
                offset = 3
            Case Else
                gain = 10
                offset = 0
        End Select
        For i = 0 To generating_times - 1
            For j = 0 To ao_channels - 1
                g_generating_data(i * ao_channels + j) = gain * Math.Sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * (j Mod 4)) + offset
            Next j
        Next i
        '----------------------------------------
        ' Set analog output data
        '----------------------------------------
        ret = AioSetAoSamplingDataEx(g_id, generating_times, g_generating_data)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Output Data Set: AioSetAoSamplingDataEx = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Open file
        '----------------------------------------
        Try
            fp = File.CreateText(g_file_name)
        Catch
            TextBox_ReturnCode.Text = "Output Data Set: File.CreateText = file open error."
            Exit Sub
        End Try
        '----------------------------------------
        ' Open file
        '----------------------------------------
        For i = 0 To generating_times - 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 ao_channels - 1
                text_string += String.Format(" {0,10:F5}", g_generating_data(i * ao_channels + j))
                '--------------------
                ' Add newline character for the last channel
                '--------------------
                If (j = ao_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 Data Set: fp.Write = file write error."
                fp.Close()
                Exit Sub
            End Try
        Next i
        '----------------------------------------
        ' Close file
        '----------------------------------------
        fp.Close()
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "Output Data Set: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Generating/Sampling Start button is clicked
    ' After clearing the memory and status, start generating/sampling.
    '================================================================================
    Private Sub Button_GeneratingSamplingStart_Click(sender As Object, e As EventArgs) Handles Button_GeneratingSamplingStart.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ai_status As Integer                          ' Analog input status
        Dim ao_status As Integer                          ' Analog output status
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Clear memory
        '----------------------------------------
        ret = AioResetAiMemory(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/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 = "Generating/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 = "Generating/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 = "Generating/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_AiStatus.Text = text_string
        '----------------------------------------
        ' Update the sampling count
        '----------------------------------------
        TextBox_AiSamplingCount.Text = "0"
        '----------------------------------------
        ' Clear the listbox for sampling data display
        '----------------------------------------
        ListBox_AiSamplingData.Items.Clear()
        '----------------------------------------
        ' Reset the analog output status
        '----------------------------------------
        ret = AioResetAoStatus(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Start: AioResetAoStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Initialize the total generating count
        '----------------------------------------
        g_generating_count = 0
        '----------------------------------------
        ' Start generating
        '----------------------------------------
        ret = AioStartAo(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        '--------------------
        ' Exit the process if not completed successfully
        '--------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Start: AioStartAo = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Get status
        '----------------------------------------
        ret = AioGetAoStatus(g_id, ao_status)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Start: AioGetAoStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display status
        '----------------------------------------
        text_string = String.Format("{0:x8}H (", ao_status)
        '--------------------
        ' When status 'Device is running' is ON
        '--------------------
        If ((ao_status And AOS_BUSY) = AOS_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 ((ao_status And AOS_START_TRG) = AOS_START_TRG) Then
            text_string += ", Wait the start trigger"
        End If
        '--------------------
        ' When status 'The specified number of data are output' is ON
        '--------------------
        If ((ao_status And AOS_DATA_NUM) = AOS_DATA_NUM) Then
            text_string += ", The specified number of data are output"
        End If
        '--------------------
        ' When status 'Sampling clock error' is ON
        '--------------------
        If ((ao_status And AOS_SCERR) = AOS_SCERR) Then
            text_string += ", Sampling clock error"
        End If
        '--------------------
        ' When status 'DA conversion error' is ON
        '--------------------
        If ((ao_status And AOS_DAERR) = AOS_DAERR) Then
            text_string += ", DA conversion error"
        End If
        '--------------------
        ' When status 'Driver spec error' is ON
        '--------------------
        If ((ao_status And AOS_DRVERR) = AOS_DRVERR) Then
            text_string += ", Driver spec error"
        End If
        text_string += ")"
        TextBox_AoStatus.Text = text_string
        '----------------------------------------
        ' Update generating count
        '----------------------------------------
        TextBox_AoGeneratingCount.Text = "0"
        '----------------------------------------
        ' Display the message that generating/sampling start is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Generating/Sampling Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the Get Status button is clicked
    ' Get the count and status of generating/sampling and display it on the dialog
    '================================================================================
    Private Sub Button_GetStatus_Click(sender As Object, e As EventArgs) Handles Button_GetStatus.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ai_sampling_count As Integer                  ' Sampling count
        Dim ai_status As Integer                          ' Sampling status
        Dim ao_generating_count As Integer                ' Generating count
        Dim ao_status As Integer                          ' Generating status
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Get the 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 = "Get Status: AioGetAiSamplingCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Update the sampling count
        '----------------------------------------
        TextBox_AiSamplingCount.Text = ai_sampling_count.ToString()
        '----------------------------------------
        ' 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 = "Get Status: 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_AiStatus.Text = text_string
        '----------------------------------------
        ' Get generating count
        '----------------------------------------
        ret = AioGetAoSamplingCount(g_id, ao_generating_count)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Get Status: AioGetAoSamplingCount = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Update generating count
        '----------------------------------------
        TextBox_AoGeneratingCount.Text = ao_generating_count.ToString()
        '----------------------------------------
        ' Get status
        '----------------------------------------
        ret = AioGetAoStatus(g_id, ao_status)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Get Status: AioGetAoStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display status
        '----------------------------------------
        text_string = String.Format("{0:x8}H (", ao_status)
        '--------------------
        ' When status 'Device is running' is ON
        '--------------------
        If ((ao_status And AOS_BUSY) = AOS_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 ((ao_status And AOS_START_TRG) = AOS_START_TRG) Then
            text_string += ", Wait the start trigger"
        End If
        '--------------------
        ' When status 'The specified number of data are output' is ON
        '--------------------
        If ((ao_status And AOS_DATA_NUM) = AOS_DATA_NUM) Then
            text_string += ", The specified number of data are output"
        End If
        '--------------------
        ' When status 'Sampling clock error' is ON
        '--------------------
        If ((ao_status And AOS_SCERR) = AOS_SCERR) Then
            text_string += ", Sampling clock error"
        End If
        '--------------------
        ' When status 'DA conversion error' is ON
        '--------------------
        If ((ao_status And AOS_DAERR) = AOS_DAERR) Then
            text_string += ", DA conversion error"
        End If
        '--------------------
        ' When status 'Driver spec error' is ON
        '--------------------
        If ((ao_status And AOS_DRVERR) = AOS_DRVERR) Then
            text_string += ", Driver spec error"
        End If
        text_string += ")"
        TextBox_AoStatus.Text = text_string
        '----------------------------------------
        ' Display the message that get status is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Get Status: Successful completion"
    End Sub

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

        '----------------------------------------
        ' Stop generating
        '----------------------------------------
        ret = AioStopAo(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Stop: AioStopAo = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Stop sampling
        '----------------------------------------
        ret = AioStopAi(g_id)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "Generating/Sampling Stop: AioStopAi = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that generating/sampling stop is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Generating/Sampling Stop: Successful completion"
    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 file_name = "SamplingData.csv"                ' Sampling data output file name
        Dim text_string As String                         ' For updating the textbox
        Dim fp As StreamWriter                            ' File pointer
        Dim i, j As Long                                  ' Loop count
        Dim ret As Integer                                ' For return code
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Open file
        '----------------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch
            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 output data buffer
        '----------------------------------------
        If (IsArray(g_generating_data) = True) Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh_generating_data.IsAllocated = True) Then
                 gCh_generating_data.Free()
            End If
            Erase g_generating_data
        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 output data buffer
        '----------------------------------------
        If (IsArray(g_generating_data) = True) Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh_generating_data.IsAllocated = True) Then
                 gCh_generating_data.Free()
            End If
            Erase g_generating_data
        End If
        '----------------------------------------
        ' Exit process
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' Close dialog
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' Operation when event occurred
    ' Process for each event occurred
    '================================================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ai_sampling_count As Integer                  ' Sampling count
        Dim ai_status As Integer                          ' Sampling status
        Dim ao_generating_count As Integer                ' Generating count
        Dim ao_status As Integer                          ' Generating Status
        Dim ret As Integer                                ' For return code
        Dim i, j As Long                                  ' Loop count
        Dim loop_max_count As Long                        ' Maximum loop count
        Dim top_address As Long                           ' Top address of buffer for sampling data display
        Dim ai_channels As Short                          ' Number of channels

        '----------------------------------------
        ' [Sampling]
        ' 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_AiStatus.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Event that AD conversion start Process: Successful completion"
        End If
        '----------------------------------------
        ' [Sampling]
        ' 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_AiStatus.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 = "Device Operation End Event Process: AioGetAiSamplingCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display sampling count
            '----------------------------------------
            TextBox_AiSamplingCount.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
                '--------------------
                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
                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
                '--------------------
                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 sampling data storage buffer
                '--------------------
                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
                '--------------------
                ListBox_AiSamplingData.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_AiSamplingData.Items.Add(text_string)
                Next i
            End If
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Device Operation End Event Process: Successful completion"
        End If
        '----------------------------------------
        ' [Sampling]
        ' 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_AiStatus.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Overflow Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' [Sampling]
        ' 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_AiStatus.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' [Sampling]
        ' 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_AiStatus.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "AD Conversion Error Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' [Generating]
        ' Event that device operation end
        ' Acquire and display data
        '----------------------------------------
        If (m.Msg = AIOM_AOE_END) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAoStatus(g_id, ao_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAoStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ao_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If ((ao_status And AOS_BUSY) = AOS_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 ((ao_status And AOS_START_TRG) = AOS_START_TRG) Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are output' is ON
            '--------------------
            If ((ao_status And AOS_DATA_NUM) = AOS_DATA_NUM) Then
                text_string += ", The specified number of data are output"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If ((ao_status And AOS_SCERR) = AOS_SCERR) Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'DA conversion error' is ON
            '--------------------
            If ((ao_status And AOS_DAERR) = AOS_DAERR) Then
                text_string += ", DA conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If ((ao_status And AOS_DRVERR) = AOS_DRVERR) Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_AoStatus.Text = text_string
            '----------------------------------------
            ' Get the current generating count
            '----------------------------------------
            ret = AioGetAoSamplingCount(g_id, ao_generating_count)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAoSamplingCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display generating count
            '----------------------------------------
            TextBox_AoGeneratingCount.Text = ao_generating_count.ToString()
            '----------------------------------------
            ' Update the total generating count
            '----------------------------------------
            g_generating_count = ao_generating_count
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Device Operation End Event Process: Successful completion"
        End If
        '----------------------------------------
        ' [Generating]
        ' Event that sampling clock error
        ' Display the status
        '----------------------------------------
        If (m.Msg = AIOM_AOE_SCERR) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAoStatus(g_id, ao_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: AioGetAoStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ao_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If ((ao_status And AOS_BUSY) = AOS_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 ((ao_status And AOS_START_TRG) = AOS_START_TRG) Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are output' is ON
            '--------------------
            If ((ao_status And AOS_DATA_NUM) = AOS_DATA_NUM) Then
                text_string += ", The specified number of data are output"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If ((ao_status And AOS_SCERR) = AOS_SCERR) Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'DA conversion error' is ON
            '--------------------
            If ((ao_status And AOS_DAERR) = AOS_DAERR) Then
                text_string += ", DA conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If ((ao_status And AOS_DRVERR) = AOS_DRVERR) Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_AoStatus.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' [Generating]
        ' Event that DA conversion error
        ' Display the status
        '----------------------------------------
        If (m.Msg = AIOM_AOE_DAERR) Then
            '----------------------------------------
            ' Get status
            '----------------------------------------
            ret = AioGetAoStatus(g_id, ao_status)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "DA Conversion Error Occurrence Process: AioGetAoStatus = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ao_status)
            '--------------------
            ' When status 'Device is running' is ON
            '--------------------
            If ((ao_status And AOS_BUSY) = AOS_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 ((ao_status And AOS_START_TRG) = AOS_START_TRG) Then
                text_string += ", Wait the start trigger"
            End If
            '--------------------
            ' When status 'The specified number of data are output' is ON
            '--------------------
            If ((ao_status And AOS_DATA_NUM) = AOS_DATA_NUM) Then
                text_string += ", The specified number of data are output"
            End If
            '--------------------
            ' When status 'Sampling clock error' is ON
            '--------------------
            If ((ao_status And AOS_SCERR) = AOS_SCERR) Then
                text_string += ", Sampling clock error"
            End If
            '--------------------
            ' When status 'DA conversion error' is ON
            '--------------------
            If ((ao_status And AOS_DAERR) = AOS_DAERR) Then
                text_string += ", DA conversion error"
            End If
            '--------------------
            ' When status 'Driver spec error' is ON
            '--------------------
            If ((ao_status And AOS_DRVERR) = AOS_DRVERR) Then
                text_string += ", Driver spec error"
            End If
            text_string += ")"
            TextBox_AoStatus.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "DA Conversion Error Occurrence Process: Successful completion"
        End If
        MyBase.WndProc(m)
    End Sub
End Class
