﻿using CaioCs;

namespace Counter
{
    public partial class Form1 : Form
    {
        //----------------------------------------
        // Create instance Caio class
        //----------------------------------------
        Caio aio = new Caio();

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public static short g_id;                   // ID for device access
        public static short g_channel;              // Counter channel
        public int g_event_count;                   // Comparison count match event occurrence count
        public static int g_compare_data_num = 1;   // Total number of comparison count data to be set
        public static short g_compare_repeat = 0;   // Repeat setting of comparison count data array
        public static int g_compare_data_0 = 100;   // Comparison count data 0
        public static int g_compare_data_1 = 200;   // Comparison count data 1
        public static int g_compare_data_2 = 300;   // Comparison count data 2
        public static int g_compare_data_3 = 400;   // Comparison count data 3
        public static int g_compare_data_4 = 500;   // Comparison count data 4
        public static int g_preset_data_num = 1;    // Total number of the preset data to be set
        public static short g_preset_repeat = 0;    // Repeat setting of preset data array
        public static int g_preset_data_0 = 0;      // Counter preset data 0
        public static int g_preset_data_1 = 100;    // Counter preset data 1
        public static int g_preset_data_2 = 200;    // Counter preset data 2
        public static int g_preset_data_3 = 300;    // Counter preset data 3
        public static int g_preset_data_4 = 400;    // Counter preset data 4

        public Form1()
        {
            InitializeComponent();
        }

        private void Counter_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Set the accept button
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // Update the initial display of counter channel
            //----------------------------------------
            textBox_ChannelNo.Text = "0";
            //----------------------------------------
            // Update the initial display of counter preset value
            //----------------------------------------
            textBox_PresetData.Text = "0";
            //----------------------------------------
            // Update the initial display of current count value
            //----------------------------------------
            textBox_CountData.Text = "0";
            //----------------------------------------
            // Update the initial display of status
            //----------------------------------------
            textBox_Status.Text = "";
            //----------------------------------------
            // Updated the initial display of comparison count match event occurrence count
            //----------------------------------------
            textBox_EventCount.Text = "0";
            //----------------------------------------
            // Initialize global variables
            //----------------------------------------
            g_id          = 0;
            g_channel     = 0;
            g_event_count = 0;
        }

        //================================================================================
        // It is the process when the AioInit button is clicked
        // Initialize the device
        //================================================================================
        private void button_AioInit_Click(object sender, EventArgs e)
        {
            string device_name;     // Device name
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Get device name from textbox
            //----------------------------------------
            device_name = textBox_DeviceName.Text;
            //----------------------------------------
            // Device initialization
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.Init(device_name, out g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Init = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioResetDevice button is clicked
        // Reset the device
        //================================================================================
        private void button_AioResetDevice_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Reset device
            //----------------------------------------
            ret = aio.ResetDevice(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the ChannelNo textbox is changed
        // Set the counter channel
        //================================================================================
        private void textBox_ChannelNo_TextChanged(object sender, EventArgs e)
        {
            //----------------------------------------
            // Get counter channel from textbox
            //----------------------------------------
            if (short.TryParse(textBox_ChannelNo.Text, out g_channel) == false)
            {
                g_channel = 0;
            }
        }

        //================================================================================
        // It is the process when the Set button is clicked
        // Open the counter setting dialog
        //================================================================================
        private void button_Setting_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Open the condition setting dialog
            //----------------------------------------
            Setting setting_dlg = new Setting();
            if (setting_dlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // Display the message that setting is completed
                //----------------------------------------
                textBox_ReturnCode.Text = "Set: Successful completion";
            }
            setting_dlg.Dispose();
        }

        //================================================================================
        // It is the process when the AioPresetCnt button is clicked
        // Preset the counter
        //================================================================================
        private void button_PresetCnt_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    preset_data;     // Counter preset value
            int    cnt_count;       // Count value

            //----------------------------------------
            // Get counter preset value from textbox
            //----------------------------------------
            if (int.TryParse(textBox_PresetData.Text, out preset_data) == false)
            {
                preset_data = 0;
            }
            //----------------------------------------
            // Preset the counter
            //----------------------------------------
            ret = aio.PresetCnt(g_id, g_channel, preset_data);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.PresetCnt = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // When the count value preset function is completed successfully
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // Get the count value
                //--------------------
                ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
                //--------------------
                // Error process
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "aio.GetCntCount = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //--------------------
                // Update count value
                //--------------------
                textBox_CountData.Text = cnt_count.ToString();
            }
        }

