﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
// Counter headder
using CcntCs;

namespace Sync
{
    public partial class MSampMode : Form
    {
        public struct StrExtNum
        {
            public string Str;
            public short  Num;
            public short  ExtSig;
            public StrExtNum(string Str, short Num, short ExtSig)
            {
                this.Str    = Str;
                this.Num    = Num;
                this.ExtSig = ExtSig;
            }
        };

        // Sampling Start
        static StrExtNum[] Start =
        {
            new StrExtNum("ソフトウェアスタート",          (short)CcntConst.CNTS_START_SOFT             , (short)CcntConst.CNTS_EXT_START_SOFT           ),
            new StrExtNum("外部信号立ち上がり",            (short)CcntConst.CNTS_START_EXT_RISE         , (short)CcntConst.CNTS_EXT_START_EXT_RISE       ),
            new StrExtNum("外部信号立ち下がり",            (short)CcntConst.CNTS_START_EXT_FALL         , (short)CcntConst.CNTS_EXT_START_EXT_FALL       ),
            new StrExtNum("カウント一致CH0 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG0    ),
            new StrExtNum("カウント一致CH1 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG0    ),
            new StrExtNum("カウント一致CH2 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG0    ),
            new StrExtNum("カウント一致CH3 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG0    ),
            new StrExtNum("カウント一致CH4 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG0    ),
            new StrExtNum("カウント一致CH5 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG0    ),
            new StrExtNum("カウント一致CH6 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG0    ),
            new StrExtNum("カウント一致CH7 比較レジスタ0", (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG0, (short)CcntConst.CNTS_EXT_CNTMATCH_CH7_REG0    ),
            new StrExtNum("カウント一致CH0 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG1    ),
            new StrExtNum("カウント一致CH1 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG1    ),
            new StrExtNum("カウント一致CH2 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG1    ),
            new StrExtNum("カウント一致CH3 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG1    ),
            new StrExtNum("カウント一致CH4 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG1    ),
            new StrExtNum("カウント一致CH5 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG1    ),
            new StrExtNum("カウント一致CH6 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG1    ),
            new StrExtNum("カウント一致CH7 比較レジスタ1", (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG1, (short)CcntConst.CNTS_EXT_CNTMATCH_CH7_REG1    ),
            new StrExtNum("End",                           0                                            , 0                                              )
        };
        // Counter Start
        static StrExtNum[] StartWith =
        {
            new StrExtNum("サンプリングとは無関係に開始",  (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP, 0                                              ),
            new StrExtNum("サンプリングと同時に開始",      (short)CcntConst.CNTS_START_CNT_WITH_SAMP    , 0                                              ),
            new StrExtNum("End",                           0                                            , 0                                              )
        };
        // Sampling Clock
        static StrExtNum[] Clock =
        {
            new StrExtNum("内部クロック",                  (short)CcntConst.CNTS_CLOCK_CLOCK            , (short)CcntConst.CNTS_EXT_CLOCK                ),
            new StrExtNum("外部クロック",                  (short)CcntConst.CNTS_CLOCK_EXT_TRG          , (short)CcntConst.CNTS_EXT_EXT_TRG              ),
            new StrExtNum("End",                           0                                            , 0                              )
        };
        // Sampling Stop
        static StrExtNum[] Stop =
        {
            new StrExtNum("ソフトウェアストップ",          (short)CcntConst.CNTS_STOP_SOFT             ,  (short)CcntConst.CNTS_EXT_STOP_SOFT            ),
            new StrExtNum("外部信号立ち上がり",            (short)CcntConst.CNTS_STOP_EXT_RISE         ,  (short)CcntConst.CNTS_EXT_STOP_EXT_RISE        ),
            new StrExtNum("外部信号立ち下がり",            (short)CcntConst.CNTS_STOP_EXT_FALL         ,  (short)CcntConst.CNTS_EXT_STOP_EXT_FALL        ),
            new StrExtNum("指定個数転送完了",              (short)CcntConst.CNTS_STOP_NUM              ,  (short)CcntConst.CNTS_EXT_TRNSNUM              ),
            new StrExtNum("カウント一致CH0 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG0    ),
            new StrExtNum("カウント一致CH1 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG0    ),
            new StrExtNum("カウント一致CH2 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG0    ),
            new StrExtNum("カウント一致CH3 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG0    ),
            new StrExtNum("カウント一致CH4 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG0    ),
            new StrExtNum("カウント一致CH5 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG0    ),
            new StrExtNum("カウント一致CH6 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG0    ),
            new StrExtNum("カウント一致CH7 比較レジスタ0", (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG0,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH7_REG0    ),
            new StrExtNum("カウント一致CH0 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG1    ),
            new StrExtNum("カウント一致CH1 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG1    ),
            new StrExtNum("カウント一致CH2 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG1    ),
            new StrExtNum("カウント一致CH3 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG1    ),
            new StrExtNum("カウント一致CH4 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG1    ),
            new StrExtNum("カウント一致CH5 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG1    ),
            new StrExtNum("カウント一致CH6 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG1    ),
            new StrExtNum("カウント一致CH7 比較レジスタ1", (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG1,  (short)CcntConst.CNTS_EXT_CNTMATCH_CH7_REG1    ),
            new StrExtNum("End",                           0                                           ,  0                                              )
        };
        // Counter Stop
        static StrExtNum[] StopWith =
        {
            new StrExtNum("サンプリングとは無関係に停止",  (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP,  0                                              ),
            new StrExtNum("サンプリングと同時に停止",      (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP    ,  0                                              ),
            new StrExtNum("End",                           0                                           ,  0                                              )
        };

        static string       szStart       = "ソフトウェアスタート";
        static string       szStartWith   = "サンプリングとは無関係に開始";
        static string       szClock       = "内部クロック";
        static string       szStop        = "ソフトウェアストップ";
        static string       szStopWith    = "サンプリングとは無関係に停止";
        static uint         StartComp     = 0x100;
        static uint         InternalClock = 1000;
        static uint         StopComp      = 0x200;
        static uint         StopNum       = 1000;
        public static uint  M_NotifyCount = 500;
        public static short M_IsRing      = (short)CcntConst.CNTS_WRITE_ONCE;
        Ccnt                cnt           = new Ccnt();

        static short str_to_num(StrExtNum[] strnum, string str)
        {
            ushort i;
            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (string.Compare(strnum[i].Str, str) == 0)
                {
                    return strnum[i].Num;
                }
            }
            return 0;
        }

        static short str_to_extsig(StrExtNum[] strnum, string str)
        {
            ushort i;
            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (string.Compare(strnum[i].Str, str) == 0)
                {
                    return strnum[i].ExtSig;
                }
            }
            return 0;
        }

        static string num_to_str(StrExtNum[] strnum, short num)
        {
            ushort i;
            for (i = 0; string.Compare(strnum[i].Str, "End") != 0; i++)
            {
                if (strnum[i].Num == num)
                {
                    return strnum[i].Str;
                }
            }
            return strnum[0].Str;
        }

        public MSampMode()
        {
            InitializeComponent();
        }

        //===================================================
        // ダイアログ初期化
        //===================================================
        private void MSampMode_Load(object sender, System.EventArgs e)
        {
            short  i;
            string szBuf = new string(' ', 100);

            //--------------------------------
            // 表示初期化
            //--------------------------------
            // Start
            for (i = 0; string.Compare(Start[i].Str, "End") != 0; i++)
            {
                cboStart.Items.Add(Start[i].Str);
            }
            // Start with
            for (i = 0; string.Compare(StartWith[i].Str, "End") != 0; i++)
            {
                cboStartWith.Items.Add(StartWith[i].Str);
            }
            // Clock
            for (i = 0; string.Compare(Clock[i].Str, "End") != 0; i++)
            {
                cboClock.Items.Add(Clock[i].Str);
            }
            // Stop
            for (i = 0; string.Compare(Stop[i].Str, "End") != 0; i++)
            {
                cboStop.Items.Add(Stop[i].Str);
            }
            // Stop with
            for (i = 0; string.Compare(StopWith[i].Str, "End") != 0; i++)
            {
                cboStopWith.Items.Add(StopWith[i].Str);
            }

            cboStart.Text     = szStart;
            cboStartWith.Text = szStartWith;
            cboClock.Text     = szClock;
            cboStop.Text      = szStop;
            cboStopWith.Text  = szStopWith;

            szBuf = string.Format("{0:X}", StartComp);
            txtCompStart.Text = szBuf;
            szBuf = InternalClock.ToString();
            txtInternalClock.Text = szBuf;
            szBuf = string.Format("{0:X}", StopComp);
            txtCompStop.Text = szBuf;
            szBuf = StopNum.ToString();
            txtStopNumber.Text = szBuf;
            szBuf = M_NotifyCount.ToString();
            txtNumberData.Text = szBuf;

            if (M_IsRing == (short)CcntConst.CNTS_WRITE_ONCE)
            {
                rdoOnce.Checked = true;
                rdoRing.Checked = false;
            }
            else
            {
                rdoOnce.Checked = false;
                rdoRing.Checked = true;
            }

            cboStart_SelectedIndexChanged(cboStart, new System.EventArgs());
            cboClock_SelectedIndexChanged(cboClock, new System.EventArgs());
            cboStop_SelectedIndexChanged(cboStop, new System.EventArgs());
        }

        //===================================================
        // スタート条件設定変更
        //===================================================
        private void cboStart_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStart.Text;
            if (szBuf.StartsWith("カウント一致"))
            {
                txtCompStart.Enabled = true;
            }
            else
            {
                txtCompStart.Enabled = false;
            }
        }

        //===================================================
        // クロック条件設定変更
        //===================================================
        private void cboClock_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            szBuf = cboClock.Text;
            if (string.Compare(szBuf, "内部クロック") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // ストップ条件設定変更
        //===================================================
        private void cboStop_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            szBuf = cboStop.Text;
            if (szBuf.StartsWith("カウント一致"))
            {
                txtCompStop.Enabled = true;
            }
            else
            {
                txtCompStop.Enabled = false;
            }
            if (string.Compare(szBuf, "指定個数転送完了") == 0)
            {
                txtStopNumber.Enabled = true;
            }
            else
            {
                txtStopNumber.Enabled = false;
            }
        }

        //===================================================
        // OKボタン処理
        //===================================================
        private void btnOK_Click(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            bool   Checked;
            short  dwStart;
            short  ExtSig1, ExtSig2, ExtSig3;
            short  wStartWith;
            short  dwClock;
            short  dwStop;
            short  wStopWith;
            int    ret;
            string strMsg;
            ushort CompChNo;
            ushort CompRegNo;
            string error_string = new string(' ', 256);

            //--------------------------------
            // 表示データを取得
            //--------------------------------
            szStart     = cboStart.Text;
            szStartWith = cboStartWith.Text;
            szClock     = cboClock.Text;
            szStop      = cboStop.Text;
            szStopWith  = cboStopWith.Text;

            szBuf = txtCompStart.Text;
            try
            {
                StartComp = uint.Parse(szBuf, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                StartComp = 0x100;
            }

            szBuf = txtInternalClock.Text;
            try
            {
                InternalClock = uint.Parse(szBuf);
            }
            catch (System.FormatException)
            {
                InternalClock = 1000;
            }

            szBuf = txtCompStop.Text;
            try
            {
                StopComp = uint.Parse(szBuf, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                StopComp = 0x200;
            }

            szBuf = txtStopNumber.Text;
            try
            {
                StopNum = uint.Parse(szBuf);
            }
            catch (System.FormatException)
            {
                StopNum = 1000;
            }

            szBuf = txtNumberData.Text;
            try
            {
                M_NotifyCount = uint.Parse(szBuf);
            }
            catch (System.FormatException)
            {
                M_NotifyCount = 500;
            }

            Checked = rdoOnce.Checked;

            //--------------------------------
            // 設定値を数値に変換
            //--------------------------------
            dwStart    = str_to_num(Start, szStart);
            ExtSig2    = str_to_extsig(Start, szStart);
            wStartWith = str_to_num(StartWith, szStartWith);

            dwClock    = str_to_num(Clock, szClock);
            ExtSig1    = str_to_extsig(Clock, szClock);

            dwStop     = str_to_num(Stop, szStop);
            ExtSig3    = str_to_extsig(Stop, szStop);
            wStopWith  = str_to_num(StopWith, szStopWith);

            if (Checked == true)
            {
                M_IsRing = (short)CcntConst.CNTS_WRITE_ONCE;
            }
            else
            {
                M_IsRing = (short)CcntConst.CNTS_WRITE_RING;
            }

            //--------------------------------
            // サンプリング条件を設定
            //--------------------------------
            ret = cnt.SetMaster(Form1.M_Id, ExtSig1, ExtSig2, ExtSig3, 1, 1);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetMaster ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
            }
            ret = cnt.SetSamplingStartTrigger(Form1.M_Id, dwStart, wStartWith, Form1.CHANNEL_NUM);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStartTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
            }
            ret = cnt.SetSamplingClockTrigger(Form1.M_Id, dwClock);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingClockTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
            }
            ret = cnt.SetSamplingStopTrigger(Form1.M_Id, dwStop, wStopWith, Form1.CHANNEL_NUM);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopTrigger ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
            }
            ret = cnt.SetSamplingInternalClock(Form1.M_Id, InternalClock, (short)CcntConst.CNTS_TIM_UNIT_NS);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingInternalClock ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
            }
            ret = cnt.SetSamplingStopNumber(Form1.M_Id, StopNum);
            if (ret != 0)
            {
                cnt.GetErrorString(ret, out error_string);
                strMsg = string.Format("CntSetSamplingStopNumber ret = {0:d}: {1}", ret, error_string);
                MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
            }

            switch (dwStart)
            {
                case (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0: CompChNo = 0;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0: CompChNo = 1;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0: CompChNo = 2;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0: CompChNo = 3;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG0: CompChNo = 4;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG0: CompChNo = 5;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG0: CompChNo = 6;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG0: CompChNo = 7;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1: CompChNo = 0;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1: CompChNo = 1;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1: CompChNo = 2;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1: CompChNo = 3;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG1: CompChNo = 4;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG1: CompChNo = 5;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG1: CompChNo = 6;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG1: CompChNo = 7;      CompRegNo = 1;      break;
                default:                                            CompChNo = 0xffff; CompRegNo = 0xffff; break;
            }
            if (CompChNo != 0xffff && CompRegNo != 0xffff)
            {

                ret = cnt.NotifyCountUp(Form1.M_Id, (short)CompChNo, (short)CompRegNo, StartComp, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                }
            }
            switch (dwStop)
            {
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0: CompChNo = 0;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0: CompChNo = 1;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0: CompChNo = 2;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0: CompChNo = 3;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG0: CompChNo = 4;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG0: CompChNo = 5;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG0: CompChNo = 6;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG0: CompChNo = 7;      CompRegNo = 0;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1: CompChNo = 0;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1: CompChNo = 1;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1: CompChNo = 2;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1: CompChNo = 3;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG1: CompChNo = 4;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG1: CompChNo = 5;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG1: CompChNo = 6;      CompRegNo = 1;      break;
                case (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG1: CompChNo = 7;      CompRegNo = 1;      break;
                default:                                           CompChNo = 0xffff; CompRegNo = 0xffff; break;
            }
            if (CompChNo != 0xffff && CompRegNo != 0xffff)
            {
                ret = cnt.NotifyCountUp(Form1.M_Id, (short)CompChNo, (short)CompRegNo, StopComp, (IntPtr)0);
                if (ret != 0)
                {
                    cnt.GetErrorString(ret, out error_string);
                    strMsg = string.Format("CntNotifyCountUp ret = {0:d}: {1}", ret, error_string);
                    MessageBox.Show(strMsg, Application.ProductName, MessageBoxButtons.OK);
                }
            }
            this.Close();
        }

        //===================================================
        // キャンセル ボタン処理
        //===================================================
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
