﻿using CaioCs;

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

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public short[] g_start_trigger = {0,     // ソフトウェア
                                          1,     // 外部トリガ立ち上がり
                                          2,     // 外部トリガ立ち下がり
                                          3,     // レベル比較
                                          4,     // インレンジ比較
                                          5};    // アウトレンジ比較
        public short[] g_clock_type    = {0,     // 内部クロック
                                          1};    // 外部クロック
        public short[] g_clock_edge    = {0,     // 立ち下がりエッジ
                                          1};    // 立ち上がりエッジ
        public short[] g_stop_trigger  = {1,     // 外部トリガ立ち上がり
                                          2,     // 外部トリガ立ち下がり
                                          3,     // レベル比較
                                          4,     // コマンド（AioStopAi）
                                          5,     // インレンジ比較
                                          6};    // アウトレンジ比較

        public Condition()
        {
            InitializeComponent();
        }

        private void Condition_Load(object sender, EventArgs e)
        {
            string error_string;           // エラーコード文字列
            string message_string = "";    // メッセージ表示用
            int    ret;                    // 戻り値取得用
            short  index;                  // コンボボックスのインデックス
            short  ai_start_trigger;       // 変換開始条件
            short  ai_stop_trigger;        // 変換停止条件
            float  ai_level;               // レベル比較データ
            short  ai_direction;           // レベル比較の方向
            float  ai_range_level1;        // インレンジ/アウトレンジ比較データの上限値
            float  ai_range_level2;        // インレンジ/アウトレンジ比較データの下限値
            int    ai_state_times;         // 状態保持サンプリング回数
            short  ai_clock_type;          // クロックの種類
            float  ai_sampling_clock;      // サンプリングクロック
            short  ai_clock_edge;          // 外部クロックの入力タイミング
            float  ai_scan_clock;          // 変換速度
            short  err_flag;               // 関数の実行エラーフラグ

            //----------------------------------------
            // パラメータの初期化
            //----------------------------------------
            err_flag = 0;
            //----------------------------------------
            // アナログ入力変換開始条件用コンボボックスの作成
            //----------------------------------------
            comboBox_StartCondition.Items.Add("ソフトウェア");
            comboBox_StartCondition.Items.Add("外部トリガ立ち上がり");
            comboBox_StartCondition.Items.Add("外部トリガ立ち下がり");
            comboBox_StartCondition.Items.Add("レベル比較");
            comboBox_StartCondition.Items.Add("インレンジ比較");
            comboBox_StartCondition.Items.Add("アウトレンジ比較");
            //----------------------------------------
            // アナログ入力変換開始条件を取得
            //----------------------------------------
            ret = aio.GetAiStartTrigger(Form1.g_id, out ai_start_trigger);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // エラーメッセージ生成
                    //--------------------
                    message_string = string.Format("aio.GetAiStartTrigger = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    comboBox_StartCondition.Enabled = false;
                }
                //--------------------
                // アナログ入力変換開始条件用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_StartCondition.SelectedIndex = 0;
                comboBox_StartCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // 変換開始条件からインデックスに変換
                //--------------------
                index = 0;
                switch (ai_start_trigger)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    case 2:
                        index = 2;
                        break;
                    case 3:
                        index = 3;
                        break;
                    case 4:
                        index = 4;
                        break;
                    case 5:
                        index = 5;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // アナログ入力変換開始条件用コンボボックスの選択を変更
                //--------------------
                comboBox_StartCondition.SelectedIndex = index;
                comboBox_StartCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // チャネル番号の初期表示を更新
            //----------------------------------------
            textBox_StartLevelChNo.Text = "0";
            //----------------------------------------
            // レベル比較方向用コンボボックスの作成
            //----------------------------------------
            comboBox_StartLevelComprionDriection.Items.Add("両方");
            comboBox_StartLevelComprionDriection.Items.Add("立ち上がり");
            comboBox_StartLevelComprionDriection.Items.Add("立ち下がり");
            //----------------------------------------
            // レベル比較の場合
            //----------------------------------------
            if (ai_start_trigger == 3)
            {
                //----------------------------------------
                // レベル比較開始時のレベルを取得
                //----------------------------------------
                ret = aio.GetAiStartLevelEx(Form1.g_id, 0, out ai_level, out ai_direction);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiStartLevelEx = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        textBox_StartLevelChNo.Enabled = false;
                        textBox_StartLevelComprionValue.Enabled = false;
                        comboBox_StartLevelComprionDriection.Enabled = false;
                    }
                    //--------------------
                    // 比較値の初期表示を更新
                    //--------------------
                    textBox_StartLevelComprionValue.Text = "1.0";
                    //--------------------
                    // レベル比較方向用コンボボックスの選択を0番に変更
                    //--------------------
                    comboBox_StartLevelComprionDriection.SelectedIndex = 0;
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 比較値を更新
                    //--------------------
                    textBox_StartLevelComprionValue.Text = ai_level.ToString("F1");
                    //--------------------
                    // レベル比較方向からインデックスに変換
                    //--------------------
                    index = 0;
                    switch (ai_direction)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        case 2:
                            index = 2;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //--------------------
                    // レベル比較方向用コンボボックスの選択を変更
                    //--------------------
                    comboBox_StartLevelComprionDriection.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // レベル比較以外の場合
            //----------------------------------------
            else
            {
                //--------------------
                // 比較値の初期表示を更新
                //--------------------
                textBox_StartLevelComprionValue.Text = "1.0";
                //--------------------
                // レベル比較方向用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_StartLevelComprionDriection.SelectedIndex = 0;
            }
            //----------------------------------------
            // チャネル番号の初期表示を更新
            //----------------------------------------
            textBox_StartRangeChNo.Text = "0";
            //----------------------------------------
            // インレンジ比較の場合
            //----------------------------------------
            if (ai_start_trigger == 4)
            {
                //----------------------------------------
                // インレンジ比較開始条件の設定値を取得
                //----------------------------------------
                ret = aio.GetAiStartInRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiStartInRangeEx = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        textBox_StartRangeChNo.Enabled = false;
                        textBox_StartRangeUpperLimitValue.Enabled = false;
                        textBox_StartRangeLowerLimitValue.Enabled = false;
                        textBox_StartRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // 上限値の初期表示を更新
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // 下限値の初期表示を更新
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // 保持回数の初期表示を更新
                    //--------------------
                    textBox_StartRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 上限値を更新
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // 下限値を更新
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // 保持回数を更新
                    //--------------------
                    textBox_StartRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // アウトレンジ比較の場合
            //----------------------------------------
            else if (ai_start_trigger == 5)
            {
                //----------------------------------------
                // アウトレンジ比較開始条件の設定値を取得
                //----------------------------------------
                ret = aio.GetAiStartOutRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiStartOutRangeEx = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        textBox_StartRangeChNo.Enabled = false;
                        textBox_StartRangeUpperLimitValue.Enabled = false;
                        textBox_StartRangeLowerLimitValue.Enabled = false;
                        textBox_StartRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // 上限値の初期表示を更新
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // 下限値の初期表示を更新
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // 保持回数の初期表示を更新
                    //--------------------
                    textBox_StartRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 上限値を更新
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // 下限値を更新
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // 保持回数を更新
                    //--------------------
                    textBox_StartRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // インレンジ比較/アウトレンジ比較以外の場合
            //----------------------------------------
            else
            {
                //--------------------
                // 上限値の初期表示を更新
                //--------------------
                textBox_StartRangeUpperLimitValue.Text = "10.0";
                //--------------------
                // 下限値の初期表示を更新
                //--------------------
                textBox_StartRangeLowerLimitValue.Text = "1.0";
                //--------------------
                // 保持回数の初期表示を更新
                //--------------------
                textBox_StartRangeStateTimes.Text = "1";
            }
            //----------------------------------------
            // アナログ入力クロック種類用コンボボックスの作成
            //----------------------------------------
            comboBox_ClockType.Items.Add("内部クロック");
            comboBox_ClockType.Items.Add("外部クロック");
            //----------------------------------------
            // クロックの種類を取得
            //----------------------------------------
            ret = aio.GetAiClockType(Form1.g_id, out ai_clock_type);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    message_string = string.Format("aio.GetAiClockType = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    comboBox_ClockType.Enabled = false;
                }
                //--------------------
                // アナログ入力クロック種類用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_ClockType.SelectedIndex = 0;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // クロック種類からインデックスに変換
                //--------------------
                index = 0;
                switch (ai_clock_type)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // アナログ入力クロック種類用コンボボックスの選択を変更
                //--------------------
                comboBox_ClockType.SelectedIndex = index;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // サンプリングクロックを取得
            //----------------------------------------
            ret = aio.GetAiSamplingClock(Form1.g_id, out ai_sampling_clock);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    message_string = string.Format("aio.GetAiSamplingClock = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    textBox_SamplingClock.Enabled = false;
                }
                //--------------------
                // サンプリングクロックの初期表示を更新
                //--------------------
                textBox_SamplingClock.Text = "1000";
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // サンプリングクロックを更新
                //--------------------
                textBox_SamplingClock.Text = Convert.ToDouble(ai_sampling_clock).ToString("F6");
            }
            //----------------------------------------
            // アナログ入力外部クロック入力タイミング用コンボボックスの作成
            //----------------------------------------
            comboBox_ExternalClockInputTiming.Items.Add("立ち下がりエッジ");
            comboBox_ExternalClockInputTiming.Items.Add("立ち上がりエッジ");
            //----------------------------------------
            // 外部クロックの場合
            //----------------------------------------
            if (ai_clock_type == 1)
            {
                //----------------------------------------
                // 外部クロックの入力タイミングを取得
                //----------------------------------------
                ret = aio.GetAiClockEdge(Form1.g_id, out ai_clock_edge);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiClockEdge = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        comboBox_ExternalClockInputTiming.Enabled = false;
                    }
                    //----------------------------------------
                    // アナログ入力外部クロック入力タイミング用コンボボックスの選択を0番に変更
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = 0;
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 入力タイミングからインデックスに変換
                    //--------------------
                    index = 0;
                    switch (ai_clock_edge)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //----------------------------------------
                    // アナログ入力外部クロック入力タイミング用コンボボックスの選択を変更
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // 外部クロック以外の場合
            //----------------------------------------
            else
            {
                //----------------------------------------
                // アナログ入力外部クロック入力タイミング用コンボボックスの選択を0番に変更
                //----------------------------------------
                comboBox_ExternalClockInputTiming.SelectedIndex = 0;
            }
            //----------------------------------------
            // 変換速度を取得
            //----------------------------------------
            ret = aio.GetAiScanClock(Form1.g_id, out ai_scan_clock);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    message_string = string.Format("aio.GetAiScanClock = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    textBox_ScanClock.Enabled = false;
                }
                //--------------------
                // 変換速度の初期表示を更新
                //--------------------
                textBox_ScanClock.Text = "10";
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // 変換速度の初期表示を更新
                //--------------------
                textBox_ScanClock.Text = ai_scan_clock.ToString();
            }
            //----------------------------------------
            // アナログ入力変換停止条件用コンボボックスの作成
            //----------------------------------------
            comboBox_StopCondition.Items.Add("外部トリガ立ち上がり");
            comboBox_StopCondition.Items.Add("外部トリガ立ち下がり");
            comboBox_StopCondition.Items.Add("レベル比較");
            comboBox_StopCondition.Items.Add("コマンド（AioStopAi）");
            comboBox_StopCondition.Items.Add("インレンジ比較");
            comboBox_StopCondition.Items.Add("アウトレンジ比較");
            //----------------------------------------
            // アナログ入力変換停止条件を取得
            //----------------------------------------
            ret = aio.GetAiStopTrigger(Form1.g_id, out ai_stop_trigger);
            //----------------------------------------
            // 異常終了の処理
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    message_string = string.Format("aio.GetAiStopTrigger = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // コントロールを無効に設定
                    //--------------------
                    comboBox_StopCondition.Enabled = false;
                }
                //--------------------
                // アナログ入力変換停止条件用コンボボックスの選択を3番に変更
                //--------------------
                comboBox_StopCondition.SelectedIndex = 3;
                comboBox_StopCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // 正常終了の処理
            //----------------------------------------
            else
            {
                //--------------------
                // 変換停止条件からインデックスに変換
                //--------------------
                index = 0;
                switch (ai_stop_trigger)
                {
                    case 0:
                        index = 3;
                        break;
                    case 1:
                        index = 0;
                        break;
                    case 2:
                        index = 1;
                        break;
                    case 3:
                        index = 2;
                        break;
                    case 4:
                        index = 3;
                        break;
                    case 5:
                        index = 4;
                        break;
                    case 6:
                        index = 5;
                        break;
                    default:
                        index = 3;
                        break;
                }
                //--------------------
                // アナログ入力変換停止条件用コンボボックスの選択を変更
                //--------------------
                comboBox_StopCondition.SelectedIndex = index;
                comboBox_StopCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // チャネル番号の初期表示を更新
            //----------------------------------------
            textBox_StopLevelChNo.Text = "0";
            //----------------------------------------
            // レベル比較方向用コンボボックスの作成
            //----------------------------------------
            comboBox_StopLevelComprionDriection.Items.Add("両方");
            comboBox_StopLevelComprionDriection.Items.Add("立ち上がり");
            comboBox_StopLevelComprionDriection.Items.Add("立ち下がり");
            //----------------------------------------
            // レベル比較の場合
            //----------------------------------------
            if (ai_stop_trigger == 3)
            {
                //----------------------------------------
                // レベル比較停止時のレベルを取得
                //----------------------------------------
                ret = aio.GetAiStopLevelEx(Form1.g_id, 0, out ai_level, out ai_direction);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiStopLevelEx = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        textBox_StopLevelChNo.Enabled = false;
                        textBox_StopLevelComprionValue.Enabled = false;
                        comboBox_StopLevelComprionDriection.Enabled = false;
                    }
                    //--------------------
                    // 比較値の初期表示を更新
                    //--------------------
                    textBox_StopLevelComprionValue.Text = "1.0";
                    //--------------------
                    // レベル比較方向用コンボボックスの選択を0番に変更
                    //--------------------
                    comboBox_StopLevelComprionDriection.SelectedIndex = 0;
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 比較値を更新
                    //--------------------
                    textBox_StopLevelComprionValue.Text = ai_level.ToString("F1");
                    //--------------------
                    // レベル比較方向からインデックスに変換
                    //--------------------
                    index = 0;
                    switch (ai_direction)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        case 2:
                            index = 2;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //--------------------
                    // レベル比較方向用コンボボックスの選択を変更
                    //--------------------
                    comboBox_StopLevelComprionDriection.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // レベル比較以外の場合
            //----------------------------------------
            else
            {
                //--------------------
                // 比較値の初期表示を更新
                //--------------------
                textBox_StopLevelComprionValue.Text = "1.0";
                //--------------------
                // レベル比較方向用コンボボックスの選択を0番に変更
                //--------------------
                comboBox_StopLevelComprionDriection.SelectedIndex = 0;
            }
            //----------------------------------------
            // チャネル番号の初期表示を更新
            //----------------------------------------
            textBox_StopRangeChNo.Text = "0";
            //----------------------------------------
            // インレンジ比較の場合
            //----------------------------------------
            if (ai_stop_trigger == 5)
            {
                //----------------------------------------
                // インレンジ比較停止条件の設定値を取得
                //----------------------------------------
                ret = aio.GetAiStopInRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiStopInRangeEx = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        textBox_StopRangeChNo.Enabled = false;
                        textBox_StopRangeUpperLimitValue.Enabled = false;
                        textBox_StopRangeLowerLimitValue.Enabled = false;
                        textBox_StopRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // 上限値の初期表示を更新
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // 下限値の初期表示を更新
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // 保持回数の初期表示を更新
                    //--------------------
                    textBox_StopRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 上限値を更新
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // 下限値を更新
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // 保持回数を更新
                    //--------------------
                    textBox_StopRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // アウトレンジ比較の場合
            //----------------------------------------
            else if (ai_stop_trigger == 6)
            {
                //----------------------------------------
                // アウトレンジ比較停止条件の設定値を取得
                //----------------------------------------
                ret = aio.GetAiStopOutRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // 異常終了の処理
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        message_string = string.Format("aio.GetAiStopOutRangeEx = {0:D} : {1}\n\n※エラーが発生したため表示を更新できませんでした。", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // コントロールを無効に設定
                        //--------------------
                        textBox_StopRangeChNo.Enabled = false;
                        textBox_StopRangeUpperLimitValue.Enabled = false;
                        textBox_StopRangeLowerLimitValue.Enabled = false;
                        textBox_StopRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // 上限値の初期表示を更新
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // 下限値の初期表示を更新
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // 保持回数の初期表示を更新
                    //--------------------
                    textBox_StopRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // 正常終了の処理
                //----------------------------------------
                else
                {
                    //--------------------
                    // 上限値を更新
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // 下限値を更新
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // 保持回数を更新
                    //--------------------
                    textBox_StopRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // インレンジ比較/アウトレンジ比較以外の場合
            //----------------------------------------
            else
            {
                //--------------------
                // 上限値の初期表示を更新
                //--------------------
                textBox_StopRangeUpperLimitValue.Text = "10.0";
                //--------------------
                // 下限値の初期表示を更新
                //--------------------
                textBox_StopRangeLowerLimitValue.Text = "1.0";
                //--------------------
                // 保持回数の初期表示を更新
                //--------------------
                textBox_StopRangeStateTimes.Text = "1";
            }
            //----------------------------------------
            // エラーが発生していた場合メッセージを表示
            //----------------------------------------
            if (err_flag != 0)
            {
                MessageBox.Show(message_string, "エラー", MessageBoxButtons.OK);
            }
        }

        //================================================================================
        // 開始条件コンボボックスのインデックスが変更された際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void comboBox_StartCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;    // コンボボックスのインデックス

            //----------------------------------------
            // コンボボックスから開始条件のインデックスを取得
            //----------------------------------------
            index = comboBox_StartCondition.SelectedIndex;
            //----------------------------------------
            // 開始条件による分岐処理
            //----------------------------------------
            switch (g_start_trigger[index])
            {
                case 0:    // ソフトウェア
                case 1:    // 外部トリガ立ち上がり
                case 2:    // 外部トリガ立ち下がり
                    textBox_StartLevelChNo.Enabled = false;
                    textBox_StartLevelComprionValue.Enabled = false;
                    comboBox_StartLevelComprionDriection.Enabled = false;
                    textBox_StartRangeChNo.Enabled = false;
                    textBox_StartRangeUpperLimitValue.Enabled = false;
                    textBox_StartRangeLowerLimitValue.Enabled = false;
                    textBox_StartRangeStateTimes.Enabled = false;
                    break;
                case 3:    // レベル比較
                    textBox_StartLevelChNo.Enabled = true;
                    textBox_StartLevelComprionValue.Enabled = true;
                    comboBox_StartLevelComprionDriection.Enabled = true;
                    textBox_StartRangeChNo.Enabled = false;
                    textBox_StartRangeUpperLimitValue.Enabled = false;
                    textBox_StartRangeLowerLimitValue.Enabled = false;
                    textBox_StartRangeStateTimes.Enabled = false;
                    break;
                case 4:    // インレンジ比較
                case 5:    // アウトレンジ比較
                    textBox_StartLevelChNo.Enabled = false;
                    textBox_StartLevelComprionValue.Enabled = false;
                    comboBox_StartLevelComprionDriection.Enabled = false;
                    textBox_StartRangeChNo.Enabled = true;
                    textBox_StartRangeUpperLimitValue.Enabled = true;
                    textBox_StartRangeLowerLimitValue.Enabled = true;
                    textBox_StartRangeStateTimes.Enabled = true;
                    break;
                default:
                    textBox_StartLevelChNo.Enabled = false;
                    textBox_StartLevelComprionValue.Enabled = false;
                    comboBox_StartLevelComprionDriection.Enabled = false;
                    textBox_StartRangeChNo.Enabled = false;
                    textBox_StartRangeUpperLimitValue.Enabled = false;
                    textBox_StartRangeLowerLimitValue.Enabled = false;
                    textBox_StartRangeStateTimes.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // クロック種類コンボボックスのインデックスが変更された際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void comboBox_ClockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;    // コンボボックスのインデックス

            //----------------------------------------
            // コンボボックスからクロック種類のインデックスを取得
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // クロック種類による分岐処理
            //----------------------------------------
            switch (g_clock_type[index])
            {
                case 0:    // 内部クロック
                    textBox_SamplingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    break;
                case 1:    // 外部クロック
                    textBox_SamplingClock.Enabled = false;
                    comboBox_ExternalClockInputTiming.Enabled = true;
                    break;
                default:
                    textBox_SamplingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // 停止条件コンボボックスのインデックスが変更された際の処理になります
        // 使用可能コントロールを更新します
        //================================================================================
        private void comboBox_StopCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;    // コンボボックスのインデックス

            //----------------------------------------
            // コンボボックスから停止条件のインデックスを取得
            //----------------------------------------
            index = comboBox_StopCondition.SelectedIndex;
            //----------------------------------------
            // 停止条件による分岐処理
            //----------------------------------------
            switch (g_stop_trigger[index])
            {
                case 1:    // 外部トリガ立ち上がり
                case 2:    // 外部トリガ立ち下がり
                case 4:    // コマンド（AioStopAi）
                    textBox_StopLevelChNo.Enabled = false;
                    textBox_StopLevelComprionValue.Enabled = false;
                    comboBox_StopLevelComprionDriection.Enabled = false;
                    textBox_StopRangeChNo.Enabled = false;
                    textBox_StopRangeUpperLimitValue.Enabled = false;
                    textBox_StopRangeLowerLimitValue.Enabled = false;
                    textBox_StopRangeStateTimes.Enabled = false;
                    break;
                case 3:    // レベル比較
                    textBox_StopLevelChNo.Enabled = true;
                    textBox_StopLevelComprionValue.Enabled = true;
                    comboBox_StopLevelComprionDriection.Enabled = true;
                    textBox_StopRangeChNo.Enabled = false;
                    textBox_StopRangeUpperLimitValue.Enabled = false;
                    textBox_StopRangeLowerLimitValue.Enabled = false;
                    textBox_StopRangeStateTimes.Enabled = false;
                    break;
                case 5:    // インレンジ比較
                case 6:    // アウトレンジ比較
                    textBox_StopLevelChNo.Enabled = false;
                    textBox_StopLevelComprionValue.Enabled = false;
                    comboBox_StopLevelComprionDriection.Enabled = false;
                    textBox_StopRangeChNo.Enabled = true;
                    textBox_StopRangeUpperLimitValue.Enabled = true;
                    textBox_StopRangeLowerLimitValue.Enabled = true;
                    textBox_StopRangeStateTimes.Enabled = true;
                    break;
                default:
                    textBox_StopLevelChNo.Enabled = false;
                    textBox_StopLevelComprionValue.Enabled = false;
                    comboBox_StopLevelComprionDriection.Enabled = false;
                    textBox_StopRangeChNo.Enabled = false;
                    textBox_StopRangeUpperLimitValue.Enabled = false;
                    textBox_StopRangeLowerLimitValue.Enabled = false;
                    textBox_StopRangeStateTimes.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // OKボタンをクリックした際の処理になります
        // サンプリング条件の設定を行います
        //================================================================================
        private void button_Ok_Click(object sender, EventArgs e)
        {
            string error_string;         // エラーコード文字列
            string text_string = "";     // テキストボックス更新用
            int    ret;                  // 戻り値取得用
            int    index;                // コンボボックスのインデックス
            short  ai_level_channel;     // レベル比較チャネル
            float  ai_level;             // レベル比較データ
            short  ai_direction;         // レベル比較の方向
            short  ai_range_channel;     // インレンジ/アウトレンジ比較チャネル
            float  ai_range_level1;      // インレンジ/アウトレンジ比較データの上限値
            float  ai_range_level2;      // インレンジ/アウトレンジ比較データの下限値
            int    ai_state_times;       // 状態保持サンプリング回数
            float  ai_sampling_clock;    // サンプリングクロック
            float  ai_scan_clock;        // 変換速度

            //----------------------------------------
            // アナログ入力の変換データ転送方式をデバイスバッファモードに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiTransferMode(Form1.g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = string.Format("aio.SetAiTransferMode = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // データ格納用メモリ形式をFIFOに設定
            // 変更要注意関数(変更するとアプリが動作しなくなる可能性があります)
            //----------------------------------------
            ret = aio.SetAiMemoryType(Form1.g_id, 0);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = string.Format("aio.SetAiMemoryType = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // コンボボックスから変換開始条件設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_StartCondition.SelectedIndex;
            //----------------------------------------
            // アナログ入力変換開始条件の設定
            //----------------------------------------
            ret = aio.SetAiStartTrigger(Form1.g_id, g_start_trigger[index]);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = string.Format("aio.SetAiStartTrigger = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // レベル比較の場合
            //----------------------------------------
            if (g_start_trigger[index] == 3)
            {
                //----------------------------------------
                // テキストボックスからレベル比較チャネルを取得
                //----------------------------------------
                if (short.TryParse(textBox_StartLevelChNo.Text, out ai_level_channel) == false)
                {
                    ai_level_channel = 0;
                }
                //----------------------------------------
                // テキストボックスからレベル比較データを取得
                //----------------------------------------
                if (float.TryParse(textBox_StartLevelComprionValue.Text, out ai_level) == false)
                {
                    ai_level = -10;
                }
                //----------------------------------------
                // コンボボックスからレベル比較の方向を取得
                //----------------------------------------
                ai_direction = (short)comboBox_StartLevelComprionDriection.SelectedIndex;
                //----------------------------------------
                // レベル比較開始時のレベルを設定
                //----------------------------------------
                ret = aio.SetAiStartLevelEx(Form1.g_id, ai_level_channel, ai_level, ai_direction);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiStartLevelEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // インレンジ比較の場合
            //----------------------------------------
            else if (g_start_trigger[index] == 4)
            {
                //----------------------------------------
                // テキストボックスからインレンジ比較チャネルを取得
                //----------------------------------------
                if (short.TryParse(textBox_StartRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // テキストボックスから上限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // テキストボックスから下限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // テキストボックスから保持回数を取得
                //----------------------------------------
                if (int.TryParse(textBox_StartRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // インレンジ比較開始条件の設定を行う
                //----------------------------------------
                ret = aio.SetAiStartInRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiStartInRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // アウトレンジ比較の場合
            //----------------------------------------
            else if (g_start_trigger[index] == 5)
            {
                //----------------------------------------
                // テキストボックスからアウトレンジ比較チャネルを取得
                //----------------------------------------
                if (short.TryParse(textBox_StartRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // テキストボックスから上限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // テキストボックスから下限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // テキストボックスから保持回数を取得
                //----------------------------------------
                if (int.TryParse(textBox_StartRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // アウトレンジ比較開始条件の設定を行う
                //----------------------------------------
                ret = aio.SetAiStartOutRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiStartOutRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // コンボボックスからクロック種類設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // アナログ入力クロック種類の設定
            //----------------------------------------
            ret = aio.SetAiClockType(Form1.g_id, g_clock_type[index]);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = string.Format("aio.SetAiClockType = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // 内部クロックの場合
            //----------------------------------------
            if (g_clock_type[index] == 0)
            {
                //----------------------------------------
                // テキストボックスからサンプリングクロックを取得
                //----------------------------------------
                if (float.TryParse(textBox_SamplingClock.Text, out ai_sampling_clock) == false)
                {
                    ai_sampling_clock = 1000;
                }
                //----------------------------------------
                // アナログ入力サンプリングクロックの設定
                //----------------------------------------
                ret = aio.SetAiSamplingClock(Form1.g_id, ai_sampling_clock);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiSamplingClock = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // 外部クロックの場合
            //----------------------------------------
            else if (g_clock_type[index] == 1)
            {
                //----------------------------------------
                // コンボボックスから外部クロック入力タイミング設定値のインデックスを取得
                //----------------------------------------
                index = comboBox_ExternalClockInputTiming.SelectedIndex;
                //----------------------------------------
                // 外部クロックの入力タイミングの設定
                //----------------------------------------
                ret = aio.SetAiClockEdge(Form1.g_id, g_clock_edge[index]);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiClockEdge = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // 変換速度機能が使用可能な場合
            //----------------------------------------
            if (textBox_ScanClock.Enabled != false)
            {
                //----------------------------------------
                // テキストボックスから変換速度を取得
                //----------------------------------------
                if (float.TryParse(textBox_ScanClock.Text, out ai_scan_clock) == false)
                {
                    ai_scan_clock = 2;
                }
                //----------------------------------------
                // アナログ入力変換速度の設定
                //----------------------------------------
                ret = aio.SetAiScanClock(Form1.g_id, ai_scan_clock);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiScanClock = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // コンボボックスから変換停止条件設定値のインデックスを取得
            //----------------------------------------
            index = comboBox_StopCondition.SelectedIndex;
            //----------------------------------------
            // アナログ入力変換停止条件の設定
            //----------------------------------------
            ret = aio.SetAiStopTrigger(Form1.g_id, g_stop_trigger[index]);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                text_string = string.Format("aio.SetAiStopTrigger = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // レベル比較の場合
            //----------------------------------------
            if (g_stop_trigger[index] == 3)
            {
                //----------------------------------------
                // テキストボックスからレベル比較チャネルを取得
                //----------------------------------------
                if (short.TryParse(textBox_StopLevelChNo.Text, out ai_level_channel) == false)
                {
                    ai_level_channel = 0;
                }
                //----------------------------------------
                // テキストボックスからレベル比較データを取得
                //----------------------------------------
                if (float.TryParse(textBox_StopLevelComprionValue.Text, out ai_level) == false)
                {
                    ai_level = -10;
                }
                //----------------------------------------
                // コンボボックスからレベル比較の方向を取得
                //----------------------------------------
                ai_direction = (short)comboBox_StopLevelComprionDriection.SelectedIndex;
                //----------------------------------------
                // レベル比較停止時のレベルを設定
                //----------------------------------------
                ret = aio.SetAiStopLevelEx(Form1.g_id, ai_level_channel, ai_level, ai_direction);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiStopLevelEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // インレンジ比較の場合
            //----------------------------------------
            else if (g_stop_trigger[index] == 5)
            {
                //----------------------------------------
                // テキストボックスからインレンジ比較チャネルを取得
                //----------------------------------------
                if (short.TryParse(textBox_StopRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // テキストボックスから上限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // テキストボックスから下限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // テキストボックスから保持回数を取得
                //----------------------------------------
                if (int.TryParse(textBox_StopRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // インレンジ比較停止条件の設定を行う
                //----------------------------------------
                ret = aio.SetAiStopInRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiStopInRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // アウトレンジ比較の場合
            //----------------------------------------
            else if (g_stop_trigger[index] == 6)
            {
                //----------------------------------------
                // テキストボックスからアウトレンジ比較チャネルを取得
                //----------------------------------------
                if (short.TryParse(textBox_StopRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // テキストボックスから上限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // テキストボックスから下限値を取得
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // テキストボックスから保持回数を取得
                //----------------------------------------
                if (int.TryParse(textBox_StopRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // アウトレンジ比較停止条件の設定を行う
                //----------------------------------------
                ret = aio.SetAiStopOutRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // エラー処理
                //----------------------------------------
                //--------------------
                // 正常終了でなければ処理を抜ける
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    text_string = string.Format("aio.SetAiStopOutRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "エラー", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // Zシリーズにおいて、バスマスタ用バッファが最大1MByte以下に制限される場合があります
            // 実システムでは、必要に応じてバッファサイズの変更処理を実装してください
            //----------------------------------------
            //ret = aio.SetAiMemorySize(g_id, 750 * 1024);
            //----------------------------------------
            // OK処理
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // Cancelボタンをクリックした際の処理になります
        // ダイアログを終了します
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Cancel処理
            //----------------------------------------
            this.Close();
        }
    }
}