        //================================================================================
        // It is the process when the Count Start button is clicked
        // Start the counter action
        //================================================================================
        private void button_Start_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    cnt_status;      // Counter status
            int    ret;             // For return code
            int    cnt_count;       // Count value

            //----------------------------------------
            // Set the event factor to 'Comparison count match event', 'Counter operation error'
            //----------------------------------------
            ret = aio.SetCntEvent(g_id, g_channel, Handle, (int)(CaioConst.CNTE_DATA_NUM | CaioConst.CNTE_ERR));
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Start: aio.SetCntEvent = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Reset status
            //----------------------------------------
            ret = aio.ResetCntStatus(g_id, g_channel, (int)(CaioConst.CNTS_DATA_NUM | CaioConst.CNTS_ORERR));
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Start: aio.ResetCntStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Initialize comparison match count
            //----------------------------------------
            g_event_count = 0;
            //----------------------------------------
            // Start the counter action
            //----------------------------------------
            ret = aio.StartCnt(g_id, g_channel);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Start: aio.StartCnt = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Get the counter status
            //----------------------------------------
            ret = aio.GetCntStatus(g_id, g_channel, out cnt_status);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Start: aio.GetCntStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display message
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", cnt_status);
            //--------------------
            // When status 'Counter in operation' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Counter in operation' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Comparison count match' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
            {
                text_string += ", Comparison count match";
            }
            //--------------------
            // When status 'Overrun' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
            {
                text_string += ", Overrun";
            }
            //--------------------
            // When status 'Counter operation error' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
            {
                text_string += ", Counter operation error";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // Get count value
            //----------------------------------------
            ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Start: aio.GetCntCount = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display count value
            //----------------------------------------
            textBox_CountData.Text = cnt_count.ToString();
            //----------------------------------------
            // Update the comparison match count
            //----------------------------------------
            textBox_EventCount.Text = "0";
            //----------------------------------------
            // Display the message that count start is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Count Start: Successful completion";
        }

        //================================================================================
        // It is the process when the AioGetCntCount button is clicked
        // Get the count and display it on the dialog
        //================================================================================
        private void button_GetCntCount_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    cnt_count;       // Count value

            //----------------------------------------
            // Get the count value
            //----------------------------------------
            ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetCntCount = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // When the count value acquisition function is completed successfully
            //----------------------------------------
            if (ret == 0)
            {
                //--------------------
                // Update count value
                //--------------------
                textBox_CountData.Text = cnt_count.ToString();
            }
        }

        //================================================================================
        // It is the process when the AioGetCntStatus button is clicked
        // Get the counter status and display it on the dialog
        //================================================================================
        private void button_GetCntStatus_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ret;             // For return code
            int    cnt_status;      // Status

            //----------------------------------------
            // Get status
            //----------------------------------------
            ret = aio.GetCntStatus(g_id, g_channel, out cnt_status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetCntStatus = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // When the status acquisition function is completed successfully
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // Display message
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", cnt_status);
                //--------------------
                // When status 'Counter in operation' is ON
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
                {
                    text_string += "Operating";
                }
                //--------------------
                // When status 'Counter in operation' is OFF
                //--------------------
                else
                {
                    text_string += "Stop";
                }
                //--------------------
                // When status 'Comparison count match' is ON
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
                {
                    text_string += ", Comparison count match";
                }
                //--------------------
                // When status 'Overrun' is ON
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
                {
                    text_string += ", Overrun";
                }
                //--------------------
                // When status 'Counter operation error' is ON
                //--------------------
                if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
                {
                    text_string += ", Counter operation error";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
            }
        }

        //================================================================================
        // It is the process when the Counter Stop button is clicked
        // Stop the counter action
        //================================================================================
        private void button_Stop_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ret;             // For return code
            int    cnt_count;       // Count value
            int    cnt_status;      // Status

            //----------------------------------------
            // Stop the counter action
            //----------------------------------------
            ret = aio.StopCnt(g_id, g_channel);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Stop: aio.StopCnt = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Get the count value
            //----------------------------------------
            ret = aio.GetCntCount(g_id, g_channel, out cnt_count);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Stop: aio.GetCntCount = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display count value
            //----------------------------------------
            textBox_CountData.Text = cnt_count.ToString();
            //----------------------------------------
            // Get status
            //----------------------------------------
            ret = aio.GetCntStatus(g_id, g_channel, out cnt_status);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Count Stop: aio.GetCntStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display message
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", cnt_status);
            //--------------------
            // When status 'Counter in operation' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Counter in operation' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Comparison count match' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
            {
                text_string += ", Comparison count match";
            }
            //--------------------
            // When status 'Overrun' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
            {
                text_string += ", Overrun";
            }
            //--------------------
            // When status 'Counter operation error' is ON
            //--------------------
            if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
            {
                text_string += ", Counter operation error";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // Display the message that count stop is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Count Stop: Successful completion";
        }

        //================================================================================
        // It is the process when the AioExit button is clicked
        // Perform the exit process of device
        //================================================================================
        private void button_AioExit_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Exit process
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Perform the exit process of device and terminate the application
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Exit process
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // Operation when event occurred
        // Process for each event occurred
        //================================================================================
        protected override void WndProc(ref Message m)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ret;             // For return code
            int    cnt_count;       // Count value
            int    cnt_status;      // Status
            short  cnt_channel;     // Channel

            //----------------------------------------
            // Comparison count match event
            // Acquire and display data
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_CNTE_DATA_NUM)
            {
                //----------------------------------------
                // Get counter channel
                //----------------------------------------
                cnt_channel = (short)((int)m.WParam >> 16);
                if (cnt_channel == g_channel)
                {
                    //----------------------------------------
                    // Get count value
                    //----------------------------------------
                    ret = aio.GetCntCount(g_id, cnt_channel, out cnt_count);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    //--------------------
                    // Exit the process if not completed successfully
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_ReturnCode.Text = "Comparison Count Match Event Process: aio.GetCntCount = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display count value
                    //----------------------------------------
                    textBox_CountData.Text = cnt_count.ToString();
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetCntStatus(g_id, cnt_channel, out cnt_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    //--------------------
                    // Exit the process if not completed successfully
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_ReturnCode.Text = "Comparison Count Match Event Process: aio.GetCntStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display message
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", cnt_status);
                    //--------------------
                    // When status 'Counter in operation' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Counter in operation' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Comparison count match' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
                    {
                        text_string += ", Comparison count match";
                    }
                    //--------------------
                    // When status 'Overrun' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
                    {
                        text_string += ", Overrun";
                    }
                    //--------------------
                    // When status 'Counter operation error' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
                    {
                        text_string += ", Counter operation error";
                    }
                    text_string += ")";
                    textBox_Status.Text = text_string;
                    //----------------------------------------
                    // Display the comparison count match event occurrence count
                    //----------------------------------------
                    g_event_count++;
                    textBox_EventCount.Text = g_event_count.ToString();
                    //----------------------------------------
                    // Reset status
                    //----------------------------------------
                    ret = aio.ResetCntStatus(g_id, cnt_channel, (int)CaioConst.CNTS_DATA_NUM);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    //--------------------
                    // Exit the process if not completed successfully
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_ReturnCode.Text = "Comparison Count Match Event Process: aio.ResetCntStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_ReturnCode.Text = "Comparison Count Match Event Process: Successful completion";
                }
                return;
            }
            //----------------------------------------
            // Counter operation error
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_CNTE_ERR)
            {
                //----------------------------------------
                // Get counter channel
                //----------------------------------------
                cnt_channel = (short)((int)m.WParam >> 16);
                if (cnt_channel == g_channel)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetCntStatus(g_id, cnt_channel, out cnt_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    //--------------------
                    // Exit the process if not completed successfully
                    //--------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_ReturnCode.Text = "Counter Operation Error Occurrence Process: aio.GetCntStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display message
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", cnt_status);
                    //--------------------
                    // When status 'Counter in operation' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_BUSY) == (int)CaioConst.CNTS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Counter in operation' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Comparison count match' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_DATA_NUM) == (int)CaioConst.CNTS_DATA_NUM)
                    {
                        text_string += ", Comparison count match";
                    }
                    //--------------------
                    // When status 'Overrun' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ORERR) == (int)CaioConst.CNTS_ORERR)
                    {
                        text_string += ", Overrun";
                    }
                    //--------------------
                    // When status 'Counter operation error' is ON
                    //--------------------
                    if ((cnt_status & (int)CaioConst.CNTS_ERR) == (int)CaioConst.CNTS_ERR)
                    {
                        text_string += ", Counter operation error";
                    }
                    text_string += ")";
                    textBox_Status.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_ReturnCode.Text = "Counter Operation Error Occurrence Process: Successful completion";
                }
                return;
            }
            base.WndProc(ref m);
        }
    }
}