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

Public Class Form1
    '----------------------------------------
    ' マクロ定義
    '----------------------------------------
    Public Const MAX_AI_RANGE_NUM As Integer = 32            ' アナログ入力レンジ配列最大要素数
    Public Const DISPLAY_SAMPLING_COUNT As Integer = 1000    ' ListBoxに表示するサンプリングデータの最大サンプリング回数

    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Public Shared g_id As Short                                               ' デバイスアクセス用ID
    Dim g_sampling_buffer() As Integer = Nothing                              ' ユーザーバッファ
    Dim g_sampling_buffer_size As Integer                                     ' ユーザーバッファサイズ
    Dim g_data_size As Short                                                  ' データサイズ（パケットの個数）
    Dim g_transfer_count As Long                                              ' 総転送回数
    Dim gCh As GCHandle                                                       ' g_sampling_buffer配列用GCHandle
    Dim pBuffer As IntPtr                                                     ' g_sampling_buffer配列用IntPtr
    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 AiSamplingAttachedData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '----------------------------------------
        ' デフォルトボタンを設定
        '----------------------------------------
        AcceptButton = Button_Close
        '----------------------------------------
        ' デバイス名の初期表示を更新
        '----------------------------------------
        TextBox_DeviceName.Text = "AIO000"
        '----------------------------------------
        ' アナログ入力のチャネル数の初期表示を更新
        '----------------------------------------
        TextBox_ChNum.Text = "1"
        '----------------------------------------
        ' アナログ入力方式用コンボボックスの作成
        '----------------------------------------
        ComboBox_InpMethod.Items.Add("シングルエンド入力")
        ComboBox_InpMethod.Items.Add("差動入力")
        '----------------------------------------
        ' アナログ入力方式用コンボボックスの選択を0番に変更
        '----------------------------------------
        ComboBox_InpMethod.SelectedIndex = 0
        '----------------------------------------
        ' サンプリングクロックの初期表示を更新
        '----------------------------------------
        TextBox_SamplingClock.Text = "1000"
        '----------------------------------------
        ' 変換速度の初期表示を更新
        '----------------------------------------
        TextBox_ScanClock.Text = "10"
        '----------------------------------------
        ' サンプリング停止回数の初期表示を更新
        '----------------------------------------
        TextBox_StopTimes.Text = "1000"
        '----------------------------------------
        ' 添付データの初期表示を更新
        '----------------------------------------
        CheckBox_AiAttachInfomation.Checked = False
        CheckBox_AoData.Checked = False
        CheckBox_DioData.Checked = False
        CheckBox_CntCh0Data.Checked = False
        CheckBox_CntCh1Data.Checked = False
        '----------------------------------------
        ' データ転送回数の初期表示を更新
        '----------------------------------------
        TextBox_SamplingTimes.Text = "2000"
        '----------------------------------------
        ' アナログ入力レンジ用コンボボックスの作成
        '----------------------------------------
        ComboBox_RangeValue.Items.Add("-10 - +10V")
        ComboBox_RangeValue.Items.Add("-5 - +5V")
        ComboBox_RangeValue.Items.Add("-2.5 - +2.5V")
        ComboBox_RangeValue.Items.Add("-1.25 - +1.25V")
        ComboBox_RangeValue.Items.Add("-1 - +1V")
        ComboBox_RangeValue.Items.Add("-0.625 - +0.625V")
        ComboBox_RangeValue.Items.Add("-0.5 - +0.5V")
        ComboBox_RangeValue.Items.Add("-0.3125 - +0.3125V")
        ComboBox_RangeValue.Items.Add("-0.25 - +0.25V")
        ComboBox_RangeValue.Items.Add("-0.125 - +0.125V")
        ComboBox_RangeValue.Items.Add("-0.1 - +0.1V")
        ComboBox_RangeValue.Items.Add("-0.05 - +0.05V")
        ComboBox_RangeValue.Items.Add("-0.025 - +0.025V")
        ComboBox_RangeValue.Items.Add("-0.0125 - +0.0125V")
        ComboBox_RangeValue.Items.Add("-0.01 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +10V")
        ComboBox_RangeValue.Items.Add("0 - +5V")
        ComboBox_RangeValue.Items.Add("0 - +4.095V")
        ComboBox_RangeValue.Items.Add("0 - +2.5V")
        ComboBox_RangeValue.Items.Add("0 - +1.25V")
        ComboBox_RangeValue.Items.Add("0 - +1V")
        ComboBox_RangeValue.Items.Add("0 - +0.5V")
        ComboBox_RangeValue.Items.Add("0 - +0.25V")
        ComboBox_RangeValue.Items.Add("0 - +0.1V")
        ComboBox_RangeValue.Items.Add("0 - +0.05V")
        ComboBox_RangeValue.Items.Add("0 - +0.025V")
        ComboBox_RangeValue.Items.Add("0 - +0.0125V")
        ComboBox_RangeValue.Items.Add("0 - +0.01V")
        ComboBox_RangeValue.Items.Add("0 - +20mA")
        ComboBox_RangeValue.Items.Add("4 - +20mA")
        ComboBox_RangeValue.Items.Add("-20mA - +20mA")
        ComboBox_RangeValue.Items.Add("1 - +5V")
        '----------------------------------------
        ' アナログ入力レンジ用コンボボックスの選択を0番に変更
        '----------------------------------------
        ComboBox_RangeValue.SelectedIndex = 0
        '----------------------------------------
        ' グローバル変数の初期化
        '----------------------------------------
        g_id = 0
        g_transfer_count = 0
        g_data_size = 0
        g_sampling_buffer_size = 0
    End Sub

    '================================================================================
    ' AioInitボタンをクリックした際の処理になります
    ' デバイスの初期化処理を行います
    '================================================================================
    Private Sub Button_AioInit_Click(sender As Object, e As EventArgs) Handles Button_AioInit.Click
        Dim device_name As String                         ' デバイス名
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' デバイス名をテキストボックスから取得
        '----------------------------------------
        device_name = TextBox_DeviceName.Text
        '----------------------------------------
        ' デバイス初期化
        ' 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioInit(device_name, g_id)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioInit = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioResetDeviceボタンをクリックした際の処理になります
    ' デバイスのリセット処理を行います
    '================================================================================
    Private Sub Button_AioResetDev_Click(sender As Object, e As EventArgs) Handles Button_AioResetDev.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' デバイスをリセット
        '----------------------------------------
        ret = AioResetDevice(g_id)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioResetDevice = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiRangeAllボタンをクリックした際の処理になります
    ' アナログ入力の入力レンジの設定を行います
    '================================================================================
    Private Sub Button_SetAiRangeAll_Click(sender As Object, e As EventArgs) Handles Button_SetAiRangeAll.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim index As Integer                              ' コンボボックスのインデックス

        '----------------------------------------
        ' GUIの情報を更新
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' コンボボックスからレンジ設定値のインデックスを取得
        '----------------------------------------
        index = ComboBox_RangeValue.SelectedIndex
        '----------------------------------------
        ' アナログ入力レンジの設定(全チャネル)
        '----------------------------------------
        ret = AioSetAiRangeAll(g_id, g_ai_range(index))
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiRangeAll = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiInputMethodボタンをクリックした際の処理になります
    ' アナログ入力の入力方式の設定を行います
    '================================================================================
    Private Sub Button_SetAiInpMethod_Click(sender As Object, e As EventArgs) Handles Button_SetAiInpMethod.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim input_method As Integer                       ' 入力方式

        '----------------------------------------
        ' GUIの情報を更新
        '----------------------------------------
        Me.Refresh()
        '----------------------------------------
        ' コンボボックスから入力方式設定値を取得
        '----------------------------------------
        input_method = ComboBox_InpMethod.SelectedIndex
        '----------------------------------------
        ' アナログ入力方式の設定
        '----------------------------------------
        ret = AioSetAiInputMethod(g_id, input_method)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiInputMethod = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiChannelsボタンをクリックした際の処理になります
    ' サンプリングに使用するアナログ入力チャネル数の設定を行います
    '================================================================================
    Private Sub Button_SetAiChannels_Click(sender As Object, e As EventArgs) Handles Button_SetAiChannels.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_channels As Short                          ' チャネル数

        '----------------------------------------
        ' テキストボックスからチャネル数を取得
        '----------------------------------------
        If (Short.TryParse(TextBox_ChNum.Text, ai_channels) = False) Then
            ai_channels = 1
        End If
        '----------------------------------------
        ' 変換に使用するアナログ入力チャネル数を設定
        '----------------------------------------
        ret = AioSetAiChannels(g_id, ai_channels)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiChannels = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiSamplingClockボタンをクリックした際の処理になります
    ' サンプリングに使用する内部クロック変換速度の設定を行います
    '================================================================================
    Private Sub Button_SamplingClock_Click(sender As Object, e As EventArgs) Handles Button_SamplingClock.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim sampling_clock As Single                      ' 内部クロックの変換速度

        '----------------------------------------
        ' テキストボックスから内部クロックの変換速度を取得
        '----------------------------------------
        If (Single.TryParse(TextBox_SamplingClock.Text, sampling_clock) = False) Then
            sampling_clock = 1000
        End If
        '----------------------------------------
        ' 内部クロックの変換速度を設定
        '----------------------------------------
        ret = AioSetAiSamplingClock(g_id, sampling_clock)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiSamplingClock = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiScanClockボタンをクリックした際の処理になります
    ' チャネル変換速度の設定を行います
    '================================================================================
    Private Sub Button_ScanClock_Click(sender As Object, e As EventArgs) Handles Button_ScanClock.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim scan_clock As Single                          ' 変換速度

        '----------------------------------------
        ' テキストボックスから変換速度を取得
        '----------------------------------------
        If (Single.TryParse(TextBox_ScanClock.Text, scan_clock) = False) Then
            scan_clock = 10
        End If
        '----------------------------------------
        ' 変換速度を設定
        '----------------------------------------
        ret = AioSetAiScanClock(g_id, scan_clock)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiScanClock = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiStopTimesボタンをクリックした際の処理になります
    ' サンプリング停止回数の設定を行います
    '================================================================================
    Private Sub Button_StopTimes_Click(sender As Object, e As EventArgs) Handles Button_StopTimes.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim stop_times As Integer                         ' サンプリング停止回数

        '----------------------------------------
        ' テキストボックスからサンプリング停止回数を取得
        '----------------------------------------
        If (Integer.TryParse(TextBox_StopTimes.Text, stop_times) = False) Then
            stop_times = 1000
        End If
        '----------------------------------------
        ' サンプリング停止回数を設定
        '----------------------------------------
        ret = AioSetAiStopTimes(g_id, stop_times)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiStopTimes = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' AioSetAiAttachedDataボタンをクリックした際の処理になります
    ' AD変換データに添付するデータの設定を行います
    '================================================================================
    Private Sub Button_AttachData_Click(sender As Object, e As EventArgs) Handles Button_AttachData.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim attach_data As Integer                        ' AD変換データに加えて添付するデータの種類

        '----------------------------------------
        ' テキストボックスから添付するデータの種類を取得
        '----------------------------------------
        attach_data = IIf(CheckBox_AiAttachInfomation.Checked, AIAT_AI, 0) Or
                      IIf(CheckBox_AoData.Checked, AIAT_AO0, 0) Or
                      IIf(CheckBox_DioData.Checked, AIAT_DIO0, 0) Or
                      IIf(CheckBox_CntCh0Data.Checked, AIAT_CNT0, 0) Or
                      IIf(CheckBox_CntCh1Data.Checked, AIAT_CNT1, 0)
        '----------------------------------------
        ' AD変換データに添付するデータを設定
        '----------------------------------------
        ret = AioSetAiAttachedData(g_id, attach_data)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioSetAiAttachedData = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' 基本設定ボタンをクリックした際の処理になります
    ' サンプリングに必要な基本設定を行います
    ' (基本設定は初期化処理/リセット処理実行後、初回のサンプリング開始前に実行する必要があります)
    '================================================================================
    Private Sub Button_SmpBasicSettings_Click(sender As Object, e As EventArgs) Handles Button_SmpBasicSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' データ格納用メモリ形式をメモリへの上書きなしに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiMemoryType(g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiMemoryType = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' サンプリング開始条件をソフトウェアに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiStartTrigger(g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiStartTrigger = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' クロック種類を内部クロックに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiClockType(g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiClockType = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' サンプリング停止条件を設定回数変換終了に設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiStopTrigger(g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiStopTrigger = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' イベントにデバイス動作終了イベント、オーバーフローイベント、
        ' サンプリングクロックエラーイベント、AD変換エラーイベントを設定
        ' 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiEvent(g_id, Handle, AIE_END Or AIE_OFERR Or AIE_SCERR Or AIE_ADERR)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiEvent = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' 基本設定完了のメッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "基本設定：正常終了"
    End Sub

    '================================================================================
    ' ユーザーバッファサイズ設定ボタンをクリックした際の処理になります
    ' 変換データを格納するユーザーバッファの設定を行います
    '================================================================================
    Private Sub Button_UserBufferSettings_Click(sender As Object, e As EventArgs) Handles Button_UserBufferSettings.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim data_size As Short                            ' データサイズ（パケットの個数）
        Dim sampling_times As Long                        ' サンプリング回数

        '----------------------------------------
        ' アナログ入力の変換データ転送方式をユーザーバッファモードに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiTransferMode(g_id, 1)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "ユーザーバッファサイズ設定：AioSetAiTransferMode = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' ユーザーバッファモードでの転送時に、１サンプリングあたりで必要となるデータサイズを取得
        '----------------------------------------
        ret = AioGetAiSamplingDataSize(g_id, data_size)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "ユーザーバッファサイズ設定：AioGetAiSamplingDataSize = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' テキストボックスからサンプリング回数を取得
        '----------------------------------------
        If (Long.TryParse(TextBox_SamplingTimes.Text, sampling_times) = False) Then
            sampling_times = 2000
        End If
        '----------------------------------------
        ' 変換データを格納するユーザーバッファを確保
        '----------------------------------------
        If (IsArray(g_sampling_buffer) = True) Then
            '----------------------------------------
            ' GCHandleの解放
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_sampling_buffer
        End If
        '--------------------
        ' ユーザーバッファサイズ：1サンプリングに必要なパケット数×サンプリング回数
        '--------------------
        g_data_size = data_size
        g_sampling_buffer_size = data_size * sampling_times
        Try
            ReDim g_sampling_buffer(g_sampling_buffer_size)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "ユーザーバッファサイズ設定：ユーザーバッファ確保に失敗"
            Exit Sub
        End Try
        '----------------------------------------
        ' PinnedハンドルをgChに分配する
        '----------------------------------------
        If (gCh.IsAllocated = False) Then
            gCh = GCHandle.Alloc(g_sampling_buffer, GCHandleType.Pinned)
        End If
        '----------------------------------------
        ' Pinnedハンドルでオブジェクトのアドレスを取得
        '----------------------------------------
        pBuffer = gCh.AddrOfPinnedObject()
        '----------------------------------------
        ' ユーザーバッファの設定
        '----------------------------------------
        ret = AioSetAiTransferData(g_id, g_sampling_buffer_size, pBuffer)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "ユーザーバッファサイズ設定：AioSetAiTransferData = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' ユーザーバッファ設定完了のメッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "ユーザーバッファサイズ設定：正常終了"
    End Sub

    '================================================================================
    ' サンプリング開始ボタンをクリックした際の処理になります
    ' メモリとステータスのクリアを行った後、サンプリング開始を行います
    '================================================================================
    Private Sub Button_SamplingStart_Click(sender As Object, e As EventArgs) Handles Button_SamplingStart.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_status As Integer                          ' アナログ入力ステータス

        '----------------------------------------
        ' ステータスクリア
        '----------------------------------------
        ret = AioResetAiStatus(g_id)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング開始：AioResetAiStatus = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' 総転送回数を初期化
        '----------------------------------------
        g_transfer_count = 0
        '----------------------------------------
        ' サンプリング開始
        '----------------------------------------
        ret = AioStartAi(g_id)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング開始：AioStartAi = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' ステータスを取得
        '----------------------------------------
        ret = AioGetAiStatus(g_id, ai_status)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング開始：AioGetAiStatus = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' ステータスの表示
        '----------------------------------------
        text_string = String.Format("{0:x8}H (", ai_status)
        '--------------------
        ' 動作中ステータス有効時
        '--------------------
        If (ai_status And AIS_BUSY) = AIS_BUSY Then
            text_string += "動作中"
        '--------------------
        ' 動作中ステータス無効時
        '--------------------
        Else
            text_string += "停止中"
        End If
        '--------------------
        ' 開始トリガ待ちステータス有効時
        '--------------------
        If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
            text_string += ", 開始トリガ待ち"
        End If
        '--------------------
        ' 指定サンプリング回数格納ステータス有効時
        '--------------------
        If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
            text_string += ", 指定回数格納"
        End If
        '--------------------
        ' オーバーフローステータス有効時
        '--------------------
        If (ai_status And AIS_OFERR) = AIS_OFERR Then
            text_string += ", オーバーフロー"
        End If
        '--------------------
        ' サンプリングクロックエラーステータス有効時
        '--------------------
        If (ai_status And AIS_SCERR) = AIS_SCERR Then
            text_string += ", サンプリングクロックエラー"
        End If
        '--------------------
        ' AD変換エラーステータス有効時
        '--------------------
        If (ai_status And AIS_AIERR) = AIS_AIERR Then
            text_string += ", AD変換エラー"
        End If
        '--------------------
        ' ドライバスペックエラーステータス有効時
        '--------------------
        If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
            text_string += ", ドライバスペックエラー"
        End If
        text_string += ")"
        TextBox_Status.Text = text_string
        '----------------------------------------
        ' 転送回数を更新
        '----------------------------------------
        TextBox_TransferCount.Text = "0"
        '----------------------------------------
        ' サンプリングデータ表示用ListBoxをクリア
        '----------------------------------------
        ListBox_SamplingData.Items.Clear()
        '----------------------------------------
        ' サンプリング開始完了のメッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "サンプリング開始：正常終了"
    End Sub

    '================================================================================
    ' AioGetAiTransferCountボタンをクリックした際の処理になります
    ' 転送回数を取得しダイアログ上に表示します
    '================================================================================
    Private Sub Button_GetTransferCount_Click(sender As Object, e As EventArgs) Handles Button_GetTransferCount.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_transfer_count As Integer                  ' 転送回数

        '----------------------------------------
        ' 転送回数を取得
        '----------------------------------------
        ret = AioGetAiTransferCount(g_id, ai_transfer_count)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiTransferCount = " & ret & "：" & error_string.ToString()
        '----------------------------------------
        ' 転送回数取得関数が正常終了した場合
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' 転送回数を更新
            '--------------------
            TextBox_TransferCount.Text = ai_transfer_count.ToString()
        End If
    End Sub

    '================================================================================
    ' AioGetAiStatusボタンをクリックした際の処理になります
    ' アナログ入力ステータスを取得しダイアログ上に表示します
    '================================================================================
    Private Sub Button_GetAiStatus_Click(sender As Object, e As EventArgs) Handles Button_GetAiStatus.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_status As Integer                          ' ステータス

        '----------------------------------------
        ' ステータスを取得
        '----------------------------------------
        ret = AioGetAiStatus(g_id, ai_status)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiStatus = " & ret & "：" & error_string.ToString()
        '----------------------------------------
        ' ステータス取得関数が正常終了した場合
        '----------------------------------------
        If (ret = 0) Then
            '----------------------------------------
            ' ステータスの表示
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' 動作中ステータス有効時
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "動作中"
            '--------------------
            ' 動作中ステータス無効時
            '--------------------
            Else
                text_string += "停止中"
            End If
            '--------------------
            ' 開始トリガ待ちステータス有効時
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", 開始トリガ待ち"
            End If
            '--------------------
            ' 指定サンプリング回数格納ステータス有効時
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", 指定回数格納"
            End If
            '--------------------
            ' オーバーフローステータス有効時
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", オーバーフロー"
            End If
            '--------------------
            ' サンプリングクロックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", サンプリングクロックエラー"
            End If
            '--------------------
            ' AD変換エラーステータス有効時
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD変換エラー"
            End If
            '--------------------
            ' ドライバスペックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", ドライバスペックエラー"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
        End If
    End Sub

    '================================================================================
    ' サンプリング停止ボタンをクリックした際の処理になります
    ' サンプリング停止コマンドを実行します
    '================================================================================
    Private Sub Button_SamplingStop_Click(sender As Object, e As EventArgs) Handles Button_SamplingStop.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' サンプリング停止
        '----------------------------------------
        ret = AioStopAi(g_id)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング停止：AioStopAi = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' サンプリング停止完了のメッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "サンプリング停止：正常終了"
    End Sub

    '================================================================================
    ' 取得データファイル出力ボタンをクリックした際の処理になります
    ' グローバルで保持しているサンプリングデータをCSVファイルに出力します
    '================================================================================
    Private Sub Button_OutputSamplingData_Click(sender As Object, e As EventArgs) Handles Button_OutputSamplingData.Click
        Dim file_name As String = "SamplingData.csv"    ' サンプリングデータ出力ファイル名
        Dim text_string As String = ""                  ' テキストボックス更新用
        Dim fp As StreamWriter                          ' ファイルポインタ
        Dim i, j As Long                                ' ループカウント
        Dim loop_max_count As Long                      ' ループカウント最大値

        '----------------------------------------
        ' ファイルを開く
        '----------------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "取得データファイル出力：File.CreateText = file open error."
            Exit Sub
        End Try
        '----------------------------------------
        ' ファイル出力用ループの最大ループ回数を設定
        '----------------------------------------
        '--------------------
        ' 総転送回数がユーザーバッファサイズを超えた場合
        '--------------------
        If (g_transfer_count > g_sampling_buffer_size) Then
            '--------------------
            ' 最大ループ回数をユーザーバッファサイズに制限
            '--------------------
            loop_max_count = g_sampling_buffer_size
        '--------------------
        ' 総転送回数がユーザーバッファサイズ以下の場合
        '--------------------
        Else
            '--------------------
            ' 最大ループ回数に総転送回数を設定
            '--------------------
            loop_max_count = g_transfer_count
        End If
        '----------------------------------------
        ' ファイル出力用サンプリングデータの作成
        '----------------------------------------
        '--------------------
        ' 総転送回数が1以上場合
        '--------------------
        If (g_transfer_count > 0) Then
            '--------------------
            ' 総転送回数まで処理を繰り返す
            '--------------------
            For i = 0 To (loop_max_count / g_data_size) - 1
                text_string = ""
                '--------------------
                ' 表示用文字列の生成
                '--------------------
                text_string += String.Format("{0,5},", (i + 1))
                '--------------------
                ' データサイズ（パケットの個数）だけ処理を繰り返す
                '--------------------
                For j = 0 To g_data_size - 1
                    text_string += String.Format(" {0:X8}", g_sampling_buffer(i * g_data_size + j))
                    '--------------------
                    ' 最終パケットの場合改行文字を追加する
                    '--------------------
                    If (j = (g_data_size - 1)) Then
                        text_string += vbCrLf
                    '--------------------
                    ' 最終パケット以外の場合カンマを追加する
                    '--------------------
                    Else
                        text_string += ","
                    End If
                Next j
                '----------------------------------------
                ' データ書き込み
                '----------------------------------------
                Try
                    fp.Write(text_string)
                Catch ex As Exception
                    TextBox_ReturnCode.Text = "取得データファイル出力：fp.Write = file write error."
                    fp.Close()
                    Exit Sub
                End Try
            Next i
        End If
        '----------------------------------------
        ' ファイルを閉じる
        '----------------------------------------
        fp.Close()
        '----------------------------------------
        ' ファイル出力完了のメッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "取得データファイル出力：" & Chr(34) & file_name & Chr(34) & "にデータを保存しました。"
    End Sub

    '================================================================================
    ' AioExitボタンをクリックした際の処理になります
    ' デバイスの終了処理を行います
    '================================================================================
    Private Sub Button_AioExit_Click(sender As Object, e As EventArgs) Handles Button_AioExit.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim ret As Integer                                ' 戻り値取得用

        '----------------------------------------
        ' 終了処理の実行
        '----------------------------------------
        ret = AioExit(g_id)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioExit = " & ret & "：" & error_string.ToString()
    End Sub

    '================================================================================
    ' 閉じるをクリックした際の処理になります
    ' デバイスの終了処理を行い、アプリケーションを終了します
    '================================================================================
    Private Sub Button_Close_Click(sender As Object, e As EventArgs) Handles Button_Close.Click
        '----------------------------------------
        ' 終了処理の実行
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' 変換データを格納するユーザーバッファを解放
        '----------------------------------------
        If (IsArray(g_sampling_buffer) = True) Then
            '----------------------------------------
            ' GCHandleの解放
            '----------------------------------------
            If (gCh.IsAllocated = True) Then
                gCh.Free()
            End If
            Erase g_sampling_buffer
        End If
        '----------------------------------------
        ' ダイアログを終了する
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' イベントが発生した時の動作
    ' 各イベントが発生したとき、イベント毎に処理を行います
    '================================================================================
    Protected Overrides Sub WndProc(ByRef m As Message)
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_transfer_count As Integer                  ' 転送回数
        Dim ai_status As Integer                          ' ステータス
        Dim i, j As Long                                  ' ループカウント
        Dim loop_max_count As Long                        ' ループカウント最大値
        Dim top_address As Long                           ' サンプリングデータ表示のバッファの先頭アドレス

        '----------------------------------------
        ' デバイス動作終了イベント
        ' データの取得、表示を行う
        '----------------------------------------
        If (m.Msg = AIOM_AIE_END) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：AioGetAiStatus = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' ステータスの表示
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' 動作中ステータス有効時
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "動作中"
            '--------------------
            ' 動作中ステータス無効時
            '--------------------
            Else
                text_string += "停止中"
            End If
            '--------------------
            ' 開始トリガ待ちステータス有効時
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", 開始トリガ待ち"
            End If
            '--------------------
            ' 指定サンプリング回数格納ステータス有効時
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", 指定回数格納"
            End If
            '--------------------
            ' オーバーフローステータス有効時
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", オーバーフロー"
            End If
            '--------------------
            ' サンプリングクロックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", サンプリングクロックエラー"
            End If
            '--------------------
            ' AD変換エラーステータス有効時
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD変換エラー"
            End If
            '--------------------
            ' ドライバスペックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", ドライバスペックエラー"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' 現在の転送回数を取得
            '----------------------------------------
            ret = AioGetAiTransferCount(g_id, ai_transfer_count)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：AioGetAiTransferCount = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' 転送回数を表示
            '----------------------------------------
            TextBox_TransferCount.Text = ai_transfer_count.ToString()
            '----------------------------------------
            ' 総転送回数を更新
            '----------------------------------------
            g_transfer_count = ai_transfer_count
            '----------------------------------------
            ' サンプリングデータ表示ループのループ回数を設定
            '----------------------------------------
            '--------------------
            ' ユーザーバッファサイズがデータ表示最大サンプリング回数未満の場合
            '--------------------
            If ((g_sampling_buffer_size / g_data_size) < DISPLAY_SAMPLING_COUNT) Then
                '--------------------
                ' 今回取得できたデータの転送回数がユーザーバッファサイズを超えた場合
                '--------------------
                If (ai_transfer_count > g_sampling_buffer_size) Then
                    '--------------------
                    ' 最大ループ回数をユーザーバッファサイズに制限
                    '--------------------
                    loop_max_count = (g_sampling_buffer_size / g_data_size)
                '--------------------
                ' 今回取得できたデータの転送回数がユーザーバッファサイズ以下の場合
                '--------------------
                Else
                    '--------------------
                    ' 最大ループ回数を転送回数に制限
                    '--------------------
                    loop_max_count = (ai_transfer_count / g_data_size)
                End If
                '--------------------
                ' 参照するバッファの先頭アドレスを0に設定
                '--------------------
                top_address = 0
            '--------------------
            ' 今回取得できたデータの転送回数がユーザーバッファサイズを超えた場合
            '--------------------
            ElseIf (ai_transfer_count > g_sampling_buffer_size) Then
                '--------------------
                ' 最大ループ回数をデータ表示最大サンプリング回数に制限
                '--------------------
                loop_max_count = DISPLAY_SAMPLING_COUNT
                '--------------------
                ' 参照するバッファの先頭アドレスをユーザーバッファの末尾から
                ' データ表示最大サンプリング回数前のアドレスに設定
                '--------------------
                top_address = (g_sampling_buffer_size / g_data_size) - DISPLAY_SAMPLING_COUNT
            '--------------------
            ' 今回取得できたデータの転送回数がデータ表示最大サンプリング回数を超えた場合
            '--------------------
            ElseIf ((ai_transfer_count / g_data_size) > DISPLAY_SAMPLING_COUNT) Then
                '--------------------
                ' 最大ループ回数をデータ表示最大サンプリング回数に制限
                '--------------------
                loop_max_count = DISPLAY_SAMPLING_COUNT
                '--------------------
                ' 参照するバッファの先頭アドレスを取得データの末尾から
                ' データ表示最大サンプリング回数前のアドレスに設定
                '--------------------
                top_address = (ai_transfer_count / g_data_size) - DISPLAY_SAMPLING_COUNT
            '--------------------
            ' 今回取得できたデータの転送回数がデータ表示最大サンプリング回数以下の場合
            '--------------------
            Else
                '--------------------
                ' 最大ループ回数にの転送回数を設定
                '--------------------
                loop_max_count = (ai_transfer_count / g_data_size)
                '--------------------
                ' 参照するバッファの先頭アドレスを0に設定
                '--------------------
                top_address = 0
            End If
            '----------------------------------------
            ' サンプリングデータを表示
            ' (取得データの末尾からデータ表示最大サンプリング回数までを表示)
            '----------------------------------------
            '--------------------
            ' 今回取得できたデータの転送回数が1以上の場合
            '--------------------
            If (ai_transfer_count > 0) Then
                '--------------------
                ' サンプリングデータ表示用ListBoxをクリア
                '--------------------
                ListBox_SamplingData.Items.Clear()
                '--------------------
                ' 表示データの最大サンプリング回数まで処理を繰り返す
                '--------------------
                For i = top_address To (loop_max_count + top_address) - 1
                    '--------------------
                    ' 表示用文字列格納変数をクリア
                    '--------------------
                    text_string = ""
                    '--------------------
                    ' 表示用文字列の生成
                    '--------------------
                    text_string += String.Format("{0,5}:  ", (i + 1))
                    '--------------------
                    ' データサイズ（パケットの個数）だけ処理を繰り返す
                    '--------------------
                    For j = 0 To g_data_size - 1
                        text_string += String.Format(" {0:X8}", g_sampling_buffer(i * g_data_size + j))
                    Next j
                    '----------------------------------------
                    ' サンプリング開始データを表示
                    '----------------------------------------
                    ListBox_SamplingData.Items.Add(text_string)
                Next i
            End If
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：正常終了"
        End If
        '----------------------------------------
        ' オーバーフローイベント
        ' ステータスの表示を行う
        '----------------------------------------
        If (m.Msg = AIOM_AIE_OFERR) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "オーバーフロー発生処理：AioGetAiStatus = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' ステータスの表示
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' 動作中ステータス有効時
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "動作中"
            '--------------------
            ' 動作中ステータス無効時
            '--------------------
            Else
                text_string += "停止中"
            End If
            '--------------------
            ' 開始トリガ待ちステータス有効時
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", 開始トリガ待ち"
            End If
            '--------------------
            ' 指定サンプリング回数格納ステータス有効時
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", 指定回数格納"
            End If
            '--------------------
            ' オーバーフローステータス有効時
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", オーバーフロー"
            End If
            '--------------------
            ' サンプリングクロックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", サンプリングクロックエラー"
            End If
            '--------------------
            ' AD変換エラーステータス有効時
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD変換エラー"
            End If
            '--------------------
            ' ドライバスペックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", ドライバスペックエラー"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            TextBox_ReturnCode.Text = "オーバーフロー発生処理：正常終了"
        End If
        '----------------------------------------
        ' サンプリングクロックエラーイベント
        ' ステータスの表示を行う
        '----------------------------------------
        If (m.Msg = AIOM_AIE_SCERR) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：AioGetAiStatus = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' ステータスの表示
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' 動作中ステータス有効時
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "動作中"
            '--------------------
            ' 動作中ステータス無効時
            '--------------------
            Else
                text_string += "停止中"
            End If
            '--------------------
            ' 開始トリガ待ちステータス有効時
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", 開始トリガ待ち"
            End If
            '--------------------
            ' 指定サンプリング回数格納ステータス有効時
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", 指定回数格納"
            End If
            '--------------------
            ' オーバーフローステータス有効時
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", オーバーフロー"
            End If
            '--------------------
            ' サンプリングクロックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", サンプリングクロックエラー"
            End If
            '--------------------
            ' AD変換エラーステータス有効時
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD変換エラー"
            End If
            '--------------------
            ' ドライバスペックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", ドライバスペックエラー"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            TextBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：正常終了"
        End If
        '----------------------------------------
        ' AD変換エラーイベント
        ' ステータスの表示を行う
        '----------------------------------------
        If (m.Msg = AIOM_AIE_ADERR) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "AD変換エラー発生処理：AioGetAiStatus = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' ステータスの表示
            '----------------------------------------
            text_string = String.Format("{0:x8}H (", ai_status)
            '--------------------
            ' 動作中ステータス有効時
            '--------------------
            If (ai_status And AIS_BUSY) = AIS_BUSY Then
                text_string += "動作中"
            '--------------------
            ' 動作中ステータス無効時
            '--------------------
            Else
                text_string += "停止中"
            End If
            '--------------------
            ' 開始トリガ待ちステータス有効時
            '--------------------
            If (ai_status And AIS_START_TRG) = AIS_START_TRG Then
                text_string += ", 開始トリガ待ち"
            End If
            '--------------------
            ' 指定サンプリング回数格納ステータス有効時
            '--------------------
            If (ai_status And AIS_DATA_NUM) = AIS_DATA_NUM Then
                text_string += ", 指定回数格納"
            End If
            '--------------------
            ' オーバーフローステータス有効時
            '--------------------
            If (ai_status And AIS_OFERR) = AIS_OFERR Then
                text_string += ", オーバーフロー"
            End If
            '--------------------
            ' サンプリングクロックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_SCERR) = AIS_SCERR Then
                text_string += ", サンプリングクロックエラー"
            End If
            '--------------------
            ' AD変換エラーステータス有効時
            '--------------------
            If (ai_status And AIS_AIERR) = AIS_AIERR Then
                text_string += ", AD変換エラー"
            End If
            '--------------------
            ' ドライバスペックエラーステータス有効時
            '--------------------
            If (ai_status And AIS_DRVERR) = AIS_DRVERR Then
                text_string += ", ドライバスペックエラー"
            End If
            text_string += ")"
            TextBox_Status.Text = text_string
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            TextBox_ReturnCode.Text = "AD変換エラー発生処理：正常終了"
        End If
        MyBase.WndProc(m)
    End Sub
End Class
