﻿using CaioCs;

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

        //----------------------------------------
        // グローバル変数の宣言
        //----------------------------------------
        public static short g_id;                        // デバイスアクセス用ID
        public static short g_channel;                   // カウンタのチャネル
        public int          g_event_count;               // 比較カウント一致イベント発生回数
        public static int   g_compare_data_num = 1;      // 設定する比較カウントデータの総数
        public static short g_compare_repeat   = 0;      // 比較カウント値データ配列の繰り返し設定
        public static int   g_compare_data_0   = 100;    // 比較カウント値0
        public static int   g_compare_data_1   = 200;    // 比較カウント値1
        public static int   g_compare_data_2   = 300;    // 比較カウント値2
        public static int   g_compare_data_3   = 400;    // 比較カウント値3
        public static int   g_compare_data_4   = 500;    // 比較カウント値4
        public static int   g_preset_data_num  = 1;      // 設定するプリセットデータの総数
        public static short g_preset_repeat    = 0;      // プリセットデータ配列の繰り返し設定
        public static int   g_preset_data_0    = 0;      // カウンタのプリセット値0
        public static int   g_preset_data_1    = 100;    // カウンタのプリセット値1
        public static int   g_preset_data_2    = 200;    // カウンタのプリセット値2
        public static int   g_preset_data_3    = 300;    // カウンタのプリセット値3
        public static int   g_preset_data_4    = 400;    // カウンタのプリセット値4

        public Form1()
        {
            InitializeComponent();
        }

        private void Counter_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // デフォルトボタンを設定
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // デバイス名の初期表示を更新
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // カウンタのチャネルの初期表示を更新
            //----------------------------------------
            textBox_ChannelNo.Text = "0";
            //----------------------------------------
            // カウンタのプリセットの初期表示を更新
            //----------------------------------------
            textBox_PresetData.Text = "0";
            //----------------------------------------
            // 現在のカウント値の初期表示を更新
            //----------------------------------------
            textBox_CountData.Text = "0";
            //----------------------------------------
            // ステータスの初期表示を更新
            //----------------------------------------
            textBox_Status.Text = "";
            //----------------------------------------
            // 比較カウント一致イベント発生回数の初期表示を更新
            //----------------------------------------
            textBox_EventCount.Text = "0";
            //----------------------------------------
            // グローバル変数の初期化
            //----------------------------------------
            g_id          = 0;
            g_channel     = 0;
            g_event_count = 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_AioResetDevice_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;
        }

        //================================================================================
        // チャネル番号テキストが変更された際の処理になります
        // チャネル番号を設定します
        //================================================================================
        private void textBox_ChannelNo_TextChanged(object sender, EventArgs e)
        {
            //----------------------------------------
            // テキストボックスからカウンタのチャネル番号を取得
            //----------------------------------------
            if (short.TryParse(textBox_ChannelNo.Text, out g_channel) == false)
            {
                g_channel = 0;
            }
        }

        //================================================================================
        // 設定...ボタンをクリックした際の処理になります
        // カウンタ設定ダイアログを開きます
        //================================================================================
        private void button_Setting_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // 設定ダイアログを開く
            //----------------------------------------
            Setting setting_dlg = new Setting();
            if (setting_dlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // 設定完了のメッセージ表示
                //----------------------------------------
                textBox_ReturnCode.Text = "設定：正常終了";
            }
            setting_dlg.Dispose();
        }

        //================================================================================
        // AioPresetCntボタンをクリックした際の処理になります
        // カウンタをプリセットします
        //================================================================================
        private void button_PresetCnt_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            int    ret;             // 戻り値取得用
            int    preset_data;     // カウンタのプリセット
            int    cnt_count;       // カウント値

            //----------------------------------------
            // テキストボックスからカウンタのプリセット値を取得
            //----------------------------------------
            if (int.TryParse(textBox_PresetData.Text, out preset_data) == false)
            {
                preset_data = 0;
            }
            //----------------------------------------
            // カウンタをプリセット
            //----------------------------------------
            ret = aio.PresetCnt(g_id, g_channel, preset_data);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.PresetCnt = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // カウント値プリセット関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // カウント値の取得
                //--------------------
                ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
                //--------------------
                // エラー処理
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // エラー文字列取得
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // メッセージ表示
                    //--------------------
                    textBox_ReturnCode.Text = "aio.GetCntCount = " + ret.ToString() + "：" + error_string;
                    return;
                }
                //--------------------
                // カウント値を更新
                //--------------------
                textBox_CountData.Text = cnt_count.ToString();
            }
        }

        //================================================================================
        // カウント開始ボタンをクリックした際の処理になります
        // カウンタ動作を開始します
        //================================================================================
        private void button_Start_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    cnt_status;      // カウンタステータス
            int    ret;             // 戻り値取得用
            int    cnt_count;       // カウント値

            //----------------------------------------
            // イベント要因の設定：比較カウント一致イベント、カウンタ動作エラーイベント
            //----------------------------------------
            ret = aio.SetCntEvent(g_id, g_channel, Handle, (int)(CaioConst.CNTE_DATA_NUM | CaioConst.CNTE_ERR));
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント開始：aio.SetCntEvent = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスリセット
            //----------------------------------------
            ret = aio.ResetCntStatus(g_id, g_channel, (int)(CaioConst.CNTS_DATA_NUM | CaioConst.CNTS_ORERR));
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント開始：aio.ResetCntStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // 比較一致回数を初期化
            //----------------------------------------
            g_event_count = 0;
            //----------------------------------------
            // カウンタ動作を開始
            //----------------------------------------
            ret = aio.StartCnt(g_id, g_channel);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント開始：aio.StartCnt = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスの取得
            //----------------------------------------
            ret = aio.GetCntStatus(g_id, g_channel, out cnt_status);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント開始：aio.GetCntStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスの表示
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", cnt_status);
            //--------------------
            // 動作中ステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
            {
                text_string += "動作中";
            }
            //--------------------
            // 動作中ステータス無効時
            //--------------------
            else
            {
                text_string += "停止中";
            }
            //--------------------
            // 比較カウント一致ステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
            {
                text_string += "、比較カウント一致";
            }
            //--------------------
            // オーバーランステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
            {
                text_string += "、オーバーラン";
            }
            //--------------------
            // カウンタ動作エラーステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
            {
                text_string += "、カウンタ動作エラー";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // カウント値の取得
            //----------------------------------------
            ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント開始：aio.GetCntCount = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // カウント値の表示
            //----------------------------------------
            textBox_CountData.Text = cnt_count.ToString();
            //----------------------------------------
            // 比較一致回数を更新
            //----------------------------------------
            textBox_EventCount.Text = "0";
            //----------------------------------------
            // カウント開始完了のメッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "カウント開始：正常終了";
        }

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

            //----------------------------------------
            // カウント値の取得
            //----------------------------------------
            ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetCntCount = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // カウント値取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // カウント値を更新
                //--------------------
                textBox_CountData.Text = cnt_count.ToString();
            }
        }

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

            //----------------------------------------
            // ステータスの取得
            //----------------------------------------
            ret = aio.GetCntStatus(g_id, g_channel, out cnt_status);
            //----------------------------------------
            // エラー文字列取得
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // メッセージ表示
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetCntStatus = " + ret.ToString() + "：" + error_string;
            //----------------------------------------
            // ステータス取得関数が正常終了した場合
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // ステータスの表示
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", cnt_status);
                //--------------------
                // 動作中ステータス有効時
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
                {
                    text_string += "動作中";
                }
                //--------------------
                // 動作中ステータス無効時
                //--------------------
                else
                {
                    text_string += "停止中";
                }
                //--------------------
                // 比較カウント一致ステータス有効時
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
                {
                    text_string += "、比較カウント一致";
                }
                //--------------------
                // オーバーランステータス有効時
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
                {
                    text_string += "、オーバーラン";
                }
                //--------------------
                // カウンタ動作エラーステータス有効時
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
                {
                    text_string += "、カウンタ動作エラー";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
            }
        }

        //================================================================================
        // カウント停止ボタンをクリックした際の処理になります
        // カウンタ動作を停止します
        //================================================================================
        private void button_Stop_Click(object sender, EventArgs e)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            int    cnt_count;       // カウント値
            int    cnt_status;      // ステータス

            //----------------------------------------
            // カウンタ動作を停止
            //----------------------------------------
            ret = aio.StopCnt(g_id, g_channel);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント停止：aio.StopCnt = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // カウント値の取得
            //----------------------------------------
            ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント停止：aio.GetCntCount = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // カウント値の表示
            //----------------------------------------
            textBox_CountData.Text = cnt_count.ToString();
            //----------------------------------------
            // ステータスの取得
            //----------------------------------------
            ret = aio.GetCntStatus(g_id, g_channel, out cnt_status);
            //----------------------------------------
            // エラー処理
            //----------------------------------------
            //--------------------
            // 正常終了でなければ処理を抜ける
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // エラー文字列取得
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // メッセージ表示
                //--------------------
                textBox_ReturnCode.Text = "カウント停止：aio.GetCntStatus = " + ret.ToString() + "：" + error_string;
                return;
            }
            //----------------------------------------
            // ステータスの表示
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", cnt_status);
            //--------------------
            // 動作中ステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
            {
                text_string += "動作中";
            }
            //--------------------
            // 動作中ステータス無効時
            //--------------------
            else
            {
                text_string += "停止中";
            }
            //--------------------
            // 比較カウント一致ステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
            {
                text_string += "、比較カウント一致";
            }
            //--------------------
            // オーバーランステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
            {
                text_string += "、オーバーラン";
            }
            //--------------------
            // カウンタ動作エラーステータス有効時
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
            {
                text_string += "、カウンタ動作エラー";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // カウント停止完了のメッセージ表示
            //----------------------------------------
            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)
        {
            //----------------------------------------
            // 終了処理の実行
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // ダイアログを終了する
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // イベントが発生した時の動作
        // 各イベントが発生したとき、イベント毎に処理を行います
        //================================================================================
        protected override void WndProc(ref Message m)
        {
            string error_string;    // エラーコード文字列
            string text_string;     // テキストボックス更新用
            int    ret;             // 戻り値取得用
            int    cnt_count;       // カウント値
            int    cnt_status;      // ステータス
            short  cnt_channel;     // チャネル

            //----------------------------------------
            // 比較カウント一致イベント
            // データの取得、表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_CNTE_DATA_NUM)
            {
                //----------------------------------------
                // カウンタチャネルの取得
                //----------------------------------------
                cnt_channel = (short)((int)m.WParam >> 16);
                if (cnt_channel == g_channel)
                {
                    //----------------------------------------
                    // カウント値の取得
                    //----------------------------------------
                    ret = aio.GetCntCount(g_id, cnt_channel, out cnt_count);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    //--------------------
                    // 正常終了でなければ処理を抜ける
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_ReturnCode.Text = "比較カウント一致イベント処理：aio.GetCntCount = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // カウント値の表示
                    //----------------------------------------
                    textBox_CountData.Text = cnt_count.ToString();
                    //----------------------------------------
                    // ステータスの取得
                    //----------------------------------------
                    ret = aio.GetCntStatus(g_id, cnt_channel, out cnt_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    //--------------------
                    // 正常終了でなければ処理を抜ける
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_ReturnCode.Text = "比較カウント一致イベント処理：aio.GetCntStatus = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // ステータスの表示
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", cnt_status);
                    //--------------------
                    // 動作中ステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
                    {
                        text_string += "動作中";
                    }
                    //--------------------
                    // 動作中ステータス無効時
                    //--------------------
                    else
                    {
                        text_string += "停止中";
                    }
                    //--------------------
                    // 比較カウント一致ステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
                    {
                        text_string += "、比較カウント一致";
                    }
                    //--------------------
                    // オーバーランステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
                    {
                        text_string += "、オーバーラン";
                    }
                    //--------------------
                    // カウンタ動作エラーステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
                    {
                        text_string += "、カウンタ動作エラー";
                    }
                    text_string += ")";
                    textBox_Status.Text = text_string;
                    //----------------------------------------
                    // 比較カウント一致イベント発生回数を表示
                    //----------------------------------------
                    g_event_count++;
                    textBox_EventCount.Text = g_event_count.ToString();
                    //----------------------------------------
                    // ステータスリセット
                    //----------------------------------------
                    ret = aio.ResetCntStatus(g_id, cnt_channel, (int)CaioConst.CNTS_DATA_NUM);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    //--------------------
                    // 正常終了でなければ処理を抜ける
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_ReturnCode.Text = "比較カウント一致イベント処理：aio.ResetCntStatus = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_ReturnCode.Text = "比較カウント一致イベント処理：正常終了";
                }
                return;
            }
            //----------------------------------------
            // カウンタ動作エラーイベント
            // ステータスの表示を行う
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_CNTE_ERR)
            {
                //----------------------------------------
                // カウンタチャネルの取得
                //----------------------------------------
                cnt_channel = (short)((int)m.WParam >> 16);
                if (cnt_channel == g_channel)
                {
                    //----------------------------------------
                    // ステータスの取得
                    //----------------------------------------
                    ret = aio.GetCntStatus(g_id, cnt_channel, out cnt_status);
                    //----------------------------------------
                    // エラー処理
                    //----------------------------------------
                    //--------------------
                    // 正常終了でなければ処理を抜ける
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // エラー文字列取得
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // メッセージ表示
                        //--------------------
                        textBox_ReturnCode.Text = "カウンタ動作エラー発生処理：aio.GetCntStatus = " + ret.ToString() + "：" + error_string;
                        return;
                    }
                    //----------------------------------------
                    // ステータスの表示
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", cnt_status);
                    //--------------------
                    // 動作中ステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
                    {
                        text_string += "動作中";
                    }
                    //--------------------
                    // 動作中ステータス無効時
                    //--------------------
                    else
                    {
                        text_string += "停止中";
                    }
                    //--------------------
                    // 比較カウント一致ステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
                    {
                        text_string += "、比較カウント一致";
                    }
                    //--------------------
                    // オーバーランステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
                    {
                        text_string += "、オーバーラン";
                    }
                    //--------------------
                    // カウンタ動作エラーステータス有効時
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
                    {
                        text_string += "、カウンタ動作エラー";
                    }
                    text_string += ")";
                    textBox_Status.Text = text_string;
                    //----------------------------------------
                    // イベント処理完了のメッセージ表示
                    //----------------------------------------
                    textBox_ReturnCode.Text = "カウンタ動作エラー発生処理：正常終了";
                }
                return;
            }
            base.WndProc(ref m);
        }
    }
}