﻿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 header
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("Software start",                         (short)CcntConst.CNTS_START_SOFT                ,(short)CcntConst.CNTS_EXT_START_SOFT           ),
            new StrExtNum("External signal rising",                 (short)CcntConst.CNTS_START_EXT_RISE            ,(short)CcntConst.CNTS_EXT_START_EXT_RISE       ),
            new StrExtNum("External signal falling",                (short)CcntConst.CNTS_START_EXT_FALL            ,(short)CcntConst.CNTS_EXT_START_EXT_FALL       ),
            new StrExtNum("Count-match CH0 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG0    ),
            new StrExtNum("Count-match CH1 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG0    ),
            new StrExtNum("Count-match CH2 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG0    ),
            new StrExtNum("Count-match CH3 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG0    ),
            new StrExtNum("Count-match CH4 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG0    ),
            new StrExtNum("Count-match CH5 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG0    ),
            new StrExtNum("Count-match CH6 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG0    ),
            new StrExtNum("Count-match CH7 Compare-register 0",     (short)CcntConst.CNTS_START_CNTMATCH_CH7_REG0   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH7_REG0    ),
            new StrExtNum("Count-match CH0 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH0_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG1    ),
            new StrExtNum("Count-match CH1 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH1_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG1    ),
            new StrExtNum("Count-match CH2 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH2_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG1    ),
            new StrExtNum("Count-match CH3 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH3_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG1    ),
            new StrExtNum("Count-match CH4 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH4_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG1    ),
            new StrExtNum("Count-match CH5 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH5_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG1    ),
            new StrExtNum("Count-match CH6 Compare-register 1",     (short)CcntConst.CNTS_START_CNTMATCH_CH6_REG1   ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG1    ),
            new StrExtNum("Count-match CH7 Compare-register 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("Start counter regardless of sampling",   (short)CcntConst.CNTS_START_CNT_NOT_WITH_SAMP   ,0                                              ),
            new StrExtNum("Start counter when sampling starts",     (short)CcntConst.CNTS_START_CNT_WITH_SAMP       ,0                                              ),
            new StrExtNum("End",                                    0                                               ,0                                              )
        };
        // Sampling Clock
        static StrExtNum[] Clock =
        {
            new StrExtNum("Internal clock",                         (short)CcntConst.CNTS_CLOCK_CLOCK               ,(short)CcntConst.CNTS_EXT_CLOCK                ),
            new StrExtNum("External clock",                         (short)CcntConst.CNTS_CLOCK_EXT_TRG             ,(short)CcntConst.CNTS_EXT_EXT_TRG              ),
            new StrExtNum("End",                                    0                                               ,0                              )
        };
        // Sampling Stop
        static StrExtNum[] Stop =
        {
            new StrExtNum("Software stop",                          (short)CcntConst.CNTS_STOP_SOFT                 ,(short)CcntConst.CNTS_EXT_STOP_SOFT            ),
            new StrExtNum("External signal rising",                 (short)CcntConst.CNTS_STOP_EXT_RISE             ,(short)CcntConst.CNTS_EXT_STOP_EXT_RISE        ),
            new StrExtNum("External signal falling",                (short)CcntConst.CNTS_STOP_EXT_FALL             ,(short)CcntConst.CNTS_EXT_STOP_EXT_FALL        ),
            new StrExtNum("Stop transfer by specified times",       (short)CcntConst.CNTS_STOP_NUM                  ,(short)CcntConst.CNTS_EXT_TRNSNUM              ),
            new StrExtNum("Count-match CH0 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG0    ),
            new StrExtNum("Count-match CH1 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG0    ),
            new StrExtNum("Count-match CH2 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG0    ),
            new StrExtNum("Count-match CH3 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG0    ),
            new StrExtNum("Count-match CH4 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG0    ),
            new StrExtNum("Count-match CH5 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG0    ),
            new StrExtNum("Count-match CH6 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG0    ),
            new StrExtNum("Count-match CH7 Compare-register 0",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH7_REG0    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH7_REG0    ),
            new StrExtNum("Count-match CH0 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH0_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH0_REG1    ),
            new StrExtNum("Count-match CH1 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH1_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH1_REG1    ),
            new StrExtNum("Count-match CH2 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH2_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH2_REG1    ),
            new StrExtNum("Count-match CH3 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH3_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH3_REG1    ),
            new StrExtNum("Count-match CH4 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH4_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH4_REG1    ),
            new StrExtNum("Count-match CH5 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH5_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH5_REG1    ),
            new StrExtNum("Count-match CH6 Compare-register 1",     (short)CcntConst.CNTS_STOP_CNTMATCH_CH6_REG1    ,(short)CcntConst.CNTS_EXT_CNTMATCH_CH6_REG1    ),
            new StrExtNum("Count-match CH7 Compare-register 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("Stop counter regardless of sampling",    (short)CcntConst.CNTS_STOP_CNT_NOT_WITH_SAMP    ,0                                              ),
            new StrExtNum("Stop counter when sampling stops",       (short)CcntConst.CNTS_STOP_CNT_WITH_SAMP        ,0                                              ),
            new StrExtNum("End",                                    0                                               ,0                                              )
        };

        static string szStart = "Software start";
        static string szStartWith = "Start counter regardless of sampling";
        static string szClock = "Internal clock";
        static string szStop = "Software stop";
        static string szStopWith = "Stop counter regardless of sampling";
        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();
        }

        //===================================================
        // Dialog initialization
        //===================================================
        private void MSampMode_Load(object sender, System.EventArgs e)
        {
            short i;
            string szBuf = new string(' ', 100);

            //--------------------------------
            // Initialize display
            //--------------------------------
            // 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());
        }

        //===================================================
        // Start condition setting is changed
        //===================================================
        private void cboStart_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);

            szBuf = cboStart.Text;
            if (szBuf.StartsWith("Count-match"))
            {
                txtCompStart.Enabled = true;
            }
            else
            {
                txtCompStart.Enabled = false;
            }
        }

        //===================================================
        // Clock condition setting is changed
        //===================================================
        private void cboClock_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            szBuf = cboClock.Text;
            if (string.Compare(szBuf, "Internal clock") == 0)
            {
                txtInternalClock.Enabled = true;
            }
            else
            {
                txtInternalClock.Enabled = false;
            }
        }

        //===================================================
        // Stop condition setting is changed
        //===================================================
        private void cboStop_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string szBuf = new string(' ', 100);
            szBuf = cboStop.Text;
            if (szBuf.StartsWith("Count-match"))
            {
                txtCompStop.Enabled = true;
            }
            else
            {
                txtCompStop.Enabled = false;
            }
            if (string.Compare(szBuf, "Stop transfer by specified times") == 0)
            {
                txtStopNumber.Enabled = true;
            }
            else
            {
                txtStopNumber.Enabled = false;
            }
        }

        //===================================================
        // OK button processing
        //===================================================
        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);

            //--------------------------------
            // Get displayed data
            //--------------------------------
            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;

            //--------------------------------
            // Convert setting value to a number
            //--------------------------------
            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;
            }

            //--------------------------------
            // Set sampling condition
            //--------------------------------
            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();
        }

        //===================================================
        // Cancel button processing
        //===================================================
        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
