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

Public Class Form1
    '----------------------------------------
    ' Macro definition
    '----------------------------------------
    Public Const MAX_AO_RANGE_NUM As Integer = 31    ' Maximum number of elements in analog output range array

    '----------------------------------------
    ' Declaration of global variables
    '----------------------------------------
    Public Shared g_id As Short                                               ' ID for device access
    Dim g_file_name As String = "GeneratingData.csv"                          ' Generating data output file name
    Dim g_generating_buffer() As Integer = Nothing                            ' User buffer
    Dim g_generating_buffer_size As Integer                                   ' User buffer size
    Dim gCh As GCHandle                                                       ' GCHandle for g_generating_buffer array
    Dim pBuffer As IntPtr                                                     ' IntPtr for g_generating_buffer 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

    Private Sub AoGeneratingInfiniteUser_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_ChNum.Text = "1"
        '----------------------------------------
        ' Update the initial display of output data generating times
        '----------------------------------------
        TextBox_GeneratingTimes.Text = "1000"
        '----------------------------------------
        ' Update the initial display of the transfer times when "Event that the specified number of data are transferred" is used
        '----------------------------------------
        TextBox_EventGeneratingTimes.Text = "500"
        '----------------------------------------
        ' Create the combobox for analog output range
        '----------------------------------------
        ComboBox_RangeValue.Items.Add("-10 - +10V")
        ComboBox_RangeValue.Items.Add("-5 - +5V")
        ComboBox_RangeValue.Items.Add("-2.5 - +2.5V")
        ComboBox_RangeValue.Items.Add("-1.25 - +1.25V")
        ComboBox_RangeValue.Items.Add("-1 - +1V")
        ComboBox_RangeValue.Items.Add("-0.625 - +0.625V")
        ComboBox_RangeValue.Items.Add("-0.5 - +0.5V")
        ComboBox_RangeValue.Items.Add("-0.3125 - +0.3125V")
        ComboBox_RangeValue.Items.Add("-0.25 - +0.25V")
        ComboBox_RangeValue.Items.Add("-0.125 - +0.125V")
        ComboBox_RangeValue.Items.Add("-0.1 - +0.1V")
        ComboBox_RangeValue.Items.Add("-0.05 - +0.05V")
        ComboBox_RangeValue.Items.Add("-0.025 - +0.025V")
        ComboBox_RangeValue.Items.Add("-0.0125 - +0.0125V")
        ComboBox_RangeValue.Items.Add("-0.01 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +10V")
        ComboBox_RangeValue.Items.Add("0 - +5V")
        ComboBox_RangeValue.Items.Add("0 - +4.095V")
        ComboBox_RangeValue.Items.Add("0 - +2.5V")
        ComboBox_RangeValue.Items.Add("0 - +1.25V")
        ComboBox_RangeValue.Items.Add("0 - +1V")
        ComboBox_RangeValue.Items.Add("0 - +0.5V")
        ComboBox_RangeValue.Items.Add("0 - +0.25V")
        ComboBox_RangeValue.Items.Add("0 - +0.1V")
        ComboBox_RangeValue.Items.Add("0 - +0.05V")
        ComboBox_RangeValue.Items.Add("0 - +0.025V")
        ComboBox_RangeValue.Items.Add("0 - +0.0125V")
        ComboBox_RangeValue.Items.Add("0 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +20mA")
        ComboBox_RangeValue.Items.Add("4 - +20mA")
        ComboBox_RangeValue.Items.Add("1 - +5V")
        '----------------------------------------
        ' Change the selection of analog output range combobox to 0
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 0
        '----------------------------------------
        ' Initialize global variables
        '----------------------------------------
        g_id = 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_ResetDevice_Click(sender As Object, e As EventArgs) Handles Button_ResetDevice.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 AioSetAoRangeAll button is clicked
    ' Set the output range of analog output
    '================================================================================
    Private Sub Button_SetAoRangeAll_Click(sender As Object, e As EventArgs) Handles Button_SetAoRangeAll.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim index As Integer                              ' Index of combobox

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

    '================================================================================
    ' It is the process when the AioSetAoChannels button is clicked
    ' Set the number of analog output channels used for generating
    '================================================================================
    Private Sub Button_SetAoChannels_Click(sender As Object, e As EventArgs) Handles Button_SetAoChannels.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ao_channels As Short                          ' Number of channels

        '----------------------------------------
        ' Get the number of channels from textbox
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.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)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAoChannels = " & ret & " : " & error_string.ToString()
    End Sub

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

        '----------------------------------------
        ' Open the condition setting dialog
        '----------------------------------------
        Dim condition_dlg As New Condition
        If (condition_dlg.ShowDialog() = DialogResult.OK) Then
            '----------------------------------------
            ' Set the event factor to 'Event that device operation end', 'Event that the specified number of data are transferred', 
            ' '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_DATA_TSF Or AOE_SCERR Or AOE_DAERR)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Condition Set: AioSetAoEvent = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display the message that condition setting is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Condition Set: Successful completion"
        End If
        condition_dlg.Dispose()
    End Sub

    '================================================================================
    ' It is the process when the User Buffer Set button is clicked
    ' Generate output data and set the user buffer that stores the output data
    '================================================================================
    Private Sub Button_UserBufferSettings_Click(sender As Object, e As EventArgs) Handles Button_UserBufferSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim 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 data_size As Short                            ' Data size
        Dim ao_resolution As Short                        ' Analog output resolution
        Dim data1, data2 As Long                          ' Temporary output data
        Dim gain, offset As Single                        ' Coefficient of sine waveform

        '----------------------------------------
        ' Get generating times from textbox
        '----------------------------------------
        If (Integer.TryParse(TextBox_GeneratingTimes.Text, generating_times) = False) Then
            generating_times = 1000
        End If
        '----------------------------------------
        ' 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 = "User Buffer Set: AioGetAoChannels = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' When the user buffer mode is used to transfer data, get data size that is necessary for one sampling
        '----------------------------------------
        ret = AioGetAoSamplingDataSize(g_id, data_size)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "User Buffer Set: AioGetAoSamplingDataSize = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Allocate a user buffer to store conversion data
        '----------------------------------------
        If (IsArray(g_generating_buffer)) Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_generating_buffer
        End If
        '--------------------
        ' User buffer size: number of packets that is necessary for one generating x generating times
        '--------------------
        g_generating_buffer_size = data_size * generating_times
        Try
            ReDim g_generating_buffer(g_generating_buffer_size - 1)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "User Buffer Set: Failed to allocate the user buffer"
            Exit Sub
        End Try
        '----------------------------------------
        ' Get the resolution of the analog output
        '----------------------------------------
        ret = AioGetAoResolution(g_id, ao_resolution)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "User Buffer Set: AioGetAoResolution = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Create analog output data: sine waveform
        '----------------------------------------
        '--------------------
        ' 16 bit resolution
        '--------------------
        If (ao_resolution = 16) Then
            gain = &HFFFF / 2
            offset = gain
        '--------------------
        ' 12 bit resolution
        '--------------------
        Else
            gain = &HFFF / 2
            offset = gain
        End If
        '--------------------
        ' Create data
        '--------------------
        For i = 0 To generating_times - 1
            For j = 0 To data_size - 1
                If ((j * 2 + 1) >= ao_channels) Then
                    data1 = gain * Math.Sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * ((j * 2) Mod 4)) + offset
                    data2 = 0
                Else
                    data1 = gain * Math.Sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * ((j * 2) Mod 4)) + offset
                    data2 = gain * Math.Sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * ((j * 2 + 1) Mod 4)) + offset
                End If
                g_generating_buffer(i * data_size + j) = (data2 And &HFFFF) << 16 Or (data1 And &HFFFF)
            Next j
        Next i
        '----------------------------------------
        ' Open file
        '----------------------------------------
        Try
            fp = File.CreateText(g_file_name)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "User Buffer Set: File.CreateText = file open error."
            Exit Sub
        End Try
        '----------------------------------------
        ' Create 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 packets
            '--------------------
            For j = 0 To data_size - 1
                text_string += String.Format(" {0:X8}", g_generating_buffer(i * data_size + j))
                '--------------------
                ' Add newline character for the last packet
                '--------------------
                If (j = (data_size - 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 = "User Buffer Set: fp.Write = file write error."
                fp.Close()
                Exit Sub
            End Try
        Next i
        '----------------------------------------
        ' Close file
        '----------------------------------------
        fp.Close()
        '----------------------------------------
        ' Specify Pinned handle to g_generating_buffer
        '----------------------------------------
        If (gCh.IsAllocated = False) Then
            gCh = GCHandle.Alloc(g_generating_buffer, GCHandleType.Pinned)
        End If
        '----------------------------------------
        ' Get address object by Pinned handle
        '----------------------------------------
        pBuffer = gCh.AddrOfPinnedObject()
        '----------------------------------------
        ' Set the user buffer
        '----------------------------------------
        ret = AioSetAoTransferData(g_id, g_generating_buffer_size, pBuffer)
        '----------------------------------------
        ' Error process
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' Get error string
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' Display message
            '--------------------
            TextBox_ReturnCode.Text = "User Buffer Set: AioSetAoTransferData = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "User Buffer Set : Successful completion"
    End Sub

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

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

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

        '----------------------------------------
        ' 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 Start: AioResetAoStatus = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' 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 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 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_Status.Text = text_string
        '----------------------------------------
        ' Update the total generating count
        '----------------------------------------
        TextBox_TransferCount.Text = "0"
        '----------------------------------------
        ' Update the setting generating count
        '----------------------------------------
        TextBox_TransferLap.Text = "0"
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "Generating Start: Successful completion"
    End Sub

    '================================================================================
    ' It is the process when the AioGetAoTransferCount button is clicked
    ' Get the number of data that are transferred from the user memory and display it on the dialog
    '================================================================================
    Private Sub Button_GetAoTransferCount_Click(sender As Object, e As EventArgs) Handles Button_GetAoTransferCount.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim ao_transfer_count As Integer                  ' Transfer times

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

    '================================================================================
    ' It is the process when the AioGetAoTransferLap button is clicked
    ' Get the repeat times if the user buffer is used in repeat mode and display it on the dialog
    '================================================================================
    Private Sub Button_GetAoTransferLap_Click(sender As Object, e As EventArgs) Handles Button_GetAoTransferLap.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim ret As Integer                                ' For return code
        Dim lap As Integer                                ' Repeat times

        '----------------------------------------
        ' Get repeat times
        '----------------------------------------
        ret = AioGetAoTransferLap(g_id, lap)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAoTransferLap = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the repeat times acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' Update repeat times
            '--------------------
            TextBox_TransferLap.Text = lap.ToString()
        End If
    End Sub

    '================================================================================
    ' It is the process when the AioGetAoStatus button is clicked
    ' Get the analog output status and display it on the dialog
    '================================================================================
    Private Sub Button_GetAoStatus_Click(sender As Object, e As EventArgs) Handles Button_GetAoStatus.Click
        Dim error_string As New StringBuilder("", 256)    ' Error code string
        Dim text_string As String                         ' For updating the textbox
        Dim ao_status As Integer                          ' Analog output status
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Get status
        '----------------------------------------
        ret = AioGetAoStatus(g_id, ao_status)
        '----------------------------------------
        ' Get error string
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' Display message
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAoStatus = " & ret & " : " & error_string.ToString()
        '----------------------------------------
        ' When the status acquisition function is completed successfully
        '----------------------------------------
        If (ret = 0) Then
            '----------------------------------------
            ' Display status
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", 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_Status.Text = text_string
        End If
    End Sub

    '================================================================================
    ' It is the process when the Generating Stop button is clicked
    ' Stop generating
    '================================================================================
    Private Sub Button_GeneratingStop_Click(sender As Object, e As EventArgs) Handles Button_GeneratingStop.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 Stop: AioStopAo = " & ret & " : " & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Display the message that generating stop is completed
        '----------------------------------------
        TextBox_ReturnCode.Text = "Generating Stop: Successful completion"
    End Sub

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

        '----------------------------------------
        ' Release the output data buffer
        '----------------------------------------
        If (IsArray(g_generating_buffer)) Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_generating_buffer
        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 output data buffer
        '----------------------------------------
        If (IsArray(g_generating_buffer)) Then
            '----------------------------------------
            ' GCHandle free
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_generating_buffer
        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 ao_transfer_count As Integer                  ' Transfer times
        Dim lap As Integer                                ' Repeat times
        Dim ao_status As Integer                          ' Analog output status
        Dim ret As Integer                                ' For return code

        '----------------------------------------
        ' Event that the specified number of data are transferred
        ' Acquire and display data
        '----------------------------------------
        If (m.Msg = AIOM_AOE_DATA_TSF) 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 = "Event that the specified number of data are transferred 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_Status.Text = text_string
            '----------------------------------------
            ' Get the number of data that are transferred from the user memory
            '----------------------------------------
            ret = AioGetAoTransferCount(g_id, ao_transfer_count)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: AioGetAoTransferCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display transfer position
            '----------------------------------------
            TextBox_TransferCount.Text = ao_transfer_count.ToString()
            '----------------------------------------
            ' Get the repeat times if the user buffer is used in repeat mode
            '----------------------------------------
            ret = AioGetAoTransferLap(g_id, lap)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: AioGetAoTransferLap = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display repeat times
            '----------------------------------------
            TextBox_TransferLap.Text = lap.ToString()
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Event that the specified number of data are transferred Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that device operation end
        ' Acquire and display data
        '----------------------------------------
        If (m.Msg = AIOM_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_Status.Text = text_string
            '----------------------------------------
            ' Get the number of data that are transferred from the user memory
            '----------------------------------------
            ret = AioGetAoTransferCount(g_id, ao_transfer_count)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAoTransferCount = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display transfer position
            '----------------------------------------
            TextBox_TransferCount.Text = ao_transfer_count.ToString()
            '----------------------------------------
            ' Get the repeat times if the user buffer is used in repeat mode
            '----------------------------------------
            ret = AioGetAoTransferLap(g_id, lap)
            '----------------------------------------
            ' Error process
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' Get error string
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' Display message
                '--------------------
                TextBox_ReturnCode.Text = "Device Operation End Event Process: AioGetAoTransferLap = " & ret & " : " & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' Display repeat times
            '----------------------------------------
            TextBox_TransferLap.Text = lap.ToString()
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Device Operation End Event Process: Successful completion"
        End If
        '----------------------------------------
        ' 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_Status.Text = text_string
            '----------------------------------------
            ' Display the message that event processing is completed
            '----------------------------------------
            TextBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion"
        End If
        '----------------------------------------
        ' Event that 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_Status.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
