﻿using System.Runtime.InteropServices;
using System.Diagnostics;
using CaioCs;

namespace AiSamplingInfiniteUser
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // クラスCaioインスタンス作成
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int MAX_AI_RANGE_NUM = 32;    // アナログ入力レンジ配列最大要素数

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public static short g_id;                                                                  // デバイスアクセス用ID
        public int []       g_sampling_buffer;                                                     // ユーザーバッファ
        public int          g_sampling_buffer_size;                                                // ユーザーバッファサイズ
        public long         g_prev_count;                                                          // 前回位置
        public long         g_prev_lap;                                                            // 前回上書き回数
        public string       g_file_name = "SamplingData.csv";                                      // サンプリングデータ出力ファイル名
        public GCHandle     gCh;                                                                   // g_sampling_buffer配列用GCHandle
        public IntPtr       pBuffer;                                                               // g_sampling_buffer配列用IntPtr
        public short[]      g_ai_range = new short[MAX_AI_RANGE_NUM]{(short)CaioConst.PM10,        // ±10V
                                                                     (short)CaioConst.PM5,         // ±5V
                                                                     (short)CaioConst.PM25,        // ±2.5V
                                                                     (short)CaioConst.PM125,       // ±1.25V
                                                                     (short)CaioConst.PM1,         // ±1V
                                                                     (short)CaioConst.PM0625,      // ±0.625V
                                                                     (short)CaioConst.PM05,        // ±0.5V
                                                                     (short)CaioConst.PM03125,     // ±0.3125V
                                                                     (short)CaioConst.PM025,       // ±0.25V
                                                                     (short)CaioConst.PM0125,      // ±0.125V
                                                                     (short)CaioConst.PM01,        // ±0.1V
                                                                     (short)CaioConst.PM005,       // ±0.05V
                                                                     (short)CaioConst.PM0025,      // ±0.025V
                                                                     (short)CaioConst.PM00125,     // ±0.0125V
                                                                     (short)CaioConst.PM001,       // ±0.01V
                                                                     (short)CaioConst.P10,         // 0～10V
                                                                     (short)CaioConst.P5,          // 0～5V
                                                                     (short)CaioConst.P4095,       // 0～4.095V
                                                                     (short)CaioConst.P25,         // 0～2.5V
                                                                     (short)CaioConst.P125,        // 0～1.25V
                                                                     (short)CaioConst.P1,          // 0～1V
                                                                     (short)CaioConst.P05,         // 0～0.5V
                                                                     (short)CaioConst.P025,        // 0～0.25V
                                                                     (short)CaioConst.P01,         // 0～0.1V
                                                                     (short)CaioConst.P005,        // 0～0.05V
                                                                     (short)CaioConst.P0025,       // 0～0.025V
                                                                     (short)CaioConst.P00125,      // 0～0.0125V
                                                                     (short)CaioConst.P001,        // 0～0.01V
                                                                     (short)CaioConst.P20MA,       // 0～20mA
                                                                     (short)CaioConst.P4TO20MA,    // 4～20mA
                                                                     (short)CaioConst.PM20MA,      // ±20mA
                                                                     (short)CaioConst.P1TO5};      // 1～5V

        public Form1()
        {
            InitializeComponent();
            g_sampling_buffer = [];
        }

        private void AiSamplingInfiniteUser_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // アナログ入力レンジ用コンボボックスの作成
            //----------------------------------------
            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;
            //----------------------------------------
            // アナログ入力方式用コンボボックスの作成
            //----------------------------------------
            comboBox_InpMethod.Items.Add("シングルエンド入力");
            comboBox_InpMethod.Items.Add("差動入力");
            //----------------------------------------
            // アナログ入力方式用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_InpMethod.SelectedIndex = 0;
            //----------------------------------------
            // アナログ入力のチャネル数の初期表示を更新
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // サンプリング回数の初期表示を更新
            //----------------------------------------
            textBox_SamplingTimes.Text = "2000";
            //----------------------------------------
            // データ転送回数の初期表示を更新
            //----------------------------------------
            textBox_TransferTimes.Text = "1000";
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_id                   = 0;
            g_sampling_buffer_size = 0;
        }

        //================================================================================
        // AioInitボタンをクリックした際の処理になります
        // デバイスの初期化処理を行います
        //================================================================================
        private void button_AioInit_Click(object sender, EventArgs e)
        {
            string device_name;     // デバイス名
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイス名をテキストボックスから取得
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // デバイス初期化
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.Init(device_name, out g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Init = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioResetDeviceボタンをクリックした際の処理になります
        // デバイスのリセット処理を行います
        //================================================================================
        private void button_AioResetDev_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // デバイスをリセット
            //----------------------------------------
            ret = aio.ResetDevice(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSetAiRangeAllボタンをクリックした際の処理になります
        // アナログ入力の入力レンジの設定を行います
        //================================================================================
        private void button_SetAiRangeAll_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            int    index;           // コンボボックスのインデックス

            //----------------------------------------
            // GUIの情報を更新
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // コンボボックスからレンジ設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_RangeValue.SelectedIndex;
            //----------------------------------------
            // アナログ入力レンジの設定(全チャネル)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_id, g_ai_range[index]);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiRangeAll = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSetAiInputMethodボタンをクリックした際の処理になります
        // アナログ入力の入力方式の設定を行います
        //================================================================================
        private void button_SetAiInpMethod_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            int    input_method;    // 入力方式

            //----------------------------------------
            // GUIの情報を更新
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // コンボボックスから入力方式設定値を取得
            //----------------------------------------
            input_method = comboBox_InpMethod.SelectedIndex;
            //----------------------------------------
            // アナログ入力方式の設定
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_id, (short)input_method);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiInputMethod = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // AioSetAiChannelsボタンをクリックした際の処理になります
        // サンプリングに使用するアナログ入力チャネル数の設定を行います
        //================================================================================
        private void button_SetAiChannels_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            short  ai_channels;     // チャネル数

            //----------------------------------------
            // テキストボックスからチャネル数を取得
            //----------------------------------------
            if (short.TryParse(textBox_ChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // 変換に使用するアナログ入力チャネル数を設定
            //----------------------------------------
            ret = aio.SetAiChannels(g_id, ai_channels);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiChannels = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 条件設定...ボタンをクリックした際の処理になります
        // 条件設定ダイアログを開きます
        //================================================================================
        private void button_ConditionSettings_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 条件設定ダイアログを開く
            //----------------------------------------
            Condition condition_dlg = new Condition();
            if (condition_dlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // イベントにAD変換開始条件成立イベント、デバイス動作終了イベント、
                // 指定転送数毎イベント、オーバーフローイベント、
                // サンプリングクロックエラーイベント、AD変換エラーイベントを設定
                // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
                //----------------------------------------
                ret = aio.SetAiEvent(g_id, Handle, (int)(CaioConst.AIE_START | CaioConst.AIE_END | CaioConst.AIE_DATA_TSF | CaioConst.AIE_OFERR | CaioConst.AIE_SCERR | CaioConst.AIE_ADERR));
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "条件設定：aio.SetAiEvent = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // 条件設定完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "条件設定：正常終了";
            }
            condition_dlg.Dispose();
        }

        //================================================================================
        // ユーザーバッファサイズ設定ボタンをクリックした際の処理になります
        // 変換データを格納するユーザーバッファの設定を行います
        //================================================================================
        private void button_UserBufferSettings_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            short  data_size;         // データサイズ（パケットの個数）
            long   sampling_times;    // サンプリング回数

            //----------------------------------------
            // アナログ入力の変換データ転送方式をユーザーバッファモードに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiTransferMode(g_id, 1);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "ユーザーバッファサイズ設定：aio.SetAiTransferMode = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ユーザーバッファモードでの転送時に、１サンプリングあたりで必要となるデータサイズを取得
            //----------------------------------------
            ret = aio.GetAiSamplingDataSize(g_id, out data_size);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "ユーザーバッファサイズ設定：aio.GetAiSamplingDataSize = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // テキストボックスからサンプリング回数を取得
            //----------------------------------------
            if (long.TryParse(textBox_SamplingTimes.Text, out sampling_times) == false)
            {
                sampling_times = 2000;
            }
            //----------------------------------------
            // 変換データを格納するユーザーバッファを確保
            //----------------------------------------
            if (g_sampling_buffer != null)
            {
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh.IsAllocated == true)
                {
                    gCh.Free();
                }
            }
            //--------------------
            // ユーザーバッファサイズ：1サンプリングに必要なパケット数×サンプリング回数
            //--------------------
            g_sampling_buffer_size = (int)(data_size * sampling_times);
            try
            {
                g_sampling_buffer = new int[g_sampling_buffer_size];
            }
            catch
            {
                textBox_ReturnCode.Text = "ユーザーバッファサイズ設定：ユーザーバッファ確保に失敗";
                return;
            }
            //----------------------------------------
            // PinnedハンドルをgChに分配する
            //----------------------------------------
            if (gCh.IsAllocated == false)
            {
                gCh = GCHandle.Alloc(g_sampling_buffer, GCHandleType.Pinned);
            }
            //----------------------------------------
            // Pinnedハンドルでオブジェクトのアドレスを取得
            //----------------------------------------
            pBuffer = gCh.AddrOfPinnedObject();
            //----------------------------------------
            // ユーザーバッファの設定
            //----------------------------------------
            ret = aio.SetAiTransferData(g_id, g_sampling_buffer_size, pBuffer);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "ユーザーバッファサイズ設定：aio.SetAiTransferData = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ユーザーバッファ設定完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "ユーザーバッファサイズ設定：正常終了";
        }

        //================================================================================
        // AioSetAiEventTransferTimesボタンをクリックした際の処理になります
        // 指定転送数毎イベントを使用する場合のデータ転送数の設定を行います
        //================================================================================
        private void button_SetAiEventTransferTimes_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            int    transfer_times;    // データ転送回数

            //----------------------------------------
            // テキストボックスからデータ転送回数を取得
            //----------------------------------------
            if (int.TryParse(textBox_TransferTimes.Text, out transfer_times) == false)
            {
                transfer_times = 1000;
            }
            //----------------------------------------
            // 指定サンプリング回数格納イベントを使用する場合のサンプリング数を設定
            //----------------------------------------
            ret = aio.SetAiEventTransferTimes(g_id, transfer_times);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAiEventTransferTimes = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // サンプリング開始ボタンをクリックした際の処理になります
        // メモリとステータスのクリアを行った後、サンプリング開始を行います
        //================================================================================
        private void button_SamplingStart_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            int    ai_status;       // アナログ入力ステータス

            //----------------------------------------
            // ステータスクリア
            //----------------------------------------
            ret = aio.ResetAiStatus(g_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "サンプリング開始：aio.ResetAiStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 転送回数を初期化
            //----------------------------------------
            g_prev_count = 0;
            g_prev_lap   = 0;
            //----------------------------------------
            // データファイル削除
            //----------------------------------------
            File.Delete(g_file_name);
            //----------------------------------------
            // サンプリング開始
            //----------------------------------------
            ret = aio.StartAi(g_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "サンプリング開始：aio.StartAi = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスを取得
            //----------------------------------------
            ret = aio.GetAiStatus(g_id, out ai_status);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "サンプリング開始：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスの表示
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", ai_status);
            //--------------------
            // 動作中ステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
            {
                text_string += "動作中";
            }
            //--------------------
            // 動作中ステータス無効時
            //--------------------
            else
            {
                text_string += "停止中";
            }
            //--------------------
            // 開始トリガ待ちステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
            {
                text_string += ", 開始トリガ待ち";
            }
            //--------------------
            // 指定サンプリング回数格納ステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
            {
                text_string += ", 指定回数格納";
            }
            //--------------------
            // オーバーフローステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
            {
                text_string += ", オーバーフロー";
            }
            //--------------------
            // サンプリングクロックエラーステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
            {
                text_string += ", サンプリングクロックエラー";
            }
            //--------------------
            // AD変換エラーステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
            {
                text_string += ", AD変換エラー";
            }
            //--------------------
            // ドライバスペックエラーステータス有効時
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
            {
                text_string += ", ドライバスペックエラー";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // 転送回数を更新
            //----------------------------------------
            textBox_TotalSamplingPosition.Text = "0";
            //----------------------------------------
            // 上書き回数を更新
            //----------------------------------------
            textBox_OverwriteCount.Text = "0";
            //----------------------------------------
            // サンプリング開始完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "サンプリング開始：正常終了";
        }

        //================================================================================
        // AioGetAiTransferCountボタンをクリックした際の処理になります
        // 転送回数を取得しダイアログ上に表示します
        //================================================================================
        private void button_GetAiTransferCount_Click(object sender, EventArgs e)
        {
            string error_string;         // エラーコード文字列
            int    ret;                  // 戻り値取得用
            int    ai_transfer_count;    // 転送回数

            //----------------------------------------
            // ユーザーメモリに転送されたデータ数を取得
            //----------------------------------------
            ret = aio.GetAiTransferCount(g_id, out ai_transfer_count);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAiTransferCount = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // 転送されたデータ数取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // 転送回数を更新
                //--------------------
                textBox_TotalSamplingPosition.Text = ai_transfer_count.ToString();
            }
        }

        //================================================================================
        // AioGetAiTransferLapボタンをクリックした際の処理になります
        // 上書き回数を取得しダイアログ上に表示します
        //================================================================================
        private void button_GetAiTransferLap_Click(object sender, EventArgs e)
        {
            string error_string;       // エラーコード文字列
            int    ret;                // 戻り値取得用
            int    ai_transfer_lap;    // 上書き回数

            //----------------------------------------
            // ユーザーバッファを上書きありで使用する場合に、上書きされた回数を取得
            //----------------------------------------
            ret = aio.GetAiTransferLap(g_id, out ai_transfer_lap);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAiTransferLap = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // 上書き回数取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // 上書き回数を更新
                //--------------------
                textBox_OverwriteCount.Text = ai_transfer_lap.ToString();
            }
        }

        //================================================================================
        // AioGetAiStatusボタンをクリックした際の処理になります
        // アナログ入力ステータスを取得しダイアログ上に表示します
        //================================================================================
        private void button_GetAiStatus_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            int    ai_status;       // ステータス

            //----------------------------------------
            // ステータスを取得
            //----------------------------------------
            ret = aio.GetAiStatus(g_id, out ai_status);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // ステータス取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
            }
        }

        //================================================================================
        // サンプリング停止ボタンをクリックした際の処理になります
        // サンプリング停止コマンドを実行します
        //================================================================================
        private void button_SamplingStop_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // サンプリング停止
            //----------------------------------------
            ret = aio.StopAi(g_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "サンプリング停止：aio.StopAi = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // サンプリング停止完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "サンプリング停止：正常終了";
        }

        //================================================================================
        // AioExitボタンをクリックした際の処理になります
        // デバイスの終了処理を行います
        //================================================================================
        private void button_AioExit_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // 変換データを格納するユーザーバッファを解放
            //----------------------------------------
            if (g_sampling_buffer != null)
            {
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh.IsAllocated == true)
                {
                    gCh.Free();
                }
            }
        }

        //================================================================================
        // 閉じるをクリックした際の処理になります
        // デバイスの終了処理を行い、アプリケーションを終了します
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // 変換データを格納するユーザーバッファを解放
            //----------------------------------------  
            if (g_sampling_buffer != null)
            {
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh.IsAllocated == true)
                {
                    gCh.Free();
                }
            }
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // イベントが発生した時の動作
        // 各イベントが発生したとき、イベント毎に処理を行います
        //================================================================================
        protected override void WndProc(ref Message m)
        {
            string       error_string;         // エラーコード文字列
            string       text_string;          // テキストボックス更新用
            int          ret;                  // 戻り値取得用
            int          ai_transfer_count;    // 転送回数
            int          ai_transfer_lap;      // 上書き回数
            int          ai_status;            // ステータス
            StreamWriter fp;                   // ファイルポインタ
            long         i;                    // ループカウント

            //----------------------------------------
            // AD変換開始条件成立イベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_START)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAiStatus(g_id, out ai_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "AD変換開始条件成立イベント処理：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "AD変換開始条件成立イベント処理：正常終了";
            }
            //----------------------------------------
            // 指定転送数毎イベント
            // データの取得、表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_DATA_TSF)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio. GetAiStatus(g_id, out ai_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "指定転送数毎イベント処理：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // ユーザーメモリに転送されたデータ数を取得
                //----------------------------------------
                ret = aio.GetAiTransferCount(g_id, out ai_transfer_count);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "指定転送数毎イベント処理：aio.GetAiTransferCount = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // 転送位置を表示
                //----------------------------------------
                textBox_TotalSamplingPosition.Text = ai_transfer_count.ToString();
                //----------------------------------------
                // ユーザーバッファを上書きありで使用する場合に、上書きされた回数を取得
                //----------------------------------------
                ret = aio.GetAiTransferLap(g_id, out ai_transfer_lap);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "指定転送数毎イベント処理：aio.GetAiTransferLap = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // 上書き回数を表示
                //----------------------------------------
                textBox_OverwriteCount.Text = ai_transfer_lap.ToString();
                //----------------------------------------
                // オーバーランエラーをチェック
                //----------------------------------------
                if ((ai_transfer_lap - g_prev_lap >= 2) || ((ai_transfer_lap - g_prev_lap == 1) && (ai_transfer_count >= g_prev_count)))
                {
                    textBox_ReturnCode.Text = string.Format("指定転送数毎イベント処理：Overrun Error!(count = {0:D})", (g_sampling_buffer_size * ai_transfer_lap) + ai_transfer_count);
                    return;
                }
                //----------------------------------------
                // サンプリングデータをファイルに出力
                //----------------------------------------
                //--------------------
                // ファイルを開く
                //--------------------
                try
                {
                    fp = File.AppendText(g_file_name);
                }
                catch
                {
                    textBox_ReturnCode.Text = "指定転送数毎イベント処理：File.AppendText = file open error.";
                    return;
                }
                //--------------------
                // 転送位置が前回位置よりも大きい場合
                //--------------------
                if (ai_transfer_count > g_prev_count)
                {
                    //--------------------
                    // 前回位置から転送位置まで処理を繰り返す
                    //--------------------
                    for (i = g_prev_count; i < ai_transfer_count; i++)
                    {
                        text_string = "";
                        //--------------------
                        // 表示用文字列の生成
                        //--------------------
                        text_string += string.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
                        text_string += string.Format(" {0:X8}\r\n", g_sampling_buffer[i]);
                        //--------------------
                        // データ書き込み
                        //--------------------
                        try
                        {
                            fp.Write(text_string);
                        }
                        catch
                        {
                            textBox_ReturnCode.Text = "指定転送数毎イベント処理：fp.Write = file write error.";
                            fp.Close();
                            return;
                        }
                    }
                }
                //--------------------
                // 転送位置が前回位置よりも小さい場合
                //--------------------
                else
                {
                    //--------------------
                    // 前回位置からユーザーバッファサイズまで処理を繰り返す
                    //--------------------
                    for (i = g_prev_count; i < g_sampling_buffer_size; i++)
                    {
                        text_string = "";
                        //--------------------
                        // 表示用文字列の生成
                        //--------------------
                        text_string += string.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * g_prev_lap));
                        text_string += string.Format(" {0:X8}\r\n", g_sampling_buffer[i]);
                        //--------------------
                        // データ書き込み
                        //--------------------
                        try
                        {
                            fp.Write(text_string);
                        }
                        catch
                        {
                            textBox_ReturnCode.Text = "指定転送数毎イベント処理：fp.Write = file write error.";
                            fp.Close();
                            return;
                        }
                    }
                    //--------------------
                    // 0から転送位置まで処理を繰り返す
                    //--------------------
                    for (i = 0; i < ai_transfer_count; i++)
                    {
                        text_string = "";
                        //--------------------
                        // 表示用文字列の生成
                        //--------------------
                        text_string += string.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
                        text_string += string.Format(" {0:X8}\r\n", g_sampling_buffer[i]);
                        //--------------------
                        // データ書き込み
                        //--------------------
                        try
                        {
                            fp.Write(text_string);
                        }
                        catch
                        {
                            textBox_ReturnCode.Text = "指定転送数毎イベント処理：fp.Write = file write error.";
                            fp.Close();
                            return;
                        }
                    }
                }
                //--------------------
                // ファイルを閉じる
                //--------------------
                fp.Close();
                //----------------------------------------
                // 前回位置を更新
                //----------------------------------------
                g_prev_count = ai_transfer_count;
                g_prev_lap   = ai_transfer_lap;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "指定転送数毎イベント処理：正常終了";
            }
            //----------------------------------------
            // デバイス動作終了イベント
            // データの取得、表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_END)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAiStatus(g_id, out ai_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // ユーザーメモリに転送されたデータ数を取得
                //----------------------------------------
                ret = aio.GetAiTransferCount(g_id, out ai_transfer_count);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiTransferCount = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // 転送位置を表示
                //----------------------------------------
                textBox_TotalSamplingPosition.Text = ai_transfer_count.ToString();
                //----------------------------------------
                // ユーザーバッファを上書きありで使用する場合に、上書きされた回数を取得
                //----------------------------------------
                ret = aio.GetAiTransferLap(g_id, out ai_transfer_lap);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiTransferLap = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // 上書き回数を表示
                //----------------------------------------
                textBox_OverwriteCount.Text = ai_transfer_lap.ToString();
                //----------------------------------------
                // オーバーランエラーをチェック
                //----------------------------------------
                if ((ai_transfer_lap - g_prev_lap >= 2) || ((ai_transfer_lap - g_prev_lap == 1) && (ai_transfer_count >= g_prev_count)))
                {
                    textBox_ReturnCode.Text = string.Format("デバイス動作終了イベント処理：Overrun Error!(count = {0:D})", (g_sampling_buffer_size * ai_transfer_lap) + ai_transfer_count);
                    return;
                }
                //----------------------------------------
                // サンプリングデータをファイルに出力
                //----------------------------------------
                //--------------------
                // ファイルを開く
                //--------------------
                try
                {
                    fp = File.AppendText(g_file_name);
                }
                catch
                {
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：File.AppendText = file open error.";
                    return;
                }
                //--------------------
                // 転送位置が前回位置よりも大きい場
                //--------------------
                if (ai_transfer_count > g_prev_count)
                {
                    //--------------------
                    // 前回位置から転送位置まで処理を繰り返す
                    //--------------------
                    for (i = g_prev_count; i < ai_transfer_count; i++)
                    {
                        text_string = "";
                        //--------------------
                        // 表示用文字列の生成
                        //--------------------
                        text_string += string.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
                        text_string += string.Format(" {0:X8}\r\n", g_sampling_buffer[i]);
                        //--------------------
                        // データ書き込み
                        //--------------------
                        try
                        {
                            fp.Write(text_string);
                        }
                        catch
                        {
                            textBox_ReturnCode.Text = "デバイス動作終了イベント処理：fp.Write = file write error.";
                            fp.Close();
                            return;
                        }
                    }
                }
                //--------------------
                // 転送位置が前回位置よりも小さい場合
                //--------------------
                else
                {
                    //--------------------
                    // 前回位置からユーザーバッファサイズまで処理を繰り返す
                    //--------------------
                    for (i = g_prev_count; i < g_sampling_buffer_size; i++)
                    {
                        text_string = "";
                        //--------------------
                        // 表示用文字列の生成
                        //--------------------
                        text_string += string.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * g_prev_lap));
                        text_string += string.Format(" {0:X8}\r\n", g_sampling_buffer[i]);
                        //--------------------
                        // データ書き込み
                        //--------------------
                        try
                        {
                            fp.Write(text_string);
                        }
                        catch
                        {
                            textBox_ReturnCode.Text = "デバイス動作終了イベント処理：fp.Write = file write error.";
                            fp.Close();
                            return;
                        }
                    }
                    //--------------------
                    // 0から転送位置まで処理を繰り返す
                    //--------------------
                    for (i = 0; i < ai_transfer_count; i++)
                    {
                        text_string = "";
                        //--------------------
                        // 表示用文字列の生成
                        //--------------------
                        text_string += string.Format("{0,5},", (i + 1) + (g_sampling_buffer_size * ai_transfer_lap));
                        text_string += string.Format(" {0:X8}\r\n", g_sampling_buffer[i]);
                        //--------------------
                        // データ書き込み
                        //--------------------
                        try
                        {
                            fp.Write(text_string);
                        }
                        catch
                        {
                            textBox_ReturnCode.Text = "デバイス動作終了イベント処理：fp.Write = file write error.";
                            fp.Close();
                            return;
                        }
                    }
                }
                //--------------------
                // ファイルを閉じる
                //--------------------
                fp.Close();
                //----------------------------------------
                // 前回位置を更新
                //----------------------------------------
                g_prev_count = ai_transfer_count;
                g_prev_lap   = ai_transfer_lap;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "デバイス動作終了イベント処理：正常終了";
            }
            //----------------------------------------
            // オーバーフローイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_OFERR)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAiStatus(g_id, out ai_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "オーバーフロー発生処理：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "オーバーフロー発生処理：正常終了";
            }
            //----------------------------------------
            // サンプリングクロックエラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_SCERR)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAiStatus(g_id, out ai_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：正常終了";
            }
            //----------------------------------------
            // AD変換エラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_ADERR)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAiStatus(g_id, out ai_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "AD変換エラー発生処理：aio.GetAiStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ai_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数格納ステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                {
                    text_string += ", 指定回数格納";
                }
                //--------------------
                // オーバーフローステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                {
                    text_string += ", オーバーフロー";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // AD変換エラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                {
                    text_string += ", AD変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "AD変換エラー発生処理：正常終了";
            }
            base.WndProc(ref m);
        }
    }
}