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

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

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

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short     g_id;                                                                  // デバイスアクセス用ID
        public string    g_file_name = "GeneratingData.csv";                                    // ジェネレーティングデータ出力ファイル名
        public float[]   g_generating_data;                                                     // 出力データ
        public GCHandle  gCh;                                                                   // g_generating_data配列用GCHandle
        public short[]   g_ao_range = new short[MAX_AO_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.P1TO5 };     // 1～5V

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

        private void AoGeneratingRingMemory_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // アナログ出力のチャネル数の初期表示を更新
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // ジェネレーティングクロックの初期表示を更新
            //----------------------------------------
            textBox_GeneratingClock.Text = "1000";
            //----------------------------------------
            // リピート回数の初期表示を更新
            //----------------------------------------
            textBox_RepeatTimes.Text = "0";
            //----------------------------------------
            // 出力データのジェネレーティング回数の初期表示を更新
            //----------------------------------------
            textBox_GeneratingTimes.Text = "1000";
            //----------------------------------------
            // アナログ出力レンジ用コンボボックスの作成
            //----------------------------------------
            comboBox_RangeValue.Items.Add("-10 - +10V");
            comboBox_RangeValue.Items.Add("-5 - +5V");
            comboBox_RangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_RangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_RangeValue.Items.Add("-1 - +1V");
            comboBox_RangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_RangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_RangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_RangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_RangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_RangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_RangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_RangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_RangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_RangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +10V");
            comboBox_RangeValue.Items.Add("0 - +5V");
            comboBox_RangeValue.Items.Add("0 - +4.095V");
            comboBox_RangeValue.Items.Add("0 - +2.5V");
            comboBox_RangeValue.Items.Add("0 - +1.25V");
            comboBox_RangeValue.Items.Add("0 - +1V");
            comboBox_RangeValue.Items.Add("0 - +0.5V");
            comboBox_RangeValue.Items.Add("0 - +0.25V");
            comboBox_RangeValue.Items.Add("0 - +0.1V");
            comboBox_RangeValue.Items.Add("0 - +0.05V");
            comboBox_RangeValue.Items.Add("0 - +0.025V");
            comboBox_RangeValue.Items.Add("0 - +0.0125V");
            comboBox_RangeValue.Items.Add("0 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +20mA");
            comboBox_RangeValue.Items.Add("4 - +20mA");
            comboBox_RangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // アナログ出力レンジ用コンボボックスの選択を0番に変更
            //----------------------------------------
            comboBox_RangeValue.SelectedIndex = 0;
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_id = 0;
        }

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

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

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

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

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

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

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

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

        //================================================================================
        // AioSetAoSamplingClockボタンをクリックした際の処理になります
        // ジェネレーティングに使用する内部クロック変換速度の設定を行います
        //================================================================================
        private void button_SetAoSamplingClock_Click(object sender, EventArgs e)
        {
            string error_string;        // エラーコード文字列
            int    ret;                 // 戻り値取得用
            float  generating_clock;    // 内部クロックの変換速度

            //----------------------------------------
            // テキストボックスから内部クロックの変換速度を取得
            //----------------------------------------
            if (float.TryParse(textBox_GeneratingClock.Text, out generating_clock) == false)
            {
                generating_clock = 1000;
            }
            //----------------------------------------
            // 内部クロックの変換速度を設定
            //----------------------------------------
            ret = aio.SetAoSamplingClock(g_id, generating_clock);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAoSamplingClock = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 基本設定ボタンをクリックした際の処理になります
        // ジェネレーティングに必要な基本設定を行います
        // (基本設定は初期化処理/リセット処理実行後、初回のジェネレーティング開始前に実行する必要があります)
        //================================================================================
        private void button_BasicSettings_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用

            //----------------------------------------
            // アナログ出力の変換データ転送方式をデバイスバッファモードに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoTransferMode(g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "基本設定：aio.SetAoTransferMode = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // データ格納用メモリ形式をRINGに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoMemoryType(g_id, 1);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "基本設定：aio.SetAoMemoryType = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ジェネレーティング開始条件をソフトウェアに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoStartTrigger(g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "基本設定：aio.SetAoStartTrigger = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // クロック種類を内部クロックに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoClockType(g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "基本設定：aio.SetAoClockType = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ジェネレーティング停止条件を設定回数変換終了に設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoStopTrigger(g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "基本設定：aio.SetAoStopTrigger = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // イベントにデバイス動作終了イベント、リピート終了イベント、
            // サンプリングクロックエラーイベント、DA変換エラーイベントを設定
            // 変更注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAoEvent(g_id, Handle, (int)(CaioConst.AOE_END | CaioConst.AOE_RPTEND | CaioConst.AOE_SCERR | CaioConst.AOE_DAERR));
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "基本設定：aio.SetAoEvent = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 基本設定完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "基本設定：正常終了";
        }

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

            //----------------------------------------
            // テキストボックスからリピート回数を取得
            //----------------------------------------
            if (int.TryParse(textBox_RepeatTimes.Text, out repeat_times) == false)
            {
                repeat_times = 0;
            }
            //----------------------------------------
            // ピート回数を設定
            //----------------------------------------
            ret = aio.SetAoRepeatTimes(g_id, repeat_times);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAoRepeatTimes = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // 出力データ生成ボタンをクリックした際の処理になります
        // 出力データファイルを作成します
        //================================================================================
        private void button_OutputDataGenerate_Click(object sender, EventArgs e)
        {
            string       error_string;        // エラーコード文字列
            string       text_string;         // テキストボックス更新用
            StreamWriter fp;                  // ファイルポインタ
            long         i, j;                // ループカウント
            int          ret;                 // 戻り値取得用
            int          generating_times;    // 出力データのジェネレーティング回数
            short        ao_channels;         // チャネル数
            short        ao_range;            // レンジ
            float        gain, offset;        // サイン波形の係数

            //----------------------------------------
            // チャネル数取得
            //----------------------------------------
            ret = aio.GetAoChannels(g_id, out ao_channels);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "出力データ生成：aio.GetAoChannels = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // レンジ取得(チャネル0のみ)
            //----------------------------------------
            ret = aio.GetAoRange(g_id, 0, out ao_range);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "出力データ生成：aio.GetAoRange = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // テキストボックスからジェネレーティング回数を取得
            //----------------------------------------
            if (int.TryParse(textBox_GeneratingTimes.Text, out generating_times) == false)
            {
                generating_times = 1000;
            }
            //----------------------------------------
            // 出力データ処理
            //----------------------------------------
            //--------------------
            // データ格納用の配列を確保
            //--------------------
            try
            {
                g_generating_data = new float[generating_times * ao_channels];
            }
            catch
            {
                textBox_ReturnCode.Text = "出力データ生成：データ格納用の配列確保に失敗";
                return;
            }
            //----------------------------------------
            // PinnedハンドルをgChに分配する
            //----------------------------------------
            if (gCh.IsAllocated == false)
            {
                gCh = GCHandle.Alloc(g_generating_data, GCHandleType.Pinned);
            }
            //----------------------------------------
            // アナログ出力データ作成：サイン波形
            //----------------------------------------
            switch (ao_range)
            {
                case (short)CaioConst.PM10:
                    gain   = 10;
                    offset = 0;
                    break;
                case (short)CaioConst.PM5:
                    gain   = 5;
                    offset = 0;
                    break;
                case (short)CaioConst.PM25:
                    gain   = 2.5f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM125:
                    gain   = 1.25f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM1:
                    gain   = 1;
                    offset = 0;
                    break;
                case (short)CaioConst.PM0625:
                    gain   = 0.625f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM05:
                    gain   = 0.5f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM03125:
                    gain   = 0.3125f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM025:
                    gain   = 0.25f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM0125:
                    gain   = 0.125f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM01:
                    gain   = 0.1f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM005:
                    gain   = 0.05f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM0025:
                    gain   = 0.025f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM00125:
                    gain   = 0.0125f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM001:
                    gain   = 0.01f;
                    offset = 0;
                    break;
                case (short)CaioConst.P10:
                    gain   = 5;
                    offset = 5;
                    break;
                case (short)CaioConst.P5:
                    gain   = 2.5f;
                    offset = 2.5f;
                    break;
                case (short)CaioConst.P4095:
                    gain   = 2.0475f;
                    offset = 2.0475f;
                    break;
                case (short)CaioConst.P25:
                    gain   = 1.25f;
                    offset = 1.25f;
                    break;
                case (short)CaioConst.P125:
                    gain   = 0.625f;
                    offset = 0.625f;
                    break;
                case (short)CaioConst.P1:
                    gain   = 0.5f;
                    offset = 0.5f;
                    break;
                case (short)CaioConst.P05:
                    gain   = 0.25f;
                    offset = 0.25f;
                    break;
                case (short)CaioConst.P025:
                    gain   = 0.125f;
                    offset = 0.125f;
                    break;
                case (short)CaioConst.P01:
                    gain   = 0.05f;
                    offset = 0.05f;
                    break;
                case (short)CaioConst.P005:
                    gain   = 0.025f;
                    offset = 0.025f;
                    break;
                case (short)CaioConst.P0025:
                    gain   = 0.0125f;
                    offset = 0.0125f;
                    break;
                case (short)CaioConst.P00125:
                    gain   = 0.00625f;
                    offset = 0.00625f;
                    break;
                case (short)CaioConst.P001:
                    gain   = 0.005f;
                    offset = 0.005f;
                    break;
                case (short)CaioConst.P20MA:
                    gain   = 10;
                    offset = 10;
                    break;
                case (short)CaioConst.P4TO20MA:
                    gain   = 8;
                    offset = 12;
                    break;
                case (short)CaioConst.P1TO5:
                    gain   = 2;
                    offset = 3;
                    break;
                default:
                    gain   = 10;
                    offset = 0;
                    break;
            }
            for (i = 0; i < generating_times; i++)
            {
                for (j = 0; j < ao_channels; j++)
                {
                    g_generating_data[i * ao_channels + j] = (float)(gain * Math.Sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * (j % 4)) + offset);
                }
            }
            //----------------------------------------
            // ファイルオープン
            //----------------------------------------
            try
            {
                fp = File.CreateText(g_file_name);
            }
            catch
            {
                textBox_ReturnCode.Text = "出力データ生成：File.CreateText = file open error.";
                return;
            }
            //----------------------------------------
            // ファイル作成
            //----------------------------------------
            for (i = 0; i < generating_times; i++)
            {
                text_string = "";
                //--------------------
                // 表示用文字列の生成
                //--------------------
                text_string += string.Format("{0,5},", (i + 1));
                //--------------------
                // 指定したチャネルの数だけ処理を繰り返す
                //--------------------
                for (j = 0; j < ao_channels; j++)
                {
                    text_string += string.Format(" {0,10:F5}", g_generating_data[i * ao_channels + j]);
                    //--------------------
                    // 最終チャネルの場合改行文字を追加する
                    //--------------------
                    if (j == (ao_channels - 1))
                    {
                        text_string += "\r\n";
                    }
                    //--------------------
                    // 最終チャネル以外の場合カンマを追加する
                    //--------------------
                    else
                    {
                        text_string += ",";
                    }
                }
                //----------------------------------------
                // データ書き込み
                //----------------------------------------
                try
                {
                    fp.Write(text_string);
                }
                catch
                {
                    textBox_ReturnCode.Text = "出力データ生成：fp.Write = file write error.";
                    fp.Close();
                    return;
                }
            }
            //----------------------------------------
            // ファイルクローズ
            //----------------------------------------
            fp.Close();
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "出力データ生成 : 正常終了";
        }

        //================================================================================
        // AioSetAoSamplingDataExボタンをクリックした際の処理になります
        // 出力データを設定します
        //================================================================================
        private void button_SetAoSamplingDataEx_Click(object sender, EventArgs e)
        {
            string error_string;        // エラーコード文字列
            int    ret;                 // 戻り値取得用
            int    generating_times;    // 出力データのジェネレーティング回数

            //----------------------------------------
            // テキストボックスからジェネレーティング回数を取得
            //----------------------------------------
            if (int.TryParse(textBox_GeneratingTimes.Text, out generating_times) == false)
            {
                generating_times = 1000;
            }
            //----------------------------------------
            // アナログ出力データの設定
            //----------------------------------------
            ret = aio.SetAoSamplingDataEx(g_id, generating_times, g_generating_data);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAoSamplingDataEx = " + ret.ToString() + "：" + error_string;
        }

        //================================================================================
        // ジェネレーティング開始ボタンをクリックした際の処理になります
        // ステータスのクリアを行った後、ジェネレーティング開始を行います
        //================================================================================
        private void button_GeneratingStart_Click(object sender, EventArgs e)
        {
            string error_string;        // エラーコード文字列
            string text_string;         // テキストボックス更新用
            int    ao_status;           // アナログ出力ステータス
            long   i, j;                // ループカウント
            int    ret;                 // 戻り値取得用
            int    generating_times;    // 出力データのジェネレーティング回数
            short  ao_channels;         // チャネル数
            short  ao_range;            // レンジ
            float  gain, offset;        // サイン波形の係数

            //----------------------------------------
            // アナログ出力ステータスのリセット
            //----------------------------------------
            ret = aio.ResetAoStatus(g_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "ジェネレーティング開始：aio.ResetAoStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 出力データファイルが存在しなければ
            //----------------------------------------
            if (File.Exists(g_file_name) == false)
            {
                //----------------------------------------
                // チャネル数取得
                //----------------------------------------
                ret = aio.GetAoChannels(g_id, out ao_channels);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "ジェネレーティング開始：aio.GetAoChannels = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // レンジ取得(チャネル0のみ)
                //----------------------------------------
                ret = aio.GetAoRange(g_id, 0, out ao_range);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "ジェネレーティング開始：aio.GetAoRange = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // テキストボックスからジェネレーティング回数を取得
                //----------------------------------------
                if (int.TryParse(textBox_GeneratingTimes.Text, out generating_times) == false)
                {
                    generating_times = 1000;
                }
                //----------------------------------------
                // 出力データ処理
                //----------------------------------------
                //--------------------
                // データ格納用の配列を確保
                //--------------------
                try
                {
                    g_generating_data = new float[generating_times * ao_channels];
                }
                catch
                {
                    textBox_ReturnCode.Text = "出力データ生成：データ格納用の配列確保に失敗";
                    return;
                }
                //----------------------------------------
                // PinnedハンドルをgChに分配する
                //----------------------------------------
                if (gCh.IsAllocated == false)
                {
                    gCh = GCHandle.Alloc(g_generating_data, GCHandleType.Pinned);
                }
                //----------------------------------------
                // アナログ出力データ作成：サイン波形
                //----------------------------------------
                switch (ao_range)
                {
                    case (short)CaioConst.PM10:
                        gain   = 10;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM5:
                        gain   = 5;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM25:
                        gain   = 2.5f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM125:
                        gain   = 1.25f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM1:
                        gain   = 1;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM0625:
                        gain   = 0.625f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM05:
                        gain   = 0.5f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM03125:
                        gain   = 0.3125f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM025:
                        gain   = 0.25f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM0125:
                        gain   = 0.125f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM01:
                        gain   = 0.1f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM005:
                        gain   = 0.05f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM0025:
                        gain   = 0.025f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM00125:
                        gain   = 0.0125f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM001:
                        gain   = 0.01f;
                        offset = 0;
                        break;
                    case (short)CaioConst.P10:
                        gain   = 5;
                        offset = 5;
                        break;
                    case (short)CaioConst.P5:
                        gain   = 2.5f;
                        offset = 2.5f;
                        break;
                    case (short)CaioConst.P4095:
                        gain   = 2.0475f;
                        offset = 2.0475f;
                        break;
                    case (short)CaioConst.P25:
                        gain   = 1.25f;
                        offset = 1.25f;
                        break;
                    case (short)CaioConst.P125:
                        gain   = 0.625f;
                        offset = 0.625f;
                        break;
                    case (short)CaioConst.P1:
                        gain   = 0.5f;
                        offset = 0.5f;
                        break;
                    case (short)CaioConst.P05:
                        gain   = 0.25f;
                        offset = 0.25f;
                        break;
                    case (short)CaioConst.P025:
                        gain   = 0.125f;
                        offset = 0.125f;
                        break;
                    case (short)CaioConst.P01:
                        gain   = 0.05f;
                        offset = 0.05f;
                        break;
                    case (short)CaioConst.P005:
                        gain   = 0.025f;
                        offset = 0.025f;
                        break;
                    case (short)CaioConst.P0025:
                        gain   = 0.0125f;
                        offset = 0.0125f;
                        break;
                    case (short)CaioConst.P00125:
                        gain   = 0.00625f;
                        offset = 0.00625f;
                        break;
                    case (short)CaioConst.P001:
                        gain   = 0.005f;
                        offset = 0.005f;
                        break;
                    case (short)CaioConst.P20MA:
                        gain   = 10;
                        offset = 10;
                        break;
                    case (short)CaioConst.P4TO20MA:
                        gain   = 8;
                        offset = 12;
                        break;
                    case (short)CaioConst.P1TO5:
                        gain   = 2;
                        offset = 3;
                        break;
                    default:
                        gain   = 10;
                        offset = 0;
                        break;
                }
                for (i = 0; i < generating_times; i++)
                {
                    for (j = 0; j < ao_channels; j++)
                    {
                        g_generating_data[i * ao_channels + j] = (float)(gain * Math.Sin(2 * 3.14159265358979 * i / generating_times + 3.14159265358979 / 2 * (j % 4)) + offset);
                    }
                }
                //----------------------------------------
                // アナログ出力データの設定
                //----------------------------------------
                ret = aio.SetAoSamplingDataEx(g_id, generating_times, g_generating_data);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "ジェネレーティング開始：aio.SetAoSamplingDataEx = " + ret.ToString() + "：" + error_string;
                    return;
                }
            }
            //----------------------------------------
            // 変換開始
            //----------------------------------------
            ret = aio.StartAo(g_id);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "ジェネレーティング開始：aio.StartAo = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスを取得
            //----------------------------------------
            ret = aio.GetAoStatus(g_id, out ao_status);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "ジェネレーティング開始：aio.GetAoStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスの表示
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", ao_status);
            //--------------------
            // 動作中ステータス有効時
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
            {
                text_string += "動作中";
            }
            //--------------------
            // 動作中ステータス無効時
            //--------------------
            else
            {
                text_string += "停止中";
            }
            //--------------------
            // 開始トリガ待ちステータス有効時
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
            {
                text_string += ", 開始トリガ待ち";
            }
            //--------------------
            // 指定サンプリング回数出力ステータス有効時
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
            {
                text_string += ", 指定サンプリング回数出力";
            }
            //--------------------
            // サンプリングクロックエラーステータス有効時
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
            {
                text_string += ", サンプリングクロックエラー";
            }
            //--------------------
            // DA変換エラーステータス有効時
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
            {
                text_string += ", DA変換エラー";
            }
            //--------------------
            // ドライバスペックエラーステータス有効時
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
            {
                text_string += ", ドライバスペックエラー";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // ジェネレーティング回数を更新
            //----------------------------------------
            textBox_GeneratingCount.Text = "0";
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "ジェネレーティング開始：正常終了";
        }

        //================================================================================
        // AioGetAoSamplingTimesボタンをクリックした際の処理になります
        // 設定されている出力データ量をジェネレーティング回数で取得しダイアログ上に表示します
        //================================================================================
        private void button_GetAoSamplingTimes_Click(object sender, EventArgs e)
        {
            string error_string;           // エラーコード文字列
            int    ret;                    // 戻り値取得用
            int    ao_generating_times;    // ジェネレーティング回数

            //----------------------------------------
            // ジェネレーティング回数を取得
            //----------------------------------------
            ret = aio.GetAoSamplingTimes(g_id, out ao_generating_times);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAoSamplingTimes = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // ジェネレーティング回数取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // ジェネレーティング回数を更新
                //--------------------
                textBox_GeneratingCount.Text = ao_generating_times.ToString();
            }
        }

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

            //----------------------------------------
            // 現在のリピート回数を取得
            //----------------------------------------
            ret = aio.GetAoRepeatCount(g_id, out ao_repeat_count);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAoRepeatCount = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // リピート回数取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // リピート回数を更新
                //--------------------
                textBox_RepeatCount.Text = ao_repeat_count.ToString();
            }
        }

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

            //----------------------------------------
            // ステータスを取得
            //----------------------------------------
            ret = aio.GetAoStatus(g_id, out ao_status);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAoStatus = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // ステータス取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数出力ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", 指定サンプリング回数出力";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // DA変換エラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
            }
        }

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

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

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

            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + "：" + error_string;
        }

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

        //================================================================================
        // イベントが発生した時の動作
        // 各イベントが発生したとき、イベント毎に処理を行います
        //================================================================================
        protected override void WndProc(ref Message m)
        {
            string error_string;           // エラーコード文字列
            string text_string;            // テキストボックス更新用
            int    ao_generating_times;    // ジェネレーティングカウント
            int    ao_repeat_count;        // 繰り返し回数
            int    ao_status;              // ステータス
            int    ret;                    // 戻り値取得用

            //----------------------------------------
            // リピート終了イベント
            // データの取得、表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_RPTEND)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "リピート終了イベント処理：aio.GetAoStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数出力ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", 指定サンプリング回数出力";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // DA変換エラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // ジェネレーティング回数を取得
                //----------------------------------------
                ret = aio.GetAoSamplingTimes(g_id, out ao_generating_times);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret,out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "リピート終了イベント処理：aio.GetAoSamplingTimes = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ジェネレーティング回数を表示
                //----------------------------------------
                textBox_GeneratingCount.Text = ao_generating_times.ToString();
                //----------------------------------------
                // 現在のリピート回数を取得
                //----------------------------------------
                ret = aio.GetAoRepeatCount(g_id, out ao_repeat_count);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "リピート終了イベント処理：aio.GetAoRepeatCount = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // リピート回数を表示
                //----------------------------------------
                textBox_RepeatCount.Text = ao_repeat_count.ToString();
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "リピート終了イベント処理：正常終了";
            }
            //----------------------------------------
            // デバイス動作終了イベント
            // データの取得、表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_END)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAoStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数出力ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", 指定サンプリング回数出力";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // DA変換エラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // ジェネレーティング回数を取得
                //----------------------------------------
                ret = aio.GetAoSamplingTimes(g_id, out ao_generating_times);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAoSamplingTimes = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ジェネレーティング回数を表示
                //----------------------------------------
                textBox_GeneratingCount.Text = ao_generating_times.ToString();
                //----------------------------------------
                // 現在のリピート回数を取得
                //----------------------------------------
                ret = aio.GetAoRepeatCount(g_id, out ao_repeat_count);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "デバイス動作終了イベント処理：aio.GetAoRepeatCount = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // リピート回数を表示
                //----------------------------------------
                textBox_RepeatCount.Text = ao_repeat_count.ToString();
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "デバイス動作終了イベント処理：正常終了";
            }
            //----------------------------------------
            // サンプリングクロックエラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_SCERR)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：aio.GetAoStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数出力ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", 指定サンプリング回数出力";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // DA変換エラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "サンプリングクロックエラー発生処理：正常終了";
            }
            //----------------------------------------
            // DA変換エラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_DAERR)
            {
                //----------------------------------------
                // ステータスを取得
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "DA変換エラー発生処理：aio.GetAoStatus = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 開始トリガ待ちステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", 開始トリガ待ち";
                }
                //--------------------
                // 指定サンプリング回数出力ステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", 指定サンプリング回数出力";
                }
                //--------------------
                // サンプリングクロックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", サンプリングクロックエラー";
                }
                //--------------------
                // DA変換エラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA変換エラー";
                }
                //--------------------
                // ドライバスペックエラーステータス有効時
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", ドライバスペックエラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // イベント処理完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "DA変換エラー発生処理：正常終了";
            }
            base.WndProc(ref m);
        }
    }
}