﻿using CaioCs;
using System.Diagnostics;
using System.Runtime.InteropServices;

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

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int MAX_AI_RANGE_NUM       = 32;      // Maximum number of elements in analog input range array
        public const int DISPLAY_SAMPLING_COUNT = 1000;    // Maximum sampling count of sampling data displayed in listbox

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short    g_master_id;                                                           // [Master] ID for device access
        public short    g_slave_id;                                                            // [Slave]  ID for device access
        public long     g_master_sampling_count;                                               // [Master] Total sampling count
        public long     g_slave_sampling_count;                                                // [Slave]  Total sampling count
        public float[]  g_master_sampling_data;                                                // [Master] Pointer to sampling data storage area
        public float[]  g_slave_sampling_data;                                                 // [Slave]  Pointer to sampling data storage area
        public GCHandle gCh_master_data;                                                       // GCHandle for g_master_sampling_data array
        public GCHandle gCh_slave_data;                                                        // GCHandle for g_slave_sampling_data array
        public short[]  g_ai_range = new short[MAX_AI_RANGE_NUM]{(short)CaioConst.PM10,        // +/-10V
                                                                 (short)CaioConst.PM5,         // +/-5V
                                                                 (short)CaioConst.PM25,        // +/-2.5V
                                                                 (short)CaioConst.PM125,       // +/-1.25V
                                                                 (short)CaioConst.PM1,         // +/-1V
                                                                 (short)CaioConst.PM0625,      // +/-0.625V
                                                                 (short)CaioConst.PM05,        // +/-0.5V
                                                                 (short)CaioConst.PM03125,     // +/-0.3125V
                                                                 (short)CaioConst.PM025,       // +/-0.25V
                                                                 (short)CaioConst.PM0125,      // +/-0.125V
                                                                 (short)CaioConst.PM01,        // +/-0.1V
                                                                 (short)CaioConst.PM005,       // +/-0.05V
                                                                 (short)CaioConst.PM0025,      // +/-0.025V
                                                                 (short)CaioConst.PM00125,     // +/-0.0125V
                                                                 (short)CaioConst.PM001,       // +/-0.01V
                                                                 (short)CaioConst.P10,         // 0 ~ 10V
                                                                 (short)CaioConst.P5,          // 0 ~ 5V
                                                                 (short)CaioConst.P4095,       // 0 ~ 4.095V
                                                                 (short)CaioConst.P25,         // 0 ~ 2.5V
                                                                 (short)CaioConst.P125,        // 0 ~ 1.25V
                                                                 (short)CaioConst.P1,          // 0 ~ 1V
                                                                 (short)CaioConst.P05,         // 0 ~ 0.5V
                                                                 (short)CaioConst.P025,        // 0 ~ 0.25V
                                                                 (short)CaioConst.P01,         // 0 ~ 0.1V
                                                                 (short)CaioConst.P005,        // 0 ~ 0.05V
                                                                 (short)CaioConst.P0025,       // 0 ~ 0.025V
                                                                 (short)CaioConst.P00125,      // 0 ~ 0.0125V
                                                                 (short)CaioConst.P001,        // 0 ~ 0.01V
                                                                 (short)CaioConst.P20MA,       // 0 ~ 20mA
                                                                 (short)CaioConst.P4TO20MA,    // 4 ~ 20mA
                                                                 (short)CaioConst.PM20MA,      // +/-20mA
                                                                 (short)CaioConst.P1TO5};      // 1 ~ 5V

        public Form1()
        {
            InitializeComponent();
            g_master_sampling_data = [];
            g_slave_sampling_data = [];
        }

        private void SyncBusSampling_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Set the accept button
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // [Master]  Update the initial display of device name
            //----------------------------------------
            textBox_MasterDeviceName.Text = "AIO000";
            //----------------------------------------
            // [Master]  Update the initial display of number of channels for analog input
            //----------------------------------------
            textBox_MasterAiChNum.Text = "1";
            //----------------------------------------
            // [Master]  Update the initial display of sampling clock
            //----------------------------------------
            textBox_MasterInternalClock.Text = "1000";
            //----------------------------------------
            // [Master]  Update the initial display of sampling stop times
            //----------------------------------------
            textBox_MasterStopTimes.Text = "1000";
            //----------------------------------------
            // [Master]  Create the combobox for analog input method
            //----------------------------------------
            comboBox_MasterAiInpMethod.Items.Add("Single-end input");
            comboBox_MasterAiInpMethod.Items.Add("Differential input");
            //----------------------------------------
            // [Master]  Change the selection of analog input method combobox to 0
            //----------------------------------------
            comboBox_MasterAiInpMethod.SelectedIndex = 0;
            //----------------------------------------
            // [Master]  Create the combobox for analog input range
            //----------------------------------------
            comboBox_MasterAiRangeValue.Items.Add("-10 - +10V");
            comboBox_MasterAiRangeValue.Items.Add("-5 - +5V");
            comboBox_MasterAiRangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_MasterAiRangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_MasterAiRangeValue.Items.Add("-1 - +1V");
            comboBox_MasterAiRangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_MasterAiRangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_MasterAiRangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_MasterAiRangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_MasterAiRangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_MasterAiRangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_MasterAiRangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_MasterAiRangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_MasterAiRangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_MasterAiRangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +10V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +5V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +4.095V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +2.5V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +1.25V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +1V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.5V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.25V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.1V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.05V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.025V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.0125V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +0.01V");
            comboBox_MasterAiRangeValue.Items.Add("0 - +20mA");
            comboBox_MasterAiRangeValue.Items.Add("4 - +20mA");
            comboBox_MasterAiRangeValue.Items.Add("-20mA - +20mA");
            comboBox_MasterAiRangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // [Master]  Change the selection of analog input range combobox to 0
            //----------------------------------------
            comboBox_MasterAiRangeValue.SelectedIndex = 0;
            //----------------------------------------
            // [Slave]    Update the initial display of device name
            //----------------------------------------
            textBox_SlaveDeviceName.Text = "AIO001";
            //----------------------------------------
            // [Slave]    Update the initial display of number of channels for analog input
            //----------------------------------------
            textBox_SlaveAiChNum.Text = "1";
            //----------------------------------------
            // [Slave]    Create the combobox for analog input method
            //----------------------------------------
            comboBox_SlaveAiInpMethod.Items.Add("Single-end input");
            comboBox_SlaveAiInpMethod.Items.Add("Differential input");
            //----------------------------------------
            // [Slave]    Change the selection of analog input method combobox to 0
            //----------------------------------------
            comboBox_SlaveAiInpMethod.SelectedIndex = 0;
            //----------------------------------------
            // [Slave]    Create the combobox for analog input range
            //----------------------------------------
            comboBox_SlaveAiRangeValue.Items.Add("-10 - +10V");
            comboBox_SlaveAiRangeValue.Items.Add("-5 - +5V");
            comboBox_SlaveAiRangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_SlaveAiRangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_SlaveAiRangeValue.Items.Add("-1 - +1V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_SlaveAiRangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +10V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +5V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +4.095V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +2.5V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +1.25V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +1V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.5V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.25V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.1V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.05V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.025V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.0125V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +0.01V");
            comboBox_SlaveAiRangeValue.Items.Add("0 - +20mA");
            comboBox_SlaveAiRangeValue.Items.Add("4 - +20mA");
            comboBox_SlaveAiRangeValue.Items.Add("-20mA - +20mA");
            comboBox_SlaveAiRangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // [Slave]    Change the selection of analog input range combobox to 0
            //----------------------------------------
            comboBox_SlaveAiRangeValue.SelectedIndex = 0;
            //----------------------------------------
            // Initialize global variables
            //----------------------------------------
            g_master_id             = 0;
            g_slave_id              = 0;
            g_master_sampling_count = 0;
            g_slave_sampling_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

            //----------------------------------------
            // [Master]  Get device name from textbox
            //----------------------------------------
            device_name = textBox_MasterDeviceName.Text;
            //----------------------------------------
            // [Master]  Device initialization
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.Init(device_name, out g_master_id);
            //----------------------------------------
            // [Master]  Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // [Master]  Display message
            //----------------------------------------
            textBox_MasterReturnCode.Text = "aio.Init = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // [Slave]    Get device name from textbox
            //----------------------------------------
            device_name = textBox_SlaveDeviceName.Text;
            //----------------------------------------
            // [Slave]    Device initialization
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.Init(device_name, out g_slave_id);
            //----------------------------------------
            // [Slave]    Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // [Slave]    Display message
            //----------------------------------------
            textBox_SlaveReturnCode.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

            //----------------------------------------
            // [Master]  Reset device
            //----------------------------------------
            ret = aio.ResetDevice(g_master_id);
            //----------------------------------------
            // [Master]  Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // [Master]  Display message
            //----------------------------------------
            textBox_MasterReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // [Slave]    Reset device
            //----------------------------------------
            ret = aio.ResetDevice(g_slave_id);
            //----------------------------------------
            // [Slave]    Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // [Slave]    Display message
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "aio.ResetDevice = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Analog Input Set button is clicked
        // Set the analog input settings
        //================================================================================
        private void button_AiSetting_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    index;           // Index of combobox
            short  input_method;    // Input method

            //----------------------------------------
            // Update GUI information
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // [Master]  Get the index of analog input range setting value from combobox
            //----------------------------------------
            index = comboBox_MasterAiRangeValue.SelectedIndex;
            //----------------------------------------
            // [Master]  Set the analog input range (all channels)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_master_id, g_ai_range[index]);
            //----------------------------------------
            // [Master]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Analog Input Set: aio.SetAiRangeAll = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master]  Get the index of input method setting value from combobox
            //----------------------------------------
            input_method = (short)comboBox_MasterAiInpMethod.SelectedIndex;
            //----------------------------------------
            // [Master]  Set the analog input method
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_master_id, input_method);
            //----------------------------------------
            // [Master]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Analog Input Set: aio.SetAiInputMethod = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master]  Display the message that analog input settings is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Analog Input Set: Successful completion";
            //----------------------------------------
            // [Slave]    Get the index of analog input range setting value from combobox
            //----------------------------------------
            index = comboBox_SlaveAiRangeValue.SelectedIndex;
            //----------------------------------------
            // [Slave]    Set the analog input range (all channels)
            //----------------------------------------
            ret = aio.SetAiRangeAll(g_slave_id, g_ai_range[index]);
            //----------------------------------------
            // [Slave]    Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Analog Input Set: aio.SetAiRangeAll = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]    Get the index of input method setting value from combobox
            //----------------------------------------
            input_method = (short)comboBox_SlaveAiInpMethod.SelectedIndex;
            //----------------------------------------
            // [Slave]    Set the analog input method
            //----------------------------------------
            ret = aio.SetAiInputMethod(g_slave_id, input_method);
            //----------------------------------------
            // [Slave]    Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Analog Input Set: aio.SetAiInputMethod = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]    Display the message that analog input settings is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Analog Input Set: Successful completion";
        }

        //================================================================================
        // It is the process when the Sampling Set button is clicked
        // Set the sampling settings
        //================================================================================
        private void button_SamplingSetting_Click(object sender, EventArgs e)
        {
            string error_string;      // Error code string
            int    ret;               // For return code
            short  ai_channels;       // Number of analog input channels
            float  sampling_clock;    // Internal clock conversion speed
            int    stop_times;        // Sampling stop times

            //----------------------------------------
            // [Slave]    Get the number of channels
            //----------------------------------------
            if (short.TryParse(textBox_SlaveAiChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // [Slave]    Set the number of analog input channels used for conversion
            //----------------------------------------
            ret = aio.SetAiChannels(g_slave_id, ai_channels);
            //----------------------------------------
            // [Slave]    Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiChannels = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Set the analog input conversion data transfer mode to device buffer mode
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiTransferMode(g_slave_id, 0);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiTransferMode = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Set the data storage memory type to FIFO
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiMemoryType(g_slave_id, 0);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiMemoryType = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Set the sampling start condition to Event controller output
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiStartTrigger(g_slave_id, 10);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiStartTrigger = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Set the clock type to Event controller output
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiClockType(g_slave_id, 10);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiClockType = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Set the sampling stop condition to Event controller output
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiStopTrigger(g_slave_id, 10);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiStopTrigger = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Set the event factor to 'Event that AD conversion start', 'Event that device operation end', 'Event that overflow', 
            // 'Event that sampling clock error', and 'Event that AD conversion error'.
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiEvent(g_slave_id, Handle, (int)(CaioConst.AIE_START | CaioConst.AIE_END | CaioConst.AIE_OFERR | CaioConst.AIE_SCERR | CaioConst.AIE_ADERR));
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Set: aio.SetAiEvent = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Display the message that sampling settings is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Sampling Set: Successful completion";
            //----------------------------------------
            // [Master] Get the number of channels
            //----------------------------------------
            if (short.TryParse(textBox_MasterAiChNum.Text, out ai_channels) == false)
            {
                ai_channels = 1;
            }
            //----------------------------------------
            // [Master] Set the number of analog input channels used for conversion
            //----------------------------------------
            ret = aio.SetAiChannels(g_master_id, ai_channels);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiChannels = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Set the analog input conversion data transfer mode to device buffer mode
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiTransferMode(g_master_id, 0);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiTransferMode = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Set the data storage memory type to FIFO
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiMemoryType(g_master_id, 0);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiMemoryType = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Set the sampling start condition to Software
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiStartTrigger(g_master_id, 0);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiStartTrigger = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Set the clock type to Internal Clock
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiClockType(g_master_id, 0);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiClockType = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Get the internal clock conversion speed from textbox
            //----------------------------------------
            if (float.TryParse(textBox_MasterInternalClock.Text, out sampling_clock) == false)
            {
                sampling_clock = 1000;
            }
            //----------------------------------------
            // [Master] Set the internal clock conversion speed
            //----------------------------------------
            ret = aio.SetAiSamplingClock(g_master_id, sampling_clock);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiSamplingClock = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Set the sampling stop condition to Stop conversion by the specified times
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiStopTrigger(g_master_id, 0);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiStopTrigger = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Get the sampling stop times from textbox
            //----------------------------------------
            if (int.TryParse(textBox_MasterStopTimes.Text, out stop_times) == false)
            {
                stop_times = 1000;
            }
            //----------------------------------------
            // [Master] Set the sampling stop times
            //----------------------------------------
            ret = aio.SetAiStopTimes(g_master_id, stop_times);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiStopTimes = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Set the event factor to 'Event that device operation end', 'Event that overflow', 
            // 'Event that sampling clock error', and 'Event that AD conversion error'.
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiEvent(g_master_id, Handle, (int)(CaioConst.AIE_END | CaioConst.AIE_OFERR | CaioConst.AIE_SCERR | CaioConst.AIE_ADERR));
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Set: aio.SetAiEvent = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Display the message that sampling settings is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Sampling Set: Successful completion";
        }

        //================================================================================
        // It is the process when the Sync Operation Set button is clicked
        // Set the synchronous operation settings
        //================================================================================
        private void button_SyncSetting_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // [Master] [Master]  Event Controller Setting: Connect Analog input internal clock signal to Synchronization bus master signal 1
            //----------------------------------------
            ret = aio.SetEcuSignal(g_master_id, (short)CaioConst.AIOECU_DEST_MASTER1, (short)CaioConst.AIOECU_SRC_AI_CLK);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sync Operation Set: aio.SetEcuSignal = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Event Controller Setting: Connect Ai software start signal to Synchronization bus master signal 2
            //----------------------------------------
            ret = aio.SetEcuSignal(g_master_id, (short)CaioConst.AIOECU_DEST_MASTER2, (short)CaioConst.AIOECU_SRC_START);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sync Operation Set: aio.SetEcuSignal = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Event Controller Setting: Connect Analog input signal that data of the specified sampling times have been converted (No delay) to Synchronization bus master signal 3
            //----------------------------------------
            ret = aio.SetEcuSignal(g_master_id, (short)CaioConst.AIOECU_DEST_MASTER3, (short)CaioConst.AIOECU_SRC_AI_STOP);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sync Operation Set: aio.SetEcuSignal = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Display the message that synchronous operation settings is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Sync Operation Set: Successful completion";
            //----------------------------------------
            // [Slave]  Event Controller Setting: Connect Synchronization bus slave signal 1 to Analog input sampling clock
            //----------------------------------------
            ret = aio.SetEcuSignal(g_slave_id, (short)CaioConst.AIOECU_DEST_AI_CLK, (short)CaioConst.AIOECU_SRC_SLAVE1);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sync Operation Set: aio.SetEcuSignal = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Event Controller Setting: Connect Synchronization bus slave signal 2 to Analog input converting start signal
            //----------------------------------------
            ret = aio.SetEcuSignal(g_slave_id, (short)CaioConst.AIOECU_DEST_AI_START, (short)CaioConst.AIOECU_SRC_SLAVE2);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sync Operation Set: aio.SetEcuSignal = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Event Controller Setting: Connect Synchronization bus slave signal 3 to Analog input converting stop signal
            //----------------------------------------
            ret = aio.SetEcuSignal(g_slave_id, (short)CaioConst.AIOECU_DEST_AI_STOP, (short)CaioConst.AIOECU_SRC_SLAVE3);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sync Operation Set: aio.SetEcuSignal = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Display the message that synchronous operation settings is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Sync Operation Set: Successful completion";
        }

        //================================================================================
        // It is the process when the Sampling Start button is clicked
        // After clearing the memory and status, start sampling.
        //================================================================================
        private void button_SampingStart_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ai_status;       // Analog input status
            int    ret;             // For return code

            //----------------------------------------
            // [Slave]  Clear memory
            //----------------------------------------
            ret = aio.ResetAiMemory(g_slave_id);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Start: aio.ResetAiMemory = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Clear status
            //----------------------------------------
            ret = aio.ResetAiStatus(g_slave_id);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Start: aio.ResetAiStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Initialize the total sampling count
            //----------------------------------------
            g_slave_sampling_count = 0;
            //----------------------------------------
            // [Slave]  Start sampling
            //----------------------------------------
            ret = aio.StartAi(g_slave_id);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Start: aio.StartAi = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Get status
            //----------------------------------------
            ret = aio.GetAiStatus(g_slave_id, out ai_status);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Sampling Start: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Display status
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", ai_status);
            //--------------------
            // When status 'Device is running' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Device is running' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Wait the start trigger' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
            {
                text_string += ", Wait the start trigger";
            }
            //--------------------
            // When status 'The specified number of data are stored' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
            {
                text_string += ", The specified number of data are stored";
            }
            //--------------------
            // When status 'Overflow' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
            {
                text_string += ", Overflow";
            }
            //--------------------
            // When status 'Sampling clock error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
            {
                text_string += ", Sampling clock error";
            }
            //--------------------
            // When status 'AD conversion error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
            {
                text_string += ", AD conversion error";
            }
            //--------------------
            // When status 'Driver spec error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
            {
                text_string += ", Driver spec error";
            }
            text_string += ")";
            textBox_SlaveAiStatus.Text = text_string;
            //----------------------------------------
            // [Slave]  Update the sampling count
            //----------------------------------------
            textBox_SlaveAiSamplingCount.Text = "0";
            //----------------------------------------
            // [Slave]  Clear the listbox for sampling data display
            //----------------------------------------
            listBox_SlaveAiSamplingData.Items.Clear();
            //----------------------------------------
            // [Slave]  Display the message that sampling start is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Sampling Start: Successful completion";
            //----------------------------------------
            // [Master] Clear memory
            //----------------------------------------
            ret = aio.ResetAiMemory(g_master_id);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Start: aio.ResetAiMemory = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Clear status
            //----------------------------------------
            ret = aio.ResetAiStatus(g_master_id);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Start: aio.ResetAiStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Initialize the total sampling count
            //----------------------------------------
            g_master_sampling_count = 0;
            //----------------------------------------
            // [Master] Start sampling
            //----------------------------------------
            ret = aio.StartAi(g_master_id);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Start: aio.StartAi = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Get status
            //----------------------------------------
            ret = aio.GetAiStatus(g_master_id, out ai_status);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Sampling Start: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Display status
            text_string = string.Format("{0:x8}H (", ai_status);
            //--------------------
            // When status 'Device is running' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Device is running' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Wait the start trigger' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
            {
                text_string += ", Wait the start trigger";
            }
            //--------------------
            // When status 'The specified number of data are stored' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
            {
                text_string += ", The specified number of data are stored";
            }
            //--------------------
            // When status 'Overflow' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
            {
                text_string += ", Overflow";
            }
            //--------------------
            // When status 'Sampling clock error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
            {
                text_string += ", Sampling clock error";
            }
            //--------------------
            // When status 'AD conversion error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
            {
                text_string += ", AD conversion error";
            }
            //--------------------
            // When status 'Driver spec error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
            {
                text_string += ", Driver spec error";
            }
            text_string += ")";
            textBox_MasterAiStatus.Text = text_string;
            //----------------------------------------
            // [Master] Update the sampling count
            //----------------------------------------
            textBox_MasterAiSamplingCount.Text = "0";
            //----------------------------------------
            // [Master] Clear the listbox for sampling data display
            //----------------------------------------
            listBox_MasterAiSamplingData.Items.Clear();
            //----------------------------------------
            // [Master] Display the message that sampling start is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Sampling Start: Successful completion";
        }

        //================================================================================
        // It is the process when the Get Status button is clicked
        // Get the count and status of sampling and display it on the dialog
        //================================================================================
        private void button_GetStatus_Click(object sender, EventArgs e)
        {
            string error_string;         // Error code string
            string text_string;          // For updating the textbox
            int    ai_sampling_count;    // Sampling count
            int    ai_status;            // Sampling status
            int    ret;                  // For return code

            //----------------------------------------
            // [Slave]  Get the sampling count
            //----------------------------------------
            ret = aio.GetAiSamplingCount(g_slave_id, out ai_sampling_count);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Get Status: aio.GetAiSamplingCount = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Update the sampling count
            //----------------------------------------
            textBox_SlaveAiSamplingCount.Text = ai_sampling_count.ToString();
            //----------------------------------------
            // [Slave]  Get status
            //----------------------------------------
            ret = aio.GetAiStatus(g_slave_id, out ai_status);
            //----------------------------------------
            // [Slave]  Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Get Status: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Display status
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", ai_status);
            //--------------------
            // When status 'Device is running' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Device is running' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Wait the start trigger' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
            {
                text_string += ", Wait the start trigger";
            }
            //--------------------
            // When status 'The specified number of data are stored' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
            {
                text_string += ", The specified number of data are stored";
            }
            //--------------------
            // When status 'Overflow' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
            {
                text_string += ", Overflow";
            }
            //--------------------
            // When status 'Sampling clock error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
            {
                text_string += ", Sampling clock error";
            }
            //--------------------
            // When status 'AD conversion error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
            {
                text_string += ", AD conversion error";
            }
            //--------------------
            // When status 'Driver spec error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
            {
                text_string += ", Driver spec error";
            }
            text_string += ")";
            textBox_SlaveAiStatus.Text = text_string;
            //----------------------------------------
            // [Slave]  Display the message that get status is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Get Status: Successful completion";
            //----------------------------------------
            // [Master] Get the sampling count
            //----------------------------------------
            ret = aio.GetAiSamplingCount(g_master_id, out ai_sampling_count);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Get Status: aio.GetAiSamplingCount = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Update the sampling count
            //----------------------------------------
            textBox_MasterAiSamplingCount.Text = ai_sampling_count.ToString();
            //----------------------------------------
            // [Master] Get status
            //----------------------------------------
            ret = aio.GetAiStatus(g_master_id, out ai_status);
            //----------------------------------------
            // [Master] Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Get Status: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Display status
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", ai_status);
            //--------------------
            // When status 'Device is running' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Device is running' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Wait the start trigger' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
            {
                text_string += ", Wait the start trigger";
            }
            //--------------------
            // When status 'The specified number of data are stored' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
            {
                text_string += ", The specified number of data are stored";
            }
            //--------------------
            // When status 'Overflow' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
            {
                text_string += ", Overflow";
            }
            //--------------------
            // When status 'Sampling clock error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
            {
                text_string += ", Sampling clock error";
            }
            //--------------------
            // When status 'AD conversion error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
            {
                text_string += ", AD conversion error";
            }
            //--------------------
            // When status 'Driver spec error' is ON
            //--------------------
            if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
            {
                text_string += ", Driver spec error";
            }
            text_string += ")";
            textBox_MasterAiStatus.Text = text_string;
            //----------------------------------------
            // [Master] Display the message that get status is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Get Status: Successful completion";
        }

        //================================================================================
        // It is the process when the Sampling Stop button is clicked
        // Stop sampling
        //================================================================================
        private void button_SamplingStop_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // [Master] Stop sampling
            //----------------------------------------
            ret = aio.StopAi(g_master_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Stop sampling: aio.StopAi = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Master] Display the message that sampling stop is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Stop sampling: Successful completion";
            //----------------------------------------
            // [Slave]  Stop sampling
            //----------------------------------------
            ret = aio.StopAi(g_slave_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Stop sampling: aio.StopAi = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // [Slave]  Display the message that sampling stop is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Stop sampling: Successful completion";
        }

        //================================================================================
        // It is the process when the Master Device Output Acquired Data to File button is clicked
        // Output the globally held sampling data to a CSV file
        //================================================================================
        private void button_MasterOutputSamplingData_Click(object sender, EventArgs e)
        {
            string       error_string;                            // Error code string
            string       file_name = "MasterSamplingData.csv";    // Sampling data output file name
            string       text_string;                             // For updating the textbox
            StreamWriter fp;                                      // File pointer
            long         i, j;                                    // Loop count
            int          ret;                                     // For return code
            short        ai_channels;                             // Number of channels

            //----------------------------------------
            // Open file
            //----------------------------------------
            try
            {
                fp = File.CreateText(file_name);
            }
            catch
            {
                textBox_MasterReturnCode.Text = "Output Acquired Data to File: File.CreateText = file open error.";
                return;
            }
            //----------------------------------------
            // Get the number of channels
            //----------------------------------------
            ret = aio.GetAiChannels(g_master_id, out ai_channels);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_MasterReturnCode.Text = "Output Acquired Data to File: aio.GetAiChannels = " + ret.ToString() + " : " + error_string;
                //----------------------------------------
                // Close file
                //----------------------------------------
                fp.Close();
                return;
            }
            //----------------------------------------
            // Create sampling data for file output
            //----------------------------------------
            //--------------------
            // If the total sampling count is 1 or more and pointer to sampling data storage area is not NULL, output the file
            //--------------------
            if ((g_master_sampling_count > 0) && (g_master_sampling_data != Array.Empty<float>()))
            {
                //--------------------
                // Repeat the process up to the total sampling count
                //--------------------
                for (i = 0; i < g_master_sampling_count; i++)
                {
                    text_string = "";
                    //--------------------
                    // Generate display string
                    //--------------------
                    text_string += string.Format("{0,5},", (i + 1));
                    //--------------------
                    // Repeat the process for the specified number of channels
                    //--------------------
                    for (j = 0; j < ai_channels; j++)
                    {
                        text_string += string.Format(" {0,10:F5}", g_master_sampling_data[i * ai_channels + j]);
                        //--------------------
                        // Add newline character for the last channel
                        //--------------------
                        if (j == (ai_channels - 1))
                        {
                            text_string += "\r\n";
                        }
                        //--------------------
                        // Add comma for channels other than the last channel
                        //--------------------
                        else
                        {
                            text_string += ",";
                        }
                    }
                    //----------------------------------------
                    // Write data
                    //----------------------------------------
                    try
                    {
                        fp.Write(text_string);
                    }
                    catch
                    {
                        textBox_MasterReturnCode.Text = "Output Acquired Data to File: fp.Write = file write error.";
                        fp.Close();
                        return;
                    }
                }
            }
            //----------------------------------------
            // Close file
            //----------------------------------------
            fp.Close();
            //----------------------------------------
            // Display the message that file output is completed
            //----------------------------------------
            textBox_MasterReturnCode.Text = "Output Acquired Data to File: Save data to \"" + file_name + "\".";
        }

        //================================================================================
        // It is the process when the Slave Device Output Acquired Data to File button is clicked
        // Output the globally held sampling data to a CSV file
        //================================================================================
        private void button_SlaveOutputSamplingData_Click(object sender, EventArgs e)
        {
            string       error_string;                           // Error code string
            string       file_name = "SlaveSamplingData.csv";    // Sampling data output file name
            string       text_string;                            // For updating the textbox
            StreamWriter fp;                                     // File pointer
            long         i, j;                                   // Loop count
            int          ret;                                    // For return code
            short        ai_channels;                            // Number of channels

            //----------------------------------------
            // Open file
            //----------------------------------------
            try
            {
                fp = File.CreateText(file_name);
            }
            catch
            {
                textBox_SlaveReturnCode.Text = "Output Acquired Data to File: File.CreateText = file open error.";
                return;
            }
            //----------------------------------------
            // Get the number of channels
            //----------------------------------------
            ret = aio.GetAiChannels(g_slave_id, out ai_channels);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_SlaveReturnCode.Text = "Output Acquired Data to File: aio.GetAiChannels = " + ret.ToString() + " : " + error_string;
                //----------------------------------------
                // Close file
                //----------------------------------------
                fp.Close();
                return;
            }
            //----------------------------------------
            // Create sampling data for file output
            //----------------------------------------
            //--------------------
            // If the total sampling count is 1 or more and pointer to sampling data storage area is not NULL, output the file
            //--------------------
            if ((g_slave_sampling_count > 0) && (g_slave_sampling_data != Array.Empty<float>()))
            {
                //--------------------
                // Repeat the process up to the total sampling count
                //--------------------
                for (i = 0; i < g_slave_sampling_count; i++)
                {
                    text_string = "";
                    //--------------------
                    // Generate display string
                    //--------------------
                    text_string += string.Format("{0,5},", (i + 1));
                    //--------------------
                    // Repeat the process for the specified number of channels
                    //--------------------
                    for (j = 0; j < ai_channels; j++)
                    {
                        text_string += string.Format(" {0,10:F5}", g_slave_sampling_data[i * ai_channels + j]);
                        //--------------------
                        // Add newline character for the last channel
                        //--------------------
                        if (j == (ai_channels - 1))
                        {
                            text_string += "\r\n";
                        }
                        //--------------------
                        // Add comma for channels other than the last channel
                        //--------------------
                        else
                        {
                            text_string += ",";
                        }
                    }
                    //----------------------------------------
                    // Write data
                    //----------------------------------------
                    try
                    {
                        fp.Write(text_string);
                    }
                    catch
                    {
                        textBox_SlaveReturnCode.Text = "Output Acquired Data to File: fp.Write = file write error.";
                        fp.Close();
                        return;
                    }
                }
            }
            //----------------------------------------
            // Close file
            //----------------------------------------
            fp.Close();
            //----------------------------------------
            // Display the message that file output is completed
            //----------------------------------------
            textBox_SlaveReturnCode.Text = "Output Acquired Data to File: Save data to \"" + file_name + "\".";
        }

        //================================================================================
        // 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

            //----------------------------------------
            // [Master] Release the array for storing AD conversion data
            //----------------------------------------
            if(g_master_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandle free
                //----------------------------------------
                if (gCh_master_data.IsAllocated == true)
                {
                    gCh_master_data.Free();
                }
                g_master_sampling_data = Array.Empty<float>();
            }
            //----------------------------------------
            // [Master] Exit process
            //----------------------------------------
            ret = aio.Exit(g_master_id);
            //----------------------------------------
            // [Master] Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // [Master] Display message
            //----------------------------------------
            textBox_MasterReturnCode.Text = "aio.Exit = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // [Slave] Release the array for storing AD conversion data
            //----------------------------------------
            if(g_slave_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandle free
                //----------------------------------------
                if (gCh_slave_data.IsAllocated == true)
                {
                    gCh_slave_data.Free();
                }
                g_slave_sampling_data = Array.Empty<float>();
            }
            //----------------------------------------
            // [Slave]  Exit process
            //----------------------------------------
            ret = aio.Exit(g_slave_id);
            //----------------------------------------
            // [Slave]  Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // [Slave]  Display message
            //----------------------------------------
            textBox_SlaveReturnCode.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)
        {
            //----------------------------------------
            // Release the array for storing AD conversion data
            //----------------------------------------
            if(g_master_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandle free
                //----------------------------------------
                if (gCh_master_data.IsAllocated == true)
                {
                    gCh_master_data.Free();
                }
                g_master_sampling_data = Array.Empty<float>();
            }
            if(g_slave_sampling_data != Array.Empty<float>()){
                //----------------------------------------
                // GCHandle free
                //----------------------------------------
                if (gCh_slave_data.IsAllocated == true)
                {
                    gCh_slave_data.Free();
                }
                g_slave_sampling_data = Array.Empty<float>();
            }
            //----------------------------------------
            // Exit process
            //----------------------------------------
            aio.Exit(g_slave_id);
            aio.Exit(g_master_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       ai_sampling_count;    // Sampling count
            int       ai_status;            // Sampling status
            int       ret;                  // For return code
            long      i, j;                 // Loop count
            long      loop_max_count;       // Maximum loop count
            long      top_address;          // Top address of buffer for sampling data display
            short     ai_channels;          // Number of channels
            short     id;                   // ID for device access

            //----------------------------------------
            // Get ID for device access
            //----------------------------------------
            id = (short)((long)m.WParam & 0x0000FFFF);
            //----------------------------------------
            // Event that AD conversion start
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_START)
            {
                //----------------------------------------
                // Slave ID
                //----------------------------------------
                if (id == g_slave_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Event that AD conversion start Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "Event that AD conversion start Process: Successful completion";
                }
            }
            //----------------------------------------
            // Event that device operation end
            // Acquire and display data
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_END)
            {
                //----------------------------------------
                // Master ID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "Device Operation End Event Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // Get the current sampling count
                    //----------------------------------------
                    ret = aio.GetAiSamplingCount(g_master_id, out ai_sampling_count);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "Device Operation End Event Process: aio.GetAiSamplingCount = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display sampling count
                    //----------------------------------------
                    textBox_MasterAiSamplingCount.Text = ai_sampling_count.ToString();
                    //----------------------------------------
                    // Get the number of channels
                    //----------------------------------------
                    ret = aio.GetAiChannels(g_master_id, out ai_channels);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "Device Operation End Event Process: aio.GetAiChannels = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Get sampling data process
                    //----------------------------------------
                    //--------------------
                    // Allocate an array for data storage
                    //--------------------
                    if(g_master_sampling_data != Array.Empty<float>()){
                        //----------------------------------------
                        // GCHandle free
                        //----------------------------------------
                        if (gCh_master_data.IsAllocated == true)
                        {
                            gCh_master_data.Free();
                        }
                        g_master_sampling_data = Array.Empty<float>();
                    }
                    try
                    {
                        g_master_sampling_data = new float[ai_sampling_count * ai_channels];
                    }
                    catch
                    {
                        textBox_MasterReturnCode.Text = "Device Operation End Event Process: Failed to allocate an array for storing data";
                        return;
                    }
                    //----------------------------------------
                    // Specify Pinned handle to g_master_sampling_data
                    //----------------------------------------
                    if (gCh_master_data.IsAllocated == false)
                    {
                        gCh_master_data = GCHandle.Alloc(g_master_sampling_data, GCHandleType.Pinned);
                    }
                    //----------------------------------------
                    // Get sampling data
                    //----------------------------------------
                    ret = aio.GetAiSamplingDataEx(g_master_id, ref ai_sampling_count, ref g_master_sampling_data);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "Device Operation End Event Process: aio.GetAiSamplingDataEx = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Update the total sampling count
                    //----------------------------------------
                    g_master_sampling_count = ai_sampling_count;
                    //----------------------------------------
                    // Set the loop count for the sampling data display loop
                    //----------------------------------------
                    //--------------------
                    // When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
                    //--------------------
                    if (ai_sampling_count > DISPLAY_SAMPLING_COUNT)
                    {
                        //--------------------
                        // Limit the maximum number of loops to the data display maximum sampling count
                        //--------------------
                        loop_max_count = DISPLAY_SAMPLING_COUNT;
                        //--------------------
                        // Set the top address of the buffer to be referenced to the address 
                        // before the maximum sampling count for data display from the end of the sampling data storage buffer
                        //--------------------
                        top_address = ai_sampling_count - DISPLAY_SAMPLING_COUNT;
                    }
                    //--------------------
                    // When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
                    //--------------------
                    else
                    {
                        //--------------------
                        // Set the number of total sampling data to the maximum loop number
                        //--------------------
                        loop_max_count = ai_sampling_count;
                        //--------------------
                        // Set the top address of the referenced buffer to 0
                        //--------------------
                        top_address = 0;
                    }
                    //----------------------------------------
                    // Display sampling data
                    // (Display from the end of acquired data to the maximum number of data display sampling count)
                    //----------------------------------------
                    //--------------------
                    // When the sampling count of the data acquired this time is 1 or more
                    //--------------------
                    if (ai_sampling_count > 0)
                    {
                        //--------------------
                        // Clear the listbox for sampling data display
                        //--------------------
                        listBox_MasterAiSamplingData.Items.Clear();
                        //--------------------
                        // Repeat the process up to the maximum sampling count of the display data
                        //--------------------
                        for (i = top_address; i < (loop_max_count + top_address); i++)
                        {
                            //--------------------
                            // Clear display string storage variable
                            //--------------------
                            text_string = "";
                            //--------------------
                            // Generate display string
                            //--------------------
                            text_string += string.Format("{0,5},  ", (i + 1));
                            //--------------------
                            // Repeat the process for the specified number of channels
                            //--------------------
                            for (j = 0; j < ai_channels; j++)
                            {
                                text_string += string.Format(" {0, 10:F5}", g_master_sampling_data[i * ai_channels + j]);
                            }
                            //----------------------------------------
                            // Display sampling start data
                            //----------------------------------------
                            listBox_MasterAiSamplingData.Items.Add(text_string);
                        }
                    }
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "Device Operation End Event Process: Successful completion";
                }
                //----------------------------------------
                // Slave ID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Device Operation End Event Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // Get the current sampling count
                    //----------------------------------------
                    ret = aio.GetAiSamplingCount(g_slave_id, out ai_sampling_count);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Device Operation End Event Process: aio.GetAiSamplingCount = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display sampling count
                    //----------------------------------------
                    textBox_SlaveAiSamplingCount.Text = ai_sampling_count.ToString();
                    //----------------------------------------
                    // Get the number of channels
                    //----------------------------------------
                    ret = aio.GetAiChannels(g_slave_id, out ai_channels);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Device Operation End Event Process: aio.GetAiChannels = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Get sampling data process
                    //----------------------------------------
                    //--------------------
                    // Allocate an array for data storage
                    //--------------------
                    if(g_slave_sampling_data != Array.Empty<float>()){
                        //----------------------------------------
                        // GCHandle free
                        //----------------------------------------
                        if (gCh_slave_data.IsAllocated == true)
                        {
                            gCh_slave_data.Free();
                        }
                        g_slave_sampling_data = Array.Empty<float>();
                    }
                    try
                    {
                        g_slave_sampling_data = new float[ai_sampling_count * ai_channels];
                    }
                    catch
                    {
                        textBox_SlaveReturnCode.Text = "Device Operation End Event Process: Failed to allocate an array for storing data";
                        return;
                    }
                    //----------------------------------------
                    // Specify Pinned handle to g_slave_sampling_data
                    //----------------------------------------
                    if (gCh_slave_data.IsAllocated == false)
                    {
                        gCh_slave_data = GCHandle.Alloc(g_slave_sampling_data, GCHandleType.Pinned);
                    }
                    //----------------------------------------
                    // Get sampling data
                    //----------------------------------------
                    ret = aio.GetAiSamplingDataEx(g_slave_id, ref ai_sampling_count, ref g_slave_sampling_data);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Device Operation End Event Process: aio.GetAiSamplingDataEx = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Update the total sampling count
                    //----------------------------------------
                    g_slave_sampling_count = ai_sampling_count;
                    //----------------------------------------
                    // Set the loop count for the sampling data display loop
                    //----------------------------------------
                    //--------------------
                    // When the sampling count of the data acquired this time exceeds the maximum sampling number of data display
                    //--------------------
                    if (ai_sampling_count > DISPLAY_SAMPLING_COUNT)
                    {
                        //--------------------
                        // Limit the maximum number of loops to the data display maximum sampling count
                        //--------------------
                        loop_max_count = DISPLAY_SAMPLING_COUNT;
                        //--------------------
                        // Set the top address of the buffer to be referenced to the address 
                        // before the maximum sampling count for data display from the end of the sampling data storage buffer
                        //--------------------
                        top_address = ai_sampling_count - DISPLAY_SAMPLING_COUNT;
                    }
                    //--------------------
                    // When the sampling count of the data acquired this time is less than or equal to the maximum sampling number of data display
                    //--------------------
                    else
                    {
                        //--------------------
                        // Set the number of total sampling data to the maximum loop number
                        //--------------------
                        loop_max_count = ai_sampling_count;
                        //--------------------
                        // Set the top address of the referenced buffer to 0
                        //--------------------
                        top_address = 0;
                    }
                    //----------------------------------------
                    // Display sampling data
                    // (Display from the end of acquired data to the maximum number of data display sampling count)
                    //----------------------------------------
                    //--------------------
                    // When the sampling count of the data acquired this time is 1 or more
                    //--------------------
                    if (ai_sampling_count > 0)
                    {
                        //--------------------
                        // Clear the listbox for sampling data display
                        //--------------------
                        listBox_SlaveAiSamplingData.Items.Clear();
                        //--------------------
                        // Repeat the process up to the maximum sampling count of the display data
                        //--------------------
                        for (i = top_address; i < (loop_max_count + top_address); i++)
                        {
                            //--------------------
                            // Clear display string storage variable
                            //--------------------
                            text_string = "";
                            //--------------------
                            // Generate display string
                            //--------------------
                            text_string += string.Format("{0,5},  ", (i + 1));
                            //--------------------
                            // Repeat the process for the specified number of channels
                            //--------------------
                            for (j = 0; j < ai_channels; j++)
                            {
                                text_string += string.Format(" {0, 10:F5}", g_slave_sampling_data[i * ai_channels + j]);
                            }
                            //----------------------------------------
                            // Display sampling start data
                            //----------------------------------------
                            listBox_SlaveAiSamplingData.Items.Add(text_string);
                        }
                    }
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "Device Operation End Event Process: Successful completion";
                }
            }
            //----------------------------------------
            // Event that overflow
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_OFERR)
            {
                //----------------------------------------
                // Master ID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "Overflow Occurrence Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "Overflow Occurrence Process: Successful completion";
                }
                //----------------------------------------
                // Slave ID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Overflow Occurrence Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "Overflow Occurrence Process: Successful completion";
                }
            }
            //----------------------------------------
            // Event that sampling clock error
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_SCERR)
            {
                //----------------------------------------
                // Master ID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "Sampling Clock Error Occurrence Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion";
                }
                //----------------------------------------
                // Slave ID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "Sampling Clock Error Occurrence Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion";
                }
            }
            //----------------------------------------
            // Event that AD conversion error
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AIE_ADERR)
            {
                //----------------------------------------
                // Master ID
                //----------------------------------------
                if (id == g_master_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_master_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_MasterReturnCode.Text = "AD Conversion Error Occurrence Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_MasterAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_MasterReturnCode.Text = "AD Conversion Error Occurrence Process: Successful completion";
                }
                //----------------------------------------
                // Slave ID
                //----------------------------------------
                else if (id == g_slave_id)
                {
                    //----------------------------------------
                    // Get status
                    //----------------------------------------
                    ret = aio.GetAiStatus(g_slave_id, out ai_status);
                    //----------------------------------------
                    // Error process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        textBox_SlaveReturnCode.Text = "AD Conversion Error Occurrence Process: aio.GetAiStatus = " + ret.ToString() + " : " + error_string;
                        return;
                    }
                    //----------------------------------------
                    // Display status
                    //----------------------------------------
                    text_string = string.Format("{0:x8}H (", ai_status);
                    //--------------------
                    // When status 'Device is running' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_BUSY) == (int)CaioConst.AIS_BUSY)
                    {
                        text_string += "Operating";
                    }
                    //--------------------
                    // When status 'Device is running' is OFF
                    //--------------------
                    else
                    {
                        text_string += "Stop";
                    }
                    //--------------------
                    // When status 'Wait the start trigger' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_START_TRG) == (int)CaioConst.AIS_START_TRG)
                    {
                        text_string += ", Wait the start trigger";
                    }
                    //--------------------
                    // When status 'The specified number of data are stored' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DATA_NUM) == (int)CaioConst.AIS_DATA_NUM)
                    {
                        text_string += ", The specified number of data are stored";
                    }
                    //--------------------
                    // When status 'Overflow' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_OFERR) == (int)CaioConst.AIS_OFERR)
                    {
                        text_string += ", Overflow";
                    }
                    //--------------------
                    // When status 'Sampling clock error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_SCERR) == (int)CaioConst.AIS_SCERR)
                    {
                        text_string += ", Sampling clock error";
                    }
                    //--------------------
                    // When status 'AD conversion error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_AIERR) == (int)CaioConst.AIS_AIERR)
                    {
                        text_string += ", AD conversion error";
                    }
                    //--------------------
                    // When status 'Driver spec error' is ON
                    //--------------------
                    if ((ai_status & (int)CaioConst.AIS_DRVERR) == (int)CaioConst.AIS_DRVERR)
                    {
                        text_string += ", Driver spec error";
                    }
                    text_string += ")";
                    textBox_SlaveAiStatus.Text = text_string;
                    //----------------------------------------
                    // Display the message that event processing is completed
                    //----------------------------------------
                    textBox_SlaveReturnCode.Text = "AD Conversion Error Occurrence Process: Successful completion";
                }
            }
            base.WndProc(ref m);
        }
    }
}