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

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

        //----------------------------------------
        // マクロ定義
        //----------------------------------------
        public const int MAX_AI_RANGE_NUM       = 32;      // アナログ入力レンジ配列最大要素数
        public const int DISPLAY_SAMPLING_COUNT = 1000;    // ListBoxに表示するサンプリングデータの最大サンプリング回数

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short    g_master_id;                                                           // マスターデバイスアクセス用ID
        public short    g_slave_id;                                                            // スレーブデバイスアクセス用ID
        public long     g_master_sampling_count;                                               // マスター総サンプリング回数
        public long     g_slave_sampling_count;                                                // スレーブ総サンプリング回数
        public float[]  g_master_sampling_data;                                                // マスターサンプリングデータ格納領域のポインタ
        public float[]  g_slave_sampling_data;                                                 // スレーブサンプリングデータ格納領域のポインタ
        public GCHandle gCh_master_data;                                                       // g_master_sampling_data配列用GCHandle
        public GCHandle gCh_slave_data;                                                        // g_slave_sampling_data配列用GCHandle
        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_master_sampling_data = [];
            g_slave_sampling_data = [];
        }

        private void SyncBusSampling_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // 「マスター」デバイス名の初期表示を更新
            //----------------------------------------
            textBox_MasterDeviceName.Text = "AIO000";
            //----------------------------------------
            // 「マスター」アナログ入力のチャネル数の初期表示を更新
            //----------------------------------------
            textBox_MasterAiChNum.Text = "1";
            //----------------------------------------
            // 「マスター」サンプリングクロックの初期表示を更新
            //----------------------------------------
            textBox_MasterInternalClock.Text = "1000";
            //----------------------------------------
            // 「マスター」サンプリング停止回数の初期表示を更新
            //----------------------------------------
            textBox_MasterStopTimes.Text = "1000";
            //----------------------------------------
            // 「マスター」アナログ入力方式用コンボボックスの作成
            //----------------------------------------
            comboBox_MasterAiInpMethod.Items.Add("シングルエンド入力");
            comboBox_MasterAiInpMethod.Items.Add("差動入力");
            //----------------------------------------
            // 「マスター」アナログ入力方式用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_MasterAiInpMethod.SelectedIndex = 0;
            //----------------------------------------
            // 「マスター」アナログ入力レンジ用コンボボックスの作成
            //----------------------------------------
            comboBox_MasterAiRangeValue.Items.Add("-10 - +10V");
            comboBox_MasterAiRangeValue.Items.Add("-5 - +5V");
            comboBox_MasterAiRangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_MasterAiRangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_MasterAiRangeValue.Items.Add("-1 - +1V");
            comboBox_MasterAiRangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_MasterAiRangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_MasterAiRangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_MasterAiRangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_MasterAiRangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_MasterAiRangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_MasterAiRangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_MasterAiRangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_MasterAiRangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_MasterAiRangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +10V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +5V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +4.095V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +2.5V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +1.25V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +1V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.5V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.25V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.1V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.05V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.025V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.0125V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.01V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +20mA");
            comboBox_MasterAiRangeValue.Items.Add("4 - +20mA");
            comboBox_MasterAiRangeValue.Items.Add("-20mA - +20mA");
            comboBox_MasterAiRangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // 「マスター」アナログ入力レンジ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_MasterAiRangeValue.SelectedIndex = 0;
            //----------------------------------------
            // 「スレーブ」デバイス名の初期表示を更新
            //----------------------------------------
            textBox_SlaveDeviceName.Text = "AIO001";
            //----------------------------------------
            // 「スレーブ」アナログ入力のチャネル数の初期表示を更新
            //----------------------------------------
            textBox_SlaveAiChNum.Text = "1";
            //----------------------------------------
            // 「スレーブ」アナログ入力方式用コンボボックスの作成
            //----------------------------------------
            comboBox_SlaveAiInpMethod.Items.Add("シングルエンド入力");
            comboBox_SlaveAiInpMethod.Items.Add("差動入力");
            //----------------------------------------
            // 「スレーブ」アナログ入力方式用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_SlaveAiInpMethod.SelectedIndex = 0;
            //----------------------------------------
            // 「スレーブ」アナログ入力レンジ用コンボボックスの作成
            //----------------------------------------
            comboBox_SlaveAiRangeValue.Items.Add("-10 - +10V");
            comboBox_SlaveAiRangeValue.Items.Add("-5 - +5V");
            comboBox_SlaveAiRangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_SlaveAiRangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_SlaveAiRangeValue.Items.Add("-1 - +1V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +10V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +5V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +4.095V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +2.5V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +1.25V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +1V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.5V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.25V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.1V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.05V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.025V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.0125V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.01V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +20mA");
            comboBox_SlaveAiRangeValue.Items.Add("4 - +20mA");
            comboBox_SlaveAiRangeValue.Items.Add("-20mA - +20mA");
            comboBox_SlaveAiRangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // 「スレーブ」アナログ入力レンジ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_SlaveAiRangeValue.SelectedIndex = 0;
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_master_id             = 0;
            g_slave_id              = 0;
            g_master_sampling_count = 0;
            g_slave_sampling_count  = 0;
        }

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

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

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

            //----------------------------------------
            // 「マスター」デバイスをリセット
            //----------------------------------------
            ret = aio.ResetDevice(g_master_id);
            //----------------------------------------
            // 「マスター」エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // 「マスター」メッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // 「スレーブ」デバイスをリセット
            //----------------------------------------
            ret = aio.ResetDevice(g_slave_id);
            //----------------------------------------
            // 「スレーブ」エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // 「スレーブ」メッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + "：" + error_string;
        }

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

            //----------------------------------------
            // GUIの情報を更新
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // 「マスター」コンボボックスからアナログ入力レンジ設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_MasterAiRangeValue.SelectedIndex;
            //----------------------------------------
            // 「マスター」アナログ入力レンジの設定(全チャネル)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_master_id, g_ai_range[index]);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "アナログ入力設定：aio.SetAiRangeAll = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」コンボボックスから入力方式設定値を取得
            //----------------------------------------
            input_method = (short)comboBox_MasterAiInpMethod.SelectedIndex;
            //----------------------------------------
            // 「マスター」アナログ入力方式の設定
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_master_id, input_method);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "アナログ入力設定：aio.SetAiInputMethod = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」アナログ入力設定完了のメッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "アナログ入力設定：正常終了";
            //----------------------------------------
            // 「スレーブ」コンボボックスからアナログ入力レンジ設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_SlaveAiRangeValue.SelectedIndex;
            //----------------------------------------
            // 「スレーブ」アナログ入力レンジの設定(全チャネル)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_slave_id, g_ai_range[index]);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "アナログ入力設定：aio.SetAiRangeAll = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」コンボボックスから入力方式設定値を取得
            //----------------------------------------
            input_method = (short)comboBox_SlaveAiInpMethod.SelectedIndex;
            //----------------------------------------
            // 「スレーブ」アナログ入力方式の設定
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_slave_id, input_method);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "アナログ入力設定：aio.SetAiInputMethod = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」アナログ入力設定完了のメッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "アナログ入力設定：正常終了";
        }

        //================================================================================
        // サンプリング設定ボタンをクリックした際の処理になります
        // サンプリング設定を行います
        //================================================================================
        private void button_SamplingSetting_Click(object sender, EventArgs e)
        {
            string error_string;      // エラーコード文字列
            int    ret;               // 戻り値取得用
            short  ai_channels;       // アナログ入力チャネル数
            float  sampling_clock;    // 内部クロックの変換速度
            int    stop_times;        // サンプリング停止回数

            //----------------------------------------
            // 「スレーブ」テキストボックスからチャネル数を取得
            //----------------------------------------
            if(short.TryParse(textBox_SlaveAiChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // 「スレーブ」変換に使用するアナログ入力チャネル数を設定
            //----------------------------------------
            ret = aio.SetAiChannels(g_slave_id, ai_channels);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiChannels = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」アナログ入力の変換データ転送方式をデバイスバッファモードに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiTransferMode(g_slave_id, 0);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiTransferMode = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」データ格納用メモリ形式をFIFOに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiMemoryType(g_slave_id, 0);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiMemoryType = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」サンプリング開始条件をイベントコントローラ出力に設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiStartTrigger(g_slave_id, 10);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiStartTrigger = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」クロック種類をイベントコントローラ出力に設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiClockType(g_slave_id, 10);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiClockType = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」サンプリング停止条件をイベントコントローラ出力に設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiStopTrigger(g_slave_id, 10);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiStopTrigger = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」イベントにAD変換開始条件成立イベント、デバイス動作終了イベント、オーバーフローイベント、
            // サンプリングクロックエラーイベント、AD変換エラーイベントを設定
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiEvent(g_slave_id, Handle, (int)(CaioConst.AIE_START | CaioConst.AIE_END | CaioConst.AIE_OFERR | CaioConst.AIE_SCERR | CaioConst.AIE_ADERR));
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング設定：aio.SetAiEvent = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」サンプリング設定完了のメッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "サンプリング設定：正常終了";
            //----------------------------------------
            // 「マスター」テキストボックスからチャネル数を取得
            //----------------------------------------
            if (short.TryParse(textBox_MasterAiChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // 「マスター」変換に使用するアナログ入力チャネル数を設定
            //----------------------------------------
            ret = aio.SetAiChannels(g_master_id, ai_channels);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiChannels = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」アナログ入力の変換データ転送方式をデバイスバッファモードに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiTransferMode(g_master_id, 0);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiTransferMode = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」データ格納用メモリ形式をFIFOに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiMemoryType(g_master_id, 0);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiMemoryType = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」サンプリング開始条件をソフトウェアに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiStartTrigger(g_master_id, 0);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiStartTrigger = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」クロック種類を内部クロックに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiClockType(g_master_id, 0);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiClockType = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」テキストボックスから内部クロックの変換速度を取得
            //----------------------------------------
            if (float.TryParse(textBox_MasterInternalClock.Text, out sampling_clock) == false)
            {
                sampling_clock = 1000;
            }
            //----------------------------------------
            // 「マスター」内部クロックの変換速度を設定
            //----------------------------------------
            ret = aio.SetAiSamplingClock(g_master_id, sampling_clock);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiSamplingClock = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」サンプリング停止条件を設定回数変換終了に設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiStopTrigger(g_master_id, 0);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiStopTrigger = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」テキストボックスからサンプリング停止回数を取得
            //----------------------------------------
            if (int.TryParse(textBox_MasterStopTimes.Text, out stop_times) == false)
            {
                stop_times = 1000;
            }
            //----------------------------------------
            // 「マスター」サンプリング停止回数を設定
            //----------------------------------------
            ret = aio.SetAiStopTimes(g_master_id, stop_times);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiStopTimes = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」イベントにデバイス動作終了イベント、オーバーフローイベント、
            // サンプリングクロックエラーイベント、AD変換エラーイベントを設定
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiEvent(g_master_id, Handle, (int)(CaioConst.AIE_END | CaioConst.AIE_OFERR | CaioConst.AIE_SCERR | CaioConst.AIE_ADERR));
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング設定：aio.SetAiEvent = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」サンプリング設定完了のメッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "サンプリング設定：正常終了";
        }

        //================================================================================
        // 同期動作設定ボタンをクリックした際の処理になります
        // 同期動作設定を行います
        //================================================================================
        private void button_SyncSetting_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // 「マスター」イベントコントローラの設定：アナログ入力内部クロック信号を同期バスマスタ信号１に接続
            //----------------------------------------
            ret = aio.SetEcuSignal(g_master_id, (short)CaioConst.AIOECU_DEST_MASTER1, (short)CaioConst.AIOECU_SRC_AI_CLK);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "同期動作設定：aio.SetEcuSignal = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」イベントコントローラの設定：アナログ入力変換開始信号を同期バスマスタ信号２に接続
            //----------------------------------------
            ret = aio.SetEcuSignal(g_master_id, (short)CaioConst.AIOECU_DEST_MASTER2, (short)CaioConst.AIOECU_SRC_START);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "同期動作設定：aio.SetEcuSignal = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」イベントコントローラの設定：アナログ入力変換回数終了信号（遅延なし）を同期バスマスタ信号３に接続
            //----------------------------------------
            ret = aio.SetEcuSignal(g_master_id, (short)CaioConst.AIOECU_DEST_MASTER3, (short)CaioConst.AIOECU_SRC_AI_STOP);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "同期動作設定：aio.SetEcuSignal = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」同期動作設定完了のメッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "同期動作設定：正常終了";
            //----------------------------------------
            // 「スレーブ」イベントコントローラの設定：同期バススレーブ信号１をアナログ入力内部クロック信号に接続
            //----------------------------------------
            ret = aio.SetEcuSignal(g_slave_id, (short)CaioConst.AIOECU_DEST_AI_CLK, (short)CaioConst.AIOECU_SRC_SLAVE1);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "同期動作設定：aio.SetEcuSignal = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」イベントコントローラの設定：同期バススレーブ信号２をアナログ入力変換開始信号に接続
            //----------------------------------------
            ret = aio.SetEcuSignal(g_slave_id, (short)CaioConst.AIOECU_DEST_AI_START, (short)CaioConst.AIOECU_SRC_SLAVE2);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "同期動作設定：aio.SetEcuSignal = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」イベントコントローラの設定：同期バススレーブ信号３をアナログ入力変換停止信号に接続
            //----------------------------------------
            ret = aio.SetEcuSignal(g_slave_id, (short)CaioConst.AIOECU_DEST_AI_STOP, (short)CaioConst.AIOECU_SRC_SLAVE3);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "同期動作設定：aio.SetEcuSignal = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」同期動作設定完了のメッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "同期動作設定：正常終了";
        }

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

            //----------------------------------------
            // 「スレーブ」メモリクリア
            //----------------------------------------
            ret = aio.ResetAiMemory(g_slave_id);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング開始：aio.ResetAiMemory = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」ステータスクリア
            //----------------------------------------
            ret = aio.ResetAiStatus(g_slave_id);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング開始：aio.ResetAiStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」総サンプリング回数を初期化
            //----------------------------------------
            g_slave_sampling_count = 0;
            //----------------------------------------
            // 「スレーブ」サンプリング開始
            //----------------------------------------
            ret = aio.StartAi(g_slave_id);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "サンプリング開始：aio.StartAi = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」ステータスを取得
            //----------------------------------------
            ret = aio.GetAiStatus(g_slave_id, out ai_status);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
            //----------------------------------------
            // 「スレーブ」サンプリング回数を更新
            //----------------------------------------
            textBox_SlaveAiSamplingCount.Text = "0";
            //----------------------------------------
            // 「スレーブ」サンプリングデータ表示用ListBoxをクリア
            //----------------------------------------
            listBox_SlaveAiSamplingData.Items.Clear();
            //----------------------------------------
            // 「スレーブ」サンプリング開始完了のメッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "サンプリング開始：正常終了";
            //----------------------------------------
            // 「マスター」メモリクリア
            //----------------------------------------
            ret = aio.ResetAiMemory(g_master_id);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング開始：aio.ResetAiMemory = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」ステータスクリア
            //----------------------------------------
            ret = aio.ResetAiStatus(g_master_id);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング開始：aio.ResetAiStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」総サンプリング回数を初期化
            //----------------------------------------
            g_master_sampling_count = 0;
            //----------------------------------------
            // 「マスター」サンプリング開始
            //----------------------------------------
            ret = aio.StartAi(g_master_id);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "サンプリング開始：aio.StartAi = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」ステータスを取得
            //----------------------------------------
            ret = aio.GetAiStatus(g_master_id, out ai_status);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.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_MasterAiStatus.Text = text_string;
            //----------------------------------------
            // 「マスター」サンプリング回数を更新
            //----------------------------------------
            textBox_MasterAiSamplingCount.Text = "0";
            //----------------------------------------
            // 「マスター」サンプリングデータ表示用ListBoxをクリア
            //----------------------------------------
            listBox_MasterAiSamplingData.Items.Clear();
            //----------------------------------------
            // 「マスター」サンプリング開始完了のメッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "サンプリング開始：正常終了";
        }

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

            //----------------------------------------
            // 「スレーブ」サンプリング回数を取得
            //----------------------------------------
            ret = aio.GetAiSamplingCount(g_slave_id, out ai_sampling_count);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "状態取得：aio.GetAiSamplingCount = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「スレーブ」サンプリング回数を更新
            //----------------------------------------
            textBox_SlaveAiSamplingCount.Text = ai_sampling_count.ToString();
            //----------------------------------------
            // 「スレーブ」ステータスを取得
            //----------------------------------------
            ret = aio.GetAiStatus(g_slave_id, out ai_status);
            //----------------------------------------
            // 「スレーブ」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
            //----------------------------------------
            // 「スレーブ」状態取得完了のメッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "状態取得：正常終了";
            //----------------------------------------
            // 「マスター」サンプリング回数を取得
            //----------------------------------------
            ret = aio.GetAiSamplingCount(g_master_id, out ai_sampling_count);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "状態取得：aio.GetAiSamplingCount = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 「マスター」サンプリング回数を更新
            //----------------------------------------
            textBox_MasterAiSamplingCount.Text = ai_sampling_count.ToString();
            //----------------------------------------
            // 「マスター」ステータスを取得
            //----------------------------------------
            ret = aio.GetAiStatus(g_master_id, out ai_status);
            //----------------------------------------
            // 「マスター」エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.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_MasterAiStatus.Text = text_string;
            //----------------------------------------
            // 「マスター」状態取得完了のメッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "状態取得：正常終了";
        }

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

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

        //================================================================================
        // マスターデバイス取得データファイル出力ボタンをクリックした際の処理になります
        // グローバルで保持しているサンプリングデータをCSVファイルに出力します
        //================================================================================
        private void button_MasterOutputSamplingData_Click(object sender, EventArgs e)
        {
            string       error_string;                            // エラーコード文字列
            string       file_name = "MasterSamplingData.csv";    // サンプリングデータ出力ファイル名
            string       text_string;                             // テキストボックス更新用
            StreamWriter fp;                                      // ファイルポインタ
            long         i, j;                                    // ループカウント
            int          ret;                                     // 戻り値取得用
            short        ai_channels;                             // チャネル数

            //----------------------------------------
            // ファイルを開く
            //----------------------------------------
            try
            {
                fp = File.CreateText(file_name);
            }
            catch
            {
                textBox_MasterReturnCode.Text = "取得データファイル出力：File.CreateText = file open error.";
                return;
            }
            //----------------------------------------
            // チャネル数取得
            //----------------------------------------
            ret = aio.GetAiChannels(g_master_id, out ai_channels);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_MasterReturnCode.Text = "取得データファイル出力：aio.GetAiChannels = " + ret.ToString() + "：" + error_string;
                //----------------------------------------
                // ファイルを閉じる
                //----------------------------------------
                fp.Close();
                return;
            }
            //----------------------------------------
            // ファイル出力用サンプリングデータの作成
            //----------------------------------------
            //--------------------
            // 総サンプリング回数が1以上かつサンプリングデータが有効であればファイル出力を行う
            //--------------------
            if ((g_master_sampling_count > 0) && (g_master_sampling_data != Array.Empty<float>()))
            {
                //--------------------
                // 総サンプリング回数まで処理を繰り返す
                //--------------------
                for (i = 0; i < g_master_sampling_count; i++)
                {
                    text_string = "";
                    //--------------------
                    // 表示用文字列の生成
                    //--------------------
                    text_string += string.Format("{0,5},", (i + 1));
                    //--------------------
                    // 指定したチャネルの数だけ処理を繰り返す
                    //--------------------
                    for (j = 0; j < ai_channels; j++)
                    {
                        text_string += string.Format(" {0,10:F5}", g_master_sampling_data[i * ai_channels + j]);
                        //--------------------
                        // 最終チャネルの場合改行文字を追加する
                        //--------------------
                        if (j == (ai_channels - 1))
                        {
                            text_string += "\r\n";
                        }
                        //--------------------
                        // 最終チャネル以外の場合カンマを追加する
                        //--------------------
                        else
                        {
                            text_string += ",";
                        }
                    }
                    //----------------------------------------
                    // データ書き込み
                    //----------------------------------------
                    try
                    {
                        fp.Write(text_string);
                    }
                    catch
                    {
                        textBox_MasterReturnCode.Text = "取得データファイル出力：fp.Write = file write error.";
                        fp.Close();
                        return;
                    }
                }
            }
            //----------------------------------------
            // ファイルを閉じる
            //----------------------------------------
            fp.Close();
            //----------------------------------------
            // ファイル出力完了のメッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "取得データファイル出力：\"" + file_name + "\"にデータを保存しました。";
        }

        //================================================================================
        // スレーブデバイス取得データファイル出力ボタンをクリックした際の処理になります
        // グローバルで保持しているサンプリングデータをCSVファイルに出力します
        //================================================================================
        private void button_SlaveOutputSamplingData_Click(object sender, EventArgs e)
        {
            string       error_string;                           // エラーコード文字列
            string       file_name = "SlaveSamplingData.csv";    // サンプリングデータ出力ファイル名
            string       text_string;                            // テキストボックス更新用
            StreamWriter fp;                                     // ファイルポインタ
            long         i, j;                                   // ループカウント
            int          ret;                                    // 戻り値取得用
            short        ai_channels;                            // チャネル数

            //----------------------------------------
            // ファイルを開く
            //----------------------------------------
            try
            {
                fp = File.CreateText(file_name);
            }
            catch
            {
                textBox_SlaveReturnCode.Text = "取得データファイル出力：File.CreateText = file open error.";
                return;
            }
            //----------------------------------------
            // チャネル数取得
            //----------------------------------------
            ret = aio.GetAiChannels(g_slave_id, out ai_channels);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_SlaveReturnCode.Text = "取得データファイル出力：aio.GetAiChannels = " + ret.ToString() + "：" + error_string;
                //----------------------------------------
                // ファイルを閉じる
                //----------------------------------------
                fp.Close();
                return;
            }
            //----------------------------------------
            // ファイル出力用サンプリングデータの作成
            //----------------------------------------
            //--------------------
            // 総サンプリング回数が1以上かつサンプリングデータが有効であればファイル出力を行う
            //--------------------
            if ((g_slave_sampling_count > 0) && (g_slave_sampling_data != Array.Empty<float>()))
            {
                //--------------------
                // 総サンプリング回数まで処理を繰り返す
                //--------------------
                for (i = 0; i < g_slave_sampling_count; i++)
                {
                    text_string = "";
                    //--------------------
                    // 表示用文字列の生成
                    //--------------------
                    text_string += string.Format("{0,5},", (i + 1));
                    //--------------------
                    // 指定したチャネルの数だけ処理を繰り返す
                    //--------------------
                    for (j = 0; j < ai_channels; j++)
                    {
                        text_string += string.Format(" {0,10:F5}", g_slave_sampling_data[i * ai_channels + j]);
                        //--------------------
                        // 最終チャネルの場合改行文字を追加する
                        //--------------------
                        if (j == (ai_channels - 1))
                        {
                            text_string += "\r\n";
                        }
                        //--------------------
                        // 最終チャネル以外の場合カンマを追加する
                        //--------------------
                        else
                        {
                            text_string += ",";
                        }
                    }
                    //----------------------------------------
                    // データ書き込み
                    //----------------------------------------
                    try
                    {
                        fp.Write(text_string);
                    }
                    catch
                    {
                        textBox_SlaveReturnCode.Text = "取得データファイル出力：fp.Write = file write error.";
                        fp.Close();
                        return;
                    }
                }
            }
            //----------------------------------------
            // ファイルを閉じる
            //----------------------------------------
            fp.Close();
            //----------------------------------------
            // ファイル出力完了のメッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "取得データファイル出力：\"" + file_name + "\"にデータを保存しました。";
        }

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

            //----------------------------------------
            // 「マスター」AD変換データ格納用の配列を解放
            //----------------------------------------
            if(g_master_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh_master_data.IsAllocated == true)
                {
                    gCh_master_data.Free();
                }
                g_master_sampling_data = Array.Empty<float>();
            }
            //----------------------------------------
            // 「マスター」終了処理の実行
            //----------------------------------------
            ret = aio.Exit(g_master_id);
            //----------------------------------------
            // 「マスター」エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // 「マスター」メッセージ表示
            //----------------------------------------
            textBox_MasterReturnCode.Text = "aio.Exit = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // 「スレーブ」AD変換データ格納用の配列を解放
            //----------------------------------------
            if(g_slave_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh_slave_data.IsAllocated == true)
                {
                    gCh_slave_data.Free();
                }
                g_slave_sampling_data = Array.Empty<float>();
            }
            //----------------------------------------
            // 「スレーブ」終了処理の実行
            //----------------------------------------
            ret = aio.Exit(g_slave_id);
            //----------------------------------------
            // 「スレーブ」エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // 「スレーブ」メッセージ表示
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "aio.Exit = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 閉じるをクリックした際の処理になります
        // デバイスの終了処理を行い、アプリケーションを終了します
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // AD変換データ格納用の配列を解放
            //----------------------------------------
            if(g_master_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh_master_data.IsAllocated == true)
                {
                    gCh_master_data.Free();
                }
                g_master_sampling_data = Array.Empty<float>();
            }
            if(g_slave_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandleの解放
                //----------------------------------------
                if (gCh_slave_data.IsAllocated == true)
                {
                    gCh_slave_data.Free();
                }
                g_slave_sampling_data = Array.Empty<float>();
            }
            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            aio.Exit(g_slave_id);
            aio.Exit(g_master_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // イベントが発生した時の動作
        // 各イベントが発生したとき、イベント毎に処理を行う
        //================================================================================
        protected override void WndProc(ref Message m)
        {
            string    error_string;         // エラーコード文字列
            string    text_string;          // テキストボックス更新用
            int       ai_sampling_count;    // サンプリングカウント
            int       ai_status;            // ステータス
            int       ret;                  // 戻り値取得用
            long      i, j;                 // ループカウント
            long      loop_max_count;       // ループカウント最大値
            long      top_address;          // サンプリングデータ表示のバッファの先頭アドレス
            short     ai_channels;          // チャネル数
            short     id;                   // デバイスアクセス用ID

            //----------------------------------------
            // デバイスアクセス用IDを取得
            //----------------------------------------
            id = (short)((long)m.WParam & 0x0000FFFF);
            //----------------------------------------
            // AD変換開始条件成立イベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_START)
            {
                //----------------------------------------
                // スレーブID
                //----------------------------------------
                if (id == g_slave_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "AD変換開始条件成立イベント処理：正常終了";
                }
            }
            //----------------------------------------
            // デバイス動作終了イベント
            // データの取得、表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_END)
            {
                //----------------------------------------
                // マスターID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.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_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // 現在のサンプリング回数を取得
                    //----------------------------------------
                    ret = aio.GetAiSamplingCount(g_master_id, out ai_sampling_count);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiSamplingCount = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // サンプリング回数を表示
                    //----------------------------------------
                    textBox_MasterAiSamplingCount.Text = ai_sampling_count.ToString();
                    //----------------------------------------
                    // チャネル数取得
                    //----------------------------------------
                    ret = aio.GetAiChannels(g_master_id, out ai_channels);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiChannels = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // サンプリングデータ取得処理
                    //----------------------------------------
                    //--------------------
                    // データ格納用の配列を確保
                    //--------------------
                    if(g_master_sampling_data != Array.Empty<float>()){
                        //----------------------------------------
                        // GCHandleの解放
                        //----------------------------------------
                        if (gCh_master_data.IsAllocated == true)
                        {
                            gCh_master_data.Free();
                        }
                        g_master_sampling_data = Array.Empty<float>();
                    }
                    try
                    {
                        g_master_sampling_data = new float[ai_sampling_count * ai_channels];
                    }
                    catch
                    {
                        textBox_MasterReturnCode.Text = "デバイス動作終了イベント処理：データ格納用の配列確保に失敗";
                        return;
                    }
                    //----------------------------------------
                    // PinnedハンドルをgCh_master_dataに分配する
                    //----------------------------------------
                    if (gCh_master_data.IsAllocated == false)
                    {
                        gCh_master_data = GCHandle.Alloc(g_master_sampling_data, GCHandleType.Pinned);
                    }
                    //----------------------------------------
                    // サンプリングデータを取得
                    //----------------------------------------
                    ret = aio.GetAiSamplingDataEx(g_master_id, ref ai_sampling_count, ref g_master_sampling_data);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiSamplingDataEx = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // 総サンプリング回数を更新
                    //----------------------------------------
                    g_master_sampling_count = ai_sampling_count;
                    //----------------------------------------
                    // サンプリングデータ表示ループのループ回数を設定
                    //----------------------------------------
                    //--------------------
                    // 今回取得できたデータのサンプリング回数がデータ表示最大サンプリング回数を超えた場合
                    //--------------------
                    if (ai_sampling_count > DISPLAY_SAMPLING_COUNT)
                    {
                        //--------------------
                        // 最大ループ回数をデータ表示最大サンプリング回数に制限
                        //--------------------
                        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;
                    }
                    //----------------------------------------
                    // サンプリングデータを表示
                    // (取得データの末尾からデータ表示最大サンプリング回数までを表示)
                    //----------------------------------------
                    //--------------------
                    // 今回取得できたデータのサンプリング回数が1以上の場合
                    //--------------------
                    if (ai_sampling_count > 0)
                    {
                        //--------------------
                        // サンプリングデータ表示用ListBoxをクリア
                        //--------------------
                        listBox_MasterAiSamplingData.Items.Clear();
                        //--------------------
                        // 表示データの最大サンプリング回数まで処理を繰り返す
                        //--------------------
                        for (i = top_address; i < (loop_max_count + top_address); i++)
                        {
                            //--------------------
                            // 表示用文字列格納変数をクリア
                            //--------------------
                            text_string = "";
                            //--------------------
                            // 表示用文字列の生成
                            //--------------------
                            text_string += string.Format("{0,5},  ", (i + 1));
                            //--------------------
                            // 指定したチャネルの数だけ処理を繰り返す
                            //--------------------
                            for (j = 0; j < ai_channels; j++)
                            {
                                text_string += string.Format(" {0, 10:F5}", g_master_sampling_data[i * ai_channels + j]);
                            }
                            //----------------------------------------
                            // サンプリング開始データを表示
                            //----------------------------------------
                            listBox_MasterAiSamplingData.Items.Add(text_string);
                        }
                    }
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "デバイス動作終了イベント処理：正常終了";
                }
                //----------------------------------------
                // スレーブID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // 現在のサンプリング回数を取得
                    //----------------------------------------
                    ret = aio.GetAiSamplingCount(g_slave_id, out ai_sampling_count);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiSamplingCount = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // サンプリング回数を表示
                    //----------------------------------------
                    textBox_SlaveAiSamplingCount.Text = ai_sampling_count.ToString();
                    //----------------------------------------
                    // チャネル数取得
                    //----------------------------------------
                    ret = aio.GetAiChannels(g_slave_id, out ai_channels);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiChannels = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // サンプリングデータ取得処理
                    //----------------------------------------
                    //--------------------
                    // データ格納用の配列を確保
                    //--------------------
                    if(g_slave_sampling_data != Array.Empty<float>()){
                        //----------------------------------------
                        // GCHandleの解放
                        //----------------------------------------
                        if (gCh_slave_data.IsAllocated == true)
                        {
                            gCh_slave_data.Free();
                        }
                        g_slave_sampling_data = Array.Empty<float>();
                    }
                    try
                    {
                        g_slave_sampling_data = new float[ai_sampling_count * ai_channels];
                    }
                    catch
                    {
                        textBox_SlaveReturnCode.Text = "デバイス動作終了イベント処理：データ格納用の配列確保に失敗";
                        return;
                    }
                    //----------------------------------------
                    // PinnedハンドルをgCh_slave_dataに分配する
                    //----------------------------------------
                    if (gCh_slave_data.IsAllocated == false)
                    {
                        gCh_slave_data = GCHandle.Alloc(g_slave_sampling_data, GCHandleType.Pinned);
                    }
                    //----------------------------------------
                    // サンプリングデータを取得
                    //----------------------------------------
                    ret = aio.GetAiSamplingDataEx(g_slave_id, ref ai_sampling_count, ref g_slave_sampling_data);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAiSamplingDataEx = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // 総サンプリング回数を更新
                    //----------------------------------------
                    g_slave_sampling_count = ai_sampling_count;
                    //----------------------------------------
                    // サンプリングデータ表示ループのループ回数を設定
                    //----------------------------------------
                    //--------------------
                    // 今回取得できたデータのサンプリング回数がデータ表示最大サンプリング回数を超えた場合
                    //--------------------
                    if (ai_sampling_count > DISPLAY_SAMPLING_COUNT)
                    {
                        //--------------------
                        // 最大ループ回数をデータ表示最大サンプリング回数に制限
                        //--------------------
                        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;
                    }
                    //----------------------------------------
                    // サンプリングデータを表示
                    // (取得データの末尾からデータ表示最大サンプリング回数までを表示)
                    //----------------------------------------
                    //--------------------
                    // 今回取得できたデータのサンプリング回数が1以上の場合
                    //--------------------
                    if (ai_sampling_count > 0)
                    {
                        //--------------------
                        // サンプリングデータ表示用ListBoxをクリア
                        //--------------------
                        listBox_SlaveAiSamplingData.Items.Clear();
                        //--------------------
                        // 表示データの最大サンプリング回数まで処理を繰り返す
                        //--------------------
                        for (i = top_address; i < (loop_max_count + top_address); i++)
                        {
                            //--------------------
                            // 表示用文字列格納変数をクリア
                            //--------------------
                            text_string = "";
                            //--------------------
                            // 表示用文字列の生成
                            //--------------------
                            text_string += string.Format("{0,5},  ", (i + 1));
                            //--------------------
                            // 指定したチャネルの数だけ処理を繰り返す
                            //--------------------
                            for (j = 0; j < ai_channels; j++)
                            {
                                text_string += string.Format(" {0, 10:F5}", g_slave_sampling_data[i * ai_channels + j]);
                            }
                            //----------------------------------------
                            // サンプリング開始データを表示
                            //----------------------------------------
                            listBox_SlaveAiSamplingData.Items.Add(text_string);
                        }
                    }
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "デバイス動作終了イベント処理：正常終了";
                }
            }
            //----------------------------------------
            // オーバーフローイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_OFERR)
            {
                //----------------------------------------
                // マスターID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.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_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "オーバーフロー発生処理：正常終了";
                }
                //----------------------------------------
                // スレーブID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "オーバーフロー発生処理：正常終了";
                }
            }
            //----------------------------------------
            // サンプリングクロックエラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_SCERR)
            {
                //----------------------------------------
                // マスターID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.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_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "サンプリングクロックエラー発生処理：正常終了";
                }
                //----------------------------------------
                // スレーブID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "サンプリングクロックエラー発生処理：正常終了";
                }
            }
            //----------------------------------------
            // AD変換エラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_ADERR)
            {
                //----------------------------------------
                // マスターID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_MasterReturnCode.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_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "AD変換エラー発生処理：正常終了";
                }
                //----------------------------------------
                // スレーブID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // ステータスを取得
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_SlaveReturnCode.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_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "AD変換エラー発生処理：正常終了";
                }
            }
            base.WndProc(ref m);
        }
    }
}