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

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

    '----------------------------------------
    ' グローバル変数の宣言
    '----------------------------------------
    Dim g_id As Short                                                         ' デバイスアクセス用ID
    Dim g_sampling_count As Long                                              ' 総サンプリング回数
    Dim g_sampling_data() As Single = Nothing                                 ' サンプリングデータ格納領域のポインタ
    Dim gCh_sampling_data As GCHandle                                         ' g_sampling_data配列用GCHandle
    Dim gCh As GCHandle                                                       ' コールバックルーチン用GCHandle
    Dim pdelegate_func As PAICALLBACK                                         ' イベント通知用デリゲート
    Dim pfunc As IntPtr                                                       ' コールバックルーチン用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 AiSamplingOnceDevCallback_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"
        '----------------------------------------
        ' アナログ入力レンジ用コンボボックスの作成
        '----------------------------------------
        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_sampling_count = 0
        '----------------------------------------
        ' イベント通知用デリゲート初期化
        '----------------------------------------
        pdelegate_func = New PAICALLBACK(AddressOf CallBackProc)
        '----------------------------------------
        ' デリゲートが開放されないように設定します。
        '----------------------------------------
        If (gCh.IsAllocated = False) Then
            gCh = GCHandle.Alloc(pdelegate_func)
        End If
    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

    '================================================================================
    ' 基本設定ボタンをクリックした際の処理になります
    ' サンプリングに必要な基本設定を行います
    ' (基本設定は初期化処理/リセット処理実行後、初回のサンプリング開始前に実行する必要があります)
    '================================================================================
    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 = AioSetAiTransferMode(g_id, 0)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiTransferMode = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' データ格納用メモリ形式をFIFOに設定
        ' 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        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
        '----------------------------------------
        ' イベント通知用デリゲートの固定ポインタを取得
        '----------------------------------------
        pfunc = Marshal.GetFunctionPointerForDelegate(pdelegate_func)
        '----------------------------------------
        ' コールバックルーチンの設定：デバイス動作終了イベント要因、オーバーフローイベント要因、
        ' サンプリングクロックエラーイベント要因、AD変換エラーイベント要因
        ' 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
        '----------------------------------------
        ret = AioSetAiCallBackProc(g_id, pfunc, AIE_END Or AIE_OFERR Or AIE_SCERR Or AIE_ADERR, Nothing)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "基本設定：AioSetAiCallBackProc = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' Zシリーズにおいて、バスマスタ用バッファが最大1MByte以下に制限される場合があります
        ' 実システムでは、必要に応じてバッファサイズの変更処理を実装してください
        '----------------------------------------
        'ret = AioSetAiMemorySize(Id, 750 * 1024)
        '----------------------------------------
        ' 基本設定完了のメッセージ表示
        '----------------------------------------
        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 = AioResetAiMemory(g_id)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング開始：AioResetAiMemory = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' ステータスクリア
        '----------------------------------------
        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_sampling_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_SamplingCount.Text = "0"
        '----------------------------------------
        ' サンプリングデータ表示用ListBoxをクリア
        '----------------------------------------
        ListBox_SamplingData.Items.Clear()
        '----------------------------------------
        ' サンプリング開始完了のメッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "サンプリング開始：正常終了"
    End Sub

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

        '----------------------------------------
        ' サンプリング回数を取得
        '----------------------------------------
        ret = AioGetAiSamplingCount(g_id, ai_sampling_count)
        '----------------------------------------
        ' エラー文字列取得
        '----------------------------------------
        AioGetErrorString(ret, error_string)
        '----------------------------------------
        ' メッセージ表示
        '----------------------------------------
        TextBox_ReturnCode.Text = "AioGetAiSamplingCount = " & ret & "：" & error_string.ToString()
        '----------------------------------------
        ' サンプリング回数取得関数が正常終了した場合
        '----------------------------------------
        If (ret = 0) Then
            '--------------------
            ' サンプリング回数を更新
            '--------------------
            TextBox_SamplingCount.Text = ai_sampling_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 text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_sampling_count As Integer                  ' サンプリングカウント
        Dim ai_status As Integer                          ' ステータス
        Dim i, j As Long                                  ' ループカウント
        Dim loop_max_count                                ' ループカウント最大値
        Dim top_address As Long                           ' サンプリングデータ表示のバッファの先頭アドレス
        Dim ai_channels As Short                          ' チャネル数

        '----------------------------------------
        ' サンプリング停止
        '----------------------------------------
        ret = AioStopAi(g_id)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング停止：AioStopAi = " & 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
        '----------------------------------------
        ' 現在のサンプリング回数を取得
        '----------------------------------------
        ret = AioGetAiSamplingCount(g_id, ai_sampling_count)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "サンプリング停止：AioGetAiSamplingCount = " & ret & "：" & error_string.ToString()
            Exit Sub
        End If
        '----------------------------------------
        ' サンプリング回数を表示
        '----------------------------------------
        TextBox_SamplingCount.Text = ai_sampling_count.ToString()
        '----------------------------------------
        ' 今回取得できたデータのサンプリング回数が1以上の場合
        '----------------------------------------
        If (ai_sampling_count > 0) Then
            '----------------------------------------
            ' チャネル数取得
            '----------------------------------------
            ret = AioGetAiChannels(g_id, ai_channels)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "サンプリング停止：AioGetAiChannels = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' サンプリングデータ取得処理
            '----------------------------------------
            '--------------------
            ' データ格納用の配列を確保
            '--------------------
            If IsArray(g_sampling_data) = True Then
                '----------------------------------------
                ' GCHandleの解放
                '----------------------------------------
                If (gCh_sampling_data.IsAllocated = True) Then
                     gCh_sampling_data.Free()
                End If
                Erase g_sampling_data
            End If
            Try
                ReDim g_sampling_data(ai_sampling_count * ai_channels - 1)
            Catch ex As Exception
                TextBox_ReturnCode.Text = "サンプリング停止：データ格納用の配列確保に失敗"
                Exit Sub
            End Try
            '----------------------------------------
            ' PinnedハンドルをgChに分配する
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = False) Then
                gCh_sampling_data = GCHandle.Alloc(g_sampling_data, GCHandleType.Pinned)
            End If
            '----------------------------------------
            ' サンプリングデータを取得
            '----------------------------------------
            ret = AioGetAiSamplingDataEx(g_id, ai_sampling_count, g_sampling_data)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                TextBox_ReturnCode.Text = "サンプリング停止：AioGetAiSamplingDataEx = " & ret & "：" & error_string.ToString()
                Exit Sub
            End If
            '----------------------------------------
            ' 総サンプリング回数を更新
            '----------------------------------------
            g_sampling_count = ai_sampling_count
            '----------------------------------------
            ' サンプリングデータ表示ループのループ回数を設定
            '----------------------------------------
            '--------------------
            ' 今回取得できたデータのサンプリング回数がデータ表示最大サンプリング回数を超えた場合
            '--------------------
            If (ai_sampling_count > DISPLAY_SAMPLING_COUNT) Then
                '--------------------
                ' 最大ループ回数をデータ表示最大サンプリング回数に制限
                '--------------------
                loop_max_count = DISPLAY_SAMPLING_COUNT
                '--------------------
                ' 参照するバッファの先頭アドレスを取得データの末尾から
                ' データ表示最大サンプリング回数前のアドレスに設定
                '--------------------
                top_address = ai_sampling_count - DISPLAY_SAMPLING_COUNT
            '--------------------
            ' 今回取得できたデータのサンプリング回数がデータ表示最大サンプリング回数以下の場合
            '--------------------
            Else
                '--------------------
                ' 最大ループ回数に総サンプリングデータ数を設定
                '--------------------
                loop_max_count = ai_sampling_count
                '--------------------
                ' 参照するバッファの先頭アドレスを0に設定
                '--------------------
                top_address = 0
            End If
            '----------------------------------------
            ' サンプリングデータを表示
            ' (取得データの末尾からデータ表示最大サンプリング回数までを表示)
            '----------------------------------------
            '--------------------
            ' サンプリングデータ表示用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 ai_channels - 1
                    text_string += String.Format(" {0,10:F5}", g_sampling_data(i * ai_channels + j))
                Next j
                '----------------------------------------
                ' サンプリング開始データを表示
                '----------------------------------------
                ListBox_SamplingData.Items.Add(text_string)
            Next i
            '----------------------------------------
            ' サンプリング停止完了のメッセージ表示
            '----------------------------------------
            TextBox_ReturnCode.Text = "サンプリング停止：正常終了"
        '----------------------------------------
        ' 今回取得できたデータのサンプリング回数が0以下の場合
        '----------------------------------------
        Else
            '----------------------------------------
            ' サンプリング停止完了のメッセージ表示
            '----------------------------------------
            TextBox_ReturnCode.Text = "サンプリング停止：正常終了"
        End If
    End Sub

    '================================================================================
    ' 取得データファイル出力ボタンをクリックした際の処理になります
    ' グローバルで保持しているサンプリングデータをCSVファイルに出力します
    '================================================================================
    Private Sub Button_OutputSamplingData_Click(sender As Object, e As EventArgs) Handles Button_OutputSamplingData.Click
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim file_name As String = "SamplingData.csv"      ' サンプリングデータ出力ファイル名
        Dim fp As StreamWriter                            ' ファイルポインタ
        Dim i, j As Long                                  ' ループカウント
        Dim ai_channels As Short                          ' チャネル数

        '----------------------------------------
        ' ファイルを開く
        '----------------------------------------
        Try
            fp = File.CreateText(file_name)
        Catch ex As Exception
            TextBox_ReturnCode.Text = "取得データファイル出力：File.CreateText = file open error."
            Exit Sub
        End Try
        '----------------------------------------
        ' チャネル数取得
        '----------------------------------------
        ret = AioGetAiChannels(g_id, ai_channels)
        '----------------------------------------
        ' エラー処理
        '----------------------------------------
        If (ret <> 0) Then
            '--------------------
            ' エラー文字列取得
            '--------------------
            AioGetErrorString(ret, error_string)
            '--------------------
            ' メッセージ表示
            '--------------------
            TextBox_ReturnCode.Text = "取得データファイル出力：AioGetAiChannels = " & ret & "：" & error_string.ToString()
            '----------------------------------------
            ' ファイルを閉じる
            '----------------------------------------
            fp.Close()
            Exit Sub
        End If
        '----------------------------------------
        ' ファイル出力用サンプリングデータの作成
        '----------------------------------------
        '--------------------
        ' 総サンプリング回数が1以上かつサンプリングデータが有効であればファイル出力を行う
        '--------------------
        If ((g_sampling_count > 0) And (IsArray(g_sampling_data) = True)) Then
            '--------------------
            ' 総サンプリング回数まで処理を繰り返す
            '--------------------
            For i = 0 To g_sampling_count - 1
                text_string = ""
                '--------------------
                ' 表示用文字列の生成
                '--------------------
                text_string += String.Format("{0,5},", (i + 1))
                '--------------------
                ' 指定したチャネルの数だけ処理を繰り返す
                '--------------------
                For j = 0 To ai_channels - 1
                    text_string += String.Format(" {0,10:F5}", g_sampling_data(i * ai_channels + j))
                    '--------------------
                    ' 最終チャネルの場合改行文字を追加する
                    '--------------------
                    If (j = (ai_channels - 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                                ' 戻り値取得用

        '----------------------------------------
        ' AD変換データ格納用の配列を解放
        '----------------------------------------
        If IsArray(g_sampling_data) = True Then
            '----------------------------------------
            ' GCHandleの解放
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = True) Then
                 gCh_sampling_data.Free()
            End If
            Erase g_sampling_data
        End If
        '----------------------------------------
        ' イベント通知用デリゲートのガベージコレクションハンドルを開放
        '----------------------------------------
        If (gCh.IsAllocated = True) Then
            gCh.Free()
        End If
        '----------------------------------------
        ' 終了処理の実行
        '----------------------------------------
        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
        '----------------------------------------
        ' AD変換データ格納用の配列を解放
        '----------------------------------------
        If IsArray(g_sampling_data) = True Then
            '----------------------------------------
            ' GCHandleの解放
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = True) Then
                 gCh_sampling_data.Free()
            End If
            Erase g_sampling_data
        End If
        '----------------------------------------
        ' イベント通知用デリゲートのガベージコレクションハンドルを開放
        '----------------------------------------
        If (gCh.IsAllocated = True) Then
            gCh.Free()
        End If
        '----------------------------------------
        ' 終了処理の実行
        '----------------------------------------
        AioExit(g_id)
        '----------------------------------------
        ' ダイアログを終了する
        '----------------------------------------
        Me.Close()
    End Sub

    '================================================================================
    ' コールバックルーチン
    '================================================================================
    Protected Sub CallBackProc(ByVal Id As Short, ByVal Message As Short, ByVal wParam As Integer, ByVal lParam As Integer, ByVal Param As IntPtr)
        Dim error_string As New StringBuilder("", 256)    ' エラーコード文字列
        Dim text_string As String                         ' テキストボックス更新用
        Dim ret As Integer                                ' 戻り値取得用
        Dim ai_sampling_count As Integer                  ' サンプリングカウント
        Dim ai_status As Integer                          ' ステータス
        Dim i, j As Long                                  ' ループカウント
        Dim loop_max_count                                ' ループカウント最大値
        Dim top_address As Long                           ' サンプリングデータ表示のバッファの先頭アドレス
        Dim ai_channels As Short                          ' チャネル数

        '----------------------------------------
        ' デバイス動作終了イベント
        ' データの取得、表示を行う
        '----------------------------------------
        If (Message = AIOM_AIE_END) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' 現在のサンプリング回数を取得
            '----------------------------------------
            ret = AioGetAiSamplingCount(g_id, ai_sampling_count)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：AioGetAiSamplingCount = " & ret & "：" & error_string.ToString()))
                Exit Sub
            End If
            '----------------------------------------
            ' サンプリング回数を表示
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_SamplingCount.Text = ai_sampling_count.ToString()))
            '----------------------------------------
            ' チャネル数取得
            '----------------------------------------
            ret = AioGetAiChannels(g_id, ai_channels)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：AioGetAiChannels = " & ret & "：" & error_string.ToString()))
                Exit Sub
            End If
            '----------------------------------------
            ' サンプリングデータ取得処理
            '----------------------------------------
            '--------------------
            ' データ格納用の配列を確保
            '--------------------
            If IsArray(g_sampling_data) = True Then
                '----------------------------------------
                ' GCHandleの解放
                '----------------------------------------
                If (gCh_sampling_data.IsAllocated = True) Then
                     gCh_sampling_data.Free()
                End If
                Erase g_sampling_data
            End If
            Try
                ReDim g_sampling_data(ai_sampling_count * ai_channels - 1)
            Catch ex As Exception
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：データ格納用の配列確保に失敗"))
                Exit Sub
            End Try
            '----------------------------------------
            ' PinnedハンドルをgChに分配する
            '----------------------------------------
            If (gCh_sampling_data.IsAllocated = False) Then
                gCh_sampling_data = GCHandle.Alloc(g_sampling_data, GCHandleType.Pinned)
            End If
            '----------------------------------------
            ' サンプリングデータを取得
            '----------------------------------------
            ret = AioGetAiSamplingDataEx(g_id, ai_sampling_count, g_sampling_data)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：AioGetAiSamplingDataEx = " & ret & "：" & error_string.ToString()))
                Exit Sub
            End If
            '----------------------------------------
            ' 総サンプリング回数を更新
            '----------------------------------------
            g_sampling_count = ai_sampling_count
            '----------------------------------------
            ' サンプリングデータ表示ループのループ回数を設定
            '----------------------------------------
            '--------------------
            ' 今回取得できたデータのサンプリング回数がデータ表示最大サンプリング回数を超えた場合
            '--------------------
            If (ai_sampling_count > DISPLAY_SAMPLING_COUNT) Then
                '--------------------
                ' 最大ループ回数をデータ表示最大サンプリング回数に制限
                '--------------------
                loop_max_count = DISPLAY_SAMPLING_COUNT
                '--------------------
                ' 参照するバッファの先頭アドレスを取得データの末尾から
                ' データ表示最大サンプリング回数前のアドレスに設定
                '--------------------
                top_address = ai_sampling_count - DISPLAY_SAMPLING_COUNT
            '--------------------
            ' 今回取得できたデータのサンプリング回数がデータ表示最大サンプリング回数以下の場合
            '--------------------
            Else
                '--------------------
                ' 最大ループ回数に総サンプリングデータ数を設定
                '--------------------
                loop_max_count = ai_sampling_count
                '--------------------
                ' 参照するバッファの先頭アドレスを0に設定
                '--------------------
                top_address = 0
            End If
            '----------------------------------------
            ' サンプリングデータを表示
            ' (取得データの末尾からデータ表示最大サンプリング回数までを表示)
            '----------------------------------------
            '--------------------
            ' 今回取得できたデータのサンプリング回数が1以上の場合
            '--------------------
            If (ai_sampling_count > 0) Then
                '--------------------
                ' サンプリングデータ表示用ListBoxをクリア
                '--------------------
                Me.Invoke(New Action(Sub() 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 ai_channels - 1
                        text_string += String.Format(" {0,10:F5}", g_sampling_data(i * ai_channels + j))
                    Next j
                    '----------------------------------------
                    ' サンプリング開始データを表示
                    '----------------------------------------
                    Me.Invoke(New Action(Sub() ListBox_SamplingData.Items.Add(text_string)))
                Next i
            End If
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "デバイス動作終了イベント処理：正常終了"))
        End If
        '----------------------------------------
        ' オーバーフローイベント
        ' ステータスの表示を行う
        '----------------------------------------
        If (Message = AIOM_AIE_OFERR) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "オーバーフロー発生処理：正常終了"))
        End If
        '----------------------------------------
        ' サンプリングクロックエラーイベント
        ' ステータスの表示を行う
        '----------------------------------------
        If (Message = AIOM_AIE_SCERR) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：正常終了"))
        End If
        '----------------------------------------
        ' AD変換エラーイベント
        ' ステータスの表示を行う
        '----------------------------------------
        If (Message = AIOM_AIE_ADERR) Then
            '----------------------------------------
            ' ステータスを取得
            '----------------------------------------
            ret = AioGetAiStatus(g_id, ai_status)
            '----------------------------------------
            ' エラー処理
            '----------------------------------------
            If (ret <> 0) Then
                '--------------------
                ' エラー文字列取得
                '--------------------
                AioGetErrorString(ret, error_string)
                '--------------------
                ' メッセージ表示
                '--------------------
                Me.Invoke(New Action(Sub() 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 += ")"
            Me.Invoke(New Action(Sub() TextBox_Status.Text = text_string))
            '----------------------------------------
            ' イベント処理完了のメッセージ表示
            '----------------------------------------
            Me.Invoke(New Action(Sub() TextBox_ReturnCode.Text = "AD変換エラー発生処理：正常終了"))
        End If
        Exit Sub
    End Sub
End Class
