﻿using CaioCs;

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

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public short[] g_start_trigger = {0,     // Software
                                          1,     // External trigger rising edge
                                          2,     // External trigger falling edge
                                          3,     // Level comparison
                                          4,     // In Range comparison
                                          5};    // Out Range comparison
        public short[] g_clock_type    = {0,     // Internal clock
                                          1};    // External clock
        public short[] g_clock_edge    = {0,     // External input falling edge
                                          1};    // External input rising edge
        public short[] g_stop_trigger  = {1,     // External trigger rising edge
                                          2,     // External trigger falling edge
                                          3,     // Level comparison
                                          4,     // Command (AioStopAi)
                                          5,     // In Range comparison
                                          6};    // Out Range comparison

        public Condition()
        {
            InitializeComponent();
        }

        private void Condition_Load(object sender, EventArgs e)
        {
            string error_string;           // Error code string
            string message_string = "";    // For displaying message
            int    ret;                    // For return code
            short  index;                  // Index of combobox
            short  ai_start_trigger;       // Conversion start condition
            short  ai_stop_trigger;        // Conversion stop condition
            float  ai_level;               // Level comparison data
            short  ai_direction;           // Level comparison direction
            float  ai_range_level1;        // Upper limit data of In Range / Out Range comparison
            float  ai_range_level2;        // Lower limit data of In Range / Out Range comparison
            int    ai_state_times;         // Sampling times of the state-holding
            short  ai_clock_type;          // Clock type
            float  ai_sampling_clock;      // Sampling clock
            short  ai_clock_edge;          // Timing of the external clock input
            short  ai_auto_mode;           // Set mode of transfer size
            int    ai_transfer_size;       // Transfer size
            float  ai_scan_clock;          // Conversion speed
            short  err_flag;               // Function execution error flag

            //----------------------------------------
            // Initialize parameters
            //----------------------------------------
            err_flag = 0;
            //----------------------------------------
            // Create the combobox for analog input conversion start condition
            //----------------------------------------
            comboBox_StartCondition.Items.Add("Software");
            comboBox_StartCondition.Items.Add("External trigger rising edge");
            comboBox_StartCondition.Items.Add("External trigger falling edge");
            comboBox_StartCondition.Items.Add("Level comparison");
            comboBox_StartCondition.Items.Add("In Range comparison");
            comboBox_StartCondition.Items.Add("Out Range comparison");
            //----------------------------------------
            // Get the conversion start condition
            //----------------------------------------
            ret = aio.GetAiStartTrigger(Form1.g_id, out ai_start_trigger);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    message_string = string.Format("aio.GetAiStartTrigger = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    comboBox_StartCondition.Enabled = false;
                }
                //--------------------
                // Change the selection of analog input conversion start condition combobox to 0
                //--------------------
                comboBox_StartCondition.SelectedIndex = 0;
                comboBox_StartCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Convert from conversion start condition to index
                //--------------------
                index = 0;
                switch (ai_start_trigger)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    case 2:
                        index = 2;
                        break;
                    case 3:
                        index = 3;
                        break;
                    case 4:
                        index = 4;
                        break;
                    case 5:
                        index = 5;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // Change the selection of the analog input conversion start condition combobox
                //--------------------
                comboBox_StartCondition.SelectedIndex = index;
                comboBox_StartCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Update the initial display of channel No.
            //----------------------------------------
            textBox_StartLevelChNo.Text = "0";
            //----------------------------------------
            // Create the combobox for level comparison direction
            //----------------------------------------
            comboBox_StartLevelComprionDriection.Items.Add("Both edges");
            comboBox_StartLevelComprionDriection.Items.Add("Rising edge");
            comboBox_StartLevelComprionDriection.Items.Add("Falling edge");
            //----------------------------------------
            // For Level comparison
            //----------------------------------------
            if (ai_start_trigger == 3)
            {
                //----------------------------------------
                // Get the level when the level comparison is started
                //----------------------------------------
                ret = aio.GetAiStartLevelEx(Form1.g_id, 0, out ai_level, out ai_direction);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiStartLevelEx = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        textBox_StartLevelChNo.Enabled = false;
                        textBox_StartLevelComprionValue.Enabled = false;
                        comboBox_StartLevelComprionDriection.Enabled = false;
                    }
                    //--------------------
                    // Update the initial display of comparison value
                    //--------------------
                    textBox_StartLevelComprionValue.Text = "1.0";
                    //--------------------
                    // Change the selection of the level comparison direction combobox to 0
                    //--------------------
                    comboBox_StartLevelComprionDriection.SelectedIndex = 0;
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Update comparison value
                    //--------------------
                    textBox_StartLevelComprionValue.Text = ai_level.ToString("F1");
                    //--------------------
                    // Convert from level comparison direction to index
                    //--------------------
                    index = 0;
                    switch (ai_direction)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        case 2:
                            index = 2;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //--------------------
                    // Change the selection of the level comparison direction combobox
                    //--------------------
                    comboBox_StartLevelComprionDriection.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // Other than Level comparison
            //----------------------------------------
            else
            {
                //--------------------
                // Update the initial display of comparison value
                //--------------------
                textBox_StartLevelComprionValue.Text = "1.0";
                //--------------------
                // Change the selection of the level comparison direction combobox to 0
                //--------------------
                comboBox_StartLevelComprionDriection.SelectedIndex = 0;
            }
            //----------------------------------------
            // Update the initial display of channel No.
            //----------------------------------------
            textBox_StartRangeChNo.Text = "0";
            //----------------------------------------
            // For In Range comparison
            //----------------------------------------
            if (ai_start_trigger == 4)
            {
                //----------------------------------------
                // Get the settings of In Range comparison start condition
                //----------------------------------------
                ret = aio.GetAiStartInRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiStartInRangeEx = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        textBox_StartRangeChNo.Enabled = false;
                        textBox_StartRangeUpperLimitValue.Enabled = false;
                        textBox_StartRangeLowerLimitValue.Enabled = false;
                        textBox_StartRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // Update the initial display of upper limit
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // Update the initial display of lower limit
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // Update the initial display of state-holding times
                    //--------------------
                    textBox_StartRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Update upper limit
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // Update lower limit
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // Update state-holding times
                    //--------------------
                    textBox_StartRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // For Out Range comparison
            //----------------------------------------
            else if (ai_start_trigger == 5)
            {
                //----------------------------------------
                // Get the settings of Out Range comparison start condition
                //----------------------------------------
                ret = aio.GetAiStartOutRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiStartOutRangeEx = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        textBox_StartRangeChNo.Enabled = false;
                        textBox_StartRangeUpperLimitValue.Enabled = false;
                        textBox_StartRangeLowerLimitValue.Enabled = false;
                        textBox_StartRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // Update the initial display of upper limit
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // Update the initial display of lower limit
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // Update the initial display of state-holding times
                    //--------------------
                    textBox_StartRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Update upper limit
                    //--------------------
                    textBox_StartRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // Update lower limit
                    //--------------------
                    textBox_StartRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // Update state-holding times
                    //--------------------
                    textBox_StartRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // Other than In Range comparison / Out Range comparison
            //----------------------------------------
            else
            {
                //--------------------
                // Update the initial display of upper limit
                //--------------------
                textBox_StartRangeUpperLimitValue.Text = "10.0";
                //--------------------
                // Update the initial display of lower limit
                //--------------------
                textBox_StartRangeLowerLimitValue.Text = "1.0";
                //--------------------
                // Update the initial display of state-holding times
                //--------------------
                textBox_StartRangeStateTimes.Text = "1";
            }
            //----------------------------------------
            // Create the combobox for analog input clock type
            //----------------------------------------
            comboBox_ClockType.Items.Add("Internal clock");
            comboBox_ClockType.Items.Add("External clock");
            //----------------------------------------
            // Get clock type
            //----------------------------------------
            ret = aio.GetAiClockType(Form1.g_id, out ai_clock_type);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    message_string = string.Format("aio.GetAiClockType = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    comboBox_ClockType.Enabled = false;
                }
                //--------------------
                // Change the selection of the analog input clock type combobox to 0
                //--------------------
                comboBox_ClockType.SelectedIndex = 0;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Convert from clock type to index
                //--------------------
                index = 0;
                switch (ai_clock_type)
                {
                    case 0:
                        index = 0;
                        break;
                    case 1:
                        index = 1;
                        break;
                    default:
                        index = 0;
                        break;
                }
                //--------------------
                // Change the selection of the analog input clock type combobox
                //--------------------
                comboBox_ClockType.SelectedIndex = index;
                comboBox_ClockType_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Get sampling clock
            //----------------------------------------
            ret = aio.GetAiSamplingClock(Form1.g_id, out ai_sampling_clock);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    message_string = string.Format("aio.GetAiSamplingClock = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    textBox_SamplingClock.Enabled = false;
                }
                //--------------------
                // Update the initial display of sampling clock
                //--------------------
                textBox_SamplingClock.Text = "1000";
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Update sampling clock
                //--------------------
                textBox_SamplingClock.Text = Convert.ToDouble(ai_sampling_clock).ToString("F6");
            }
            //----------------------------------------
            // Create the combobox for analog input external clock input timing
            //----------------------------------------
            comboBox_ExternalClockInputTiming.Items.Add("External input falling edge");
            comboBox_ExternalClockInputTiming.Items.Add("External input rising edge");
            //----------------------------------------
            // For External clock
            //----------------------------------------
            if (ai_clock_type == 1)
            {
                //----------------------------------------
                // Get the timing of external clock input
                //----------------------------------------
                ret = aio.GetAiClockEdge(Form1.g_id, out ai_clock_edge);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiClockEdge = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        comboBox_ExternalClockInputTiming.Enabled = false;
                    }
                    //----------------------------------------
                    // Change the selection of the analog input external clock input timing combobox to 0
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = 0;
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Convert from input timing to index
                    //--------------------
                    index = 0;
                    switch (ai_clock_edge)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //----------------------------------------
                    // Change the selection of the analog input external clock input timing combobox
                    //----------------------------------------
                    comboBox_ExternalClockInputTiming.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // Other than External clock
            //----------------------------------------
            else
            {
                //----------------------------------------
                // Change the selection of the analog input external clock input timing combobox to 0
                //----------------------------------------
                comboBox_ExternalClockInputTiming.SelectedIndex = 0;
            }
            //----------------------------------------
            // Get transfer size
            //----------------------------------------
            ret = aio.GetAiTransferSize(Form1.g_id, out ai_auto_mode, out ai_transfer_size);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    message_string = string.Format("aio.GetAiTransferSize = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    textBox_TransferSize.Enabled = false;
                }
                //--------------------
                // Update the initial display of transfer size
                //--------------------
                textBox_TransferSize.Text = "0";
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Update transfer size
                //--------------------
                textBox_TransferSize.Text = ai_transfer_size.ToString();
            }
            //----------------------------------------
            // Get conversion speed
            //----------------------------------------
            ret = aio.GetAiScanClock(Form1.g_id, out ai_scan_clock);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    message_string = string.Format("aio.GetAiScanClock = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    textBox_ScanClock.Enabled = false;
                }
                //--------------------
                // Update the initial display of conversion speed
                //--------------------
                textBox_ScanClock.Text = "10";
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Update conversion speed
                //--------------------
                textBox_ScanClock.Text = ai_scan_clock.ToString();
            }
            //----------------------------------------
            // Create the combobox for analog input conversion stop condition
            //----------------------------------------
            comboBox_StopCondition.Items.Add("External trigger rising edge");
            comboBox_StopCondition.Items.Add("External trigger falling edge");
            comboBox_StopCondition.Items.Add("Level comparison");
            comboBox_StopCondition.Items.Add("Command (AioStopAi)");
            comboBox_StopCondition.Items.Add("In Range comparison");
            comboBox_StopCondition.Items.Add("Out Range comparison");
            //----------------------------------------
            // Get the conversion stop condition
            //----------------------------------------
            ret = aio.GetAiStopTrigger(Form1.g_id, out ai_stop_trigger);
            //----------------------------------------
            // Abnormal termination process
            //----------------------------------------
            if (ret != 0)
            {
                if (ret != 20001)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    message_string = string.Format("aio.GetAiStopTrigger = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                    err_flag = 1;
                }
                else
                {
                    //--------------------
                    // Disable control
                    //--------------------
                    comboBox_StopCondition.Enabled = false;
                }
                //--------------------
                // Change the selection in the analog input conversion stop condition combobox to 3
                //--------------------
                comboBox_StopCondition.SelectedIndex = 3;
                comboBox_StopCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Normal termination process
            //----------------------------------------
            else
            {
                //--------------------
                // Convert from conversion stop condition to index
                //--------------------
                index = 0;
                switch (ai_stop_trigger)
                {
                    case 0:
                        index = 3;
                        break;
                    case 1:
                        index = 0;
                        break;
                    case 2:
                        index = 1;
                        break;
                    case 3:
                        index = 2;
                        break;
                    case 4:
                        index = 3;
                        break;
                    case 5:
                        index = 4;
                        break;
                    case 6:
                        index = 5;
                        break;
                    default:
                        index = 3;
                        break;
                }
                //--------------------
                // Change the selection in the analog input conversion stop condition combobox
                //--------------------
                comboBox_StopCondition.SelectedIndex = index;
                comboBox_StopCondition_SelectedIndexChanged(new object(), new EventArgs());
            }
            //----------------------------------------
            // Update the initial display of channel No.
            //----------------------------------------
            textBox_StopLevelChNo.Text = "0";
            //----------------------------------------
            // Create the combobox for level comparison direction
            //----------------------------------------
            comboBox_StopLevelComprionDriection.Items.Add("Both edges");
            comboBox_StopLevelComprionDriection.Items.Add("Rising edge");
            comboBox_StopLevelComprionDriection.Items.Add("Falling edge");
            //----------------------------------------
            // For Level comparison
            //----------------------------------------
            if (ai_stop_trigger == 3)
            {
                //----------------------------------------
                // Get the level when the level comparison is stopped
                //----------------------------------------
                ret = aio.GetAiStopLevelEx(Form1.g_id, 0, out ai_level, out ai_direction);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiStopLevelEx = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        textBox_StopLevelChNo.Enabled = false;
                        textBox_StopLevelComprionValue.Enabled = false;
                        comboBox_StopLevelComprionDriection.Enabled = false;
                    }
                    //--------------------
                    // Update the initial display of comparison value
                    //--------------------
                    textBox_StopLevelComprionValue.Text = "1.0";
                    //--------------------
                    // Change the selection of the level comparison direction combobox to 0
                    //--------------------
                    comboBox_StopLevelComprionDriection.SelectedIndex = 0;
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Update comparison value
                    //--------------------
                    textBox_StopLevelComprionValue.Text = ai_level.ToString("F1");
                    //--------------------
                    // Convert from level comparison direction to index
                    //--------------------
                    index = 0;
                    switch (ai_direction)
                    {
                        case 0:
                            index = 0;
                            break;
                        case 1:
                            index = 1;
                            break;
                        case 2:
                            index = 2;
                            break;
                        default:
                            index = 0;
                            break;
                    }
                    //--------------------
                    // Change the selection of the level comparison direction combobox
                    //--------------------
                    comboBox_StopLevelComprionDriection.SelectedIndex = index;
                }
            }
            //----------------------------------------
            // Other than Level comparison
            //----------------------------------------
            else
            {
                //--------------------
                // Update the initial display of comparison value
                //--------------------
                textBox_StopLevelComprionValue.Text = "1.0";
                //--------------------
                // Change the selection of the level comparison direction combobox to 0
                //--------------------
                comboBox_StopLevelComprionDriection.SelectedIndex = 0;
            }
            //----------------------------------------
            // Update the initial display of channel No.
            //----------------------------------------
            textBox_StopRangeChNo.Text = "0";
            //----------------------------------------
            // For In Range comparison
            //----------------------------------------
            if (ai_stop_trigger == 5)
            {
                //----------------------------------------
                // Get the settings of In Range comparison stop condition
                //----------------------------------------
                ret = aio.GetAiStopInRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiStopInRangeEx = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        textBox_StopRangeChNo.Enabled = false;
                        textBox_StopRangeUpperLimitValue.Enabled = false;
                        textBox_StopRangeLowerLimitValue.Enabled = false;
                        textBox_StopRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // Update the initial display of upper limit
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // Update the initial display of lower limit
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // Update the initial display of state-holding times
                    //--------------------
                    textBox_StopRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Update upper limit
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // Update lower limit
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // Update state-holding times
                    //--------------------
                    textBox_StopRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // For Out Range comparison
            //----------------------------------------
            else if (ai_stop_trigger == 6)
            {
                //----------------------------------------
                // Get the settings of Out Range comparison stop condition
                //----------------------------------------
                ret = aio.GetAiStopOutRangeEx(Form1.g_id, 0, out ai_range_level1, out ai_range_level2, out ai_state_times);
                //----------------------------------------
                // Abnormal termination process
                //----------------------------------------
                if (ret != 0)
                {
                    if (ret != 20001)
                    {
                        //--------------------
                        // Get error string
                        //--------------------
                        aio.GetErrorString(ret, out error_string);
                        //--------------------
                        // Display message
                        //--------------------
                        message_string = string.Format("aio.GetAiStopOutRangeEx = {0:D} : {1}\n\n*The display could not be updated since an error occurred.", ret, error_string);
                        err_flag = 1;
                    }
                    else
                    {
                        //--------------------
                        // Disable control
                        //--------------------
                        textBox_StopRangeChNo.Enabled = false;
                        textBox_StopRangeUpperLimitValue.Enabled = false;
                        textBox_StopRangeLowerLimitValue.Enabled = false;
                        textBox_StopRangeStateTimes.Enabled = false;
                    }
                    //--------------------
                    // Update the initial display of upper limit
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = "10.0";
                    //--------------------
                    // Update the initial display of lower limit
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = "1.0";
                    //--------------------
                    // Update the initial display of state-holding times
                    //--------------------
                    textBox_StopRangeStateTimes.Text = "1";
                }
                //----------------------------------------
                // Normal termination process
                //----------------------------------------
                else
                {
                    //--------------------
                    // Update upper limit
                    //--------------------
                    textBox_StopRangeUpperLimitValue.Text = ai_range_level1.ToString("F1");
                    //--------------------
                    // Update lower limit
                    //--------------------
                    textBox_StopRangeLowerLimitValue.Text = ai_range_level2.ToString("F1");
                    //--------------------
                    // Update state-holding times
                    //--------------------
                    textBox_StopRangeStateTimes.Text = ai_state_times.ToString();
                }
            }
            //----------------------------------------
            // Other than In Range comparison / Out Range comparison
            //----------------------------------------
            else
            {
                //--------------------
                // Update the initial display of upper limit
                //--------------------
                textBox_StopRangeUpperLimitValue.Text = "10.0";
                //--------------------
                // Update the initial display of lower limit
                //--------------------
                textBox_StopRangeLowerLimitValue.Text = "1.0";
                //--------------------
                // Update the initial display of state-holding times
                //--------------------
                textBox_StopRangeStateTimes.Text = "1";
            }
            //----------------------------------------
            // Display message if an error occurs
            //----------------------------------------
            if (err_flag != 0)
            {
                MessageBox.Show(message_string, "Error", MessageBoxButtons.OK);
            }
        }

        //================================================================================
        // It is the process when the Start Condition combobox is changed
        // Update available controls
        //================================================================================
        private void comboBox_StartCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;    // Index of combobox

            //----------------------------------------
            // Get the index of start condition from combobox
            //----------------------------------------
            index = comboBox_StartCondition.SelectedIndex;
            //----------------------------------------
            // Branch processing by start condition
            //----------------------------------------
            switch (g_start_trigger[index])
            {
                case 0:    // Software
                case 1:    // External trigger rising edge
                case 2:    // External trigger falling edge
                    textBox_StartLevelChNo.Enabled = false;
                    textBox_StartLevelComprionValue.Enabled = false;
                    comboBox_StartLevelComprionDriection.Enabled = false;
                    textBox_StartRangeChNo.Enabled = false;
                    textBox_StartRangeUpperLimitValue.Enabled = false;
                    textBox_StartRangeLowerLimitValue.Enabled = false;
                    textBox_StartRangeStateTimes.Enabled = false;
                    break;
                case 3:    // Level comparison
                    textBox_StartLevelChNo.Enabled = true;
                    textBox_StartLevelComprionValue.Enabled = true;
                    comboBox_StartLevelComprionDriection.Enabled = true;
                    textBox_StartRangeChNo.Enabled = false;
                    textBox_StartRangeUpperLimitValue.Enabled = false;
                    textBox_StartRangeLowerLimitValue.Enabled = false;
                    textBox_StartRangeStateTimes.Enabled = false;
                    break;
                case 4:    // In Range comparison
                case 5:    // Out Range comparison
                    textBox_StartLevelChNo.Enabled = false;
                    textBox_StartLevelComprionValue.Enabled = false;
                    comboBox_StartLevelComprionDriection.Enabled = false;
                    textBox_StartRangeChNo.Enabled = true;
                    textBox_StartRangeUpperLimitValue.Enabled = true;
                    textBox_StartRangeLowerLimitValue.Enabled = true;
                    textBox_StartRangeStateTimes.Enabled = true;
                    break;
                default:
                    textBox_StartLevelChNo.Enabled = false;
                    textBox_StartLevelComprionValue.Enabled = false;
                    comboBox_StartLevelComprionDriection.Enabled = false;
                    textBox_StartRangeChNo.Enabled = false;
                    textBox_StartRangeUpperLimitValue.Enabled = false;
                    textBox_StartRangeLowerLimitValue.Enabled = false;
                    textBox_StartRangeStateTimes.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // It is the process when the Clock Type combobox is changed
        // Update available controls
        //================================================================================
        private void comboBox_ClockType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int     index;          // Index of combobox
            int     ret;            // For return code
            short   auto_mode;      // Set mode of transfer size
            int     transfer_size;  // Transfer size

            //----------------------------------------
            // Get the index of clock type from combobox
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // Branch processing by clock type
            //----------------------------------------
            switch (g_clock_type[index])
            {
                case 0:    // Internal clock
                    textBox_SamplingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    textBox_TransferSize.Enabled = false;
                    break;
                case 1:    // External clock
                    textBox_SamplingClock.Enabled = false;
                    comboBox_ExternalClockInputTiming.Enabled = true;
                    //----------------------------------------
                    // Get transfer size
                    //----------------------------------------
                    ret = aio.GetAiTransferSize(Form1.g_id, out auto_mode, out transfer_size);
                    //----------------------------------------
                    // Abnormal termination process
                    //----------------------------------------
                    if (ret != 0)
                    {
                        textBox_TransferSize.Enabled = false;
                    }
                    //----------------------------------------
                    // Normal termination process
                    //----------------------------------------
                    else
                    {
                        textBox_TransferSize.Enabled = true;
                    }
                    break;
                default:
                    textBox_SamplingClock.Enabled = true;
                    comboBox_ExternalClockInputTiming.Enabled = false;
                    textBox_TransferSize.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // It is the process when the Stop Condition combobox is changed
        // Update available controls
        //================================================================================
        private void comboBox_StopCondition_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index;    // Index of combobox

            //----------------------------------------
            // Get the index of stop condition from combobox
            //----------------------------------------
            index = comboBox_StopCondition.SelectedIndex;
            //----------------------------------------
            // Branch processing by stop condition
            //----------------------------------------
            switch (g_stop_trigger[index])
            {
                case 1:    // External trigger rising edge
                case 2:    // External trigger falling edge
                case 4:    // Command (AioStopAi)
                    textBox_StopLevelChNo.Enabled = false;
                    textBox_StopLevelComprionValue.Enabled = false;
                    comboBox_StopLevelComprionDriection.Enabled = false;
                    textBox_StopRangeChNo.Enabled = false;
                    textBox_StopRangeUpperLimitValue.Enabled = false;
                    textBox_StopRangeLowerLimitValue.Enabled = false;
                    textBox_StopRangeStateTimes.Enabled = false;
                    break;
                case 3:    // Level comparison
                    textBox_StopLevelChNo.Enabled = true;
                    textBox_StopLevelComprionValue.Enabled = true;
                    comboBox_StopLevelComprionDriection.Enabled = true;
                    textBox_StopRangeChNo.Enabled = false;
                    textBox_StopRangeUpperLimitValue.Enabled = false;
                    textBox_StopRangeLowerLimitValue.Enabled = false;
                    textBox_StopRangeStateTimes.Enabled = false;
                    break;
                case 5:    // In Range comparison
                case 6:    // Out Range comparison
                    textBox_StopLevelChNo.Enabled = false;
                    textBox_StopLevelComprionValue.Enabled = false;
                    comboBox_StopLevelComprionDriection.Enabled = false;
                    textBox_StopRangeChNo.Enabled = true;
                    textBox_StopRangeUpperLimitValue.Enabled = true;
                    textBox_StopRangeLowerLimitValue.Enabled = true;
                    textBox_StopRangeStateTimes.Enabled = true;
                    break;
                default:
                    textBox_StopLevelChNo.Enabled = false;
                    textBox_StopLevelComprionValue.Enabled = false;
                    comboBox_StopLevelComprionDriection.Enabled = false;
                    textBox_StopRangeChNo.Enabled = false;
                    textBox_StopRangeUpperLimitValue.Enabled = false;
                    textBox_StopRangeLowerLimitValue.Enabled = false;
                    textBox_StopRangeStateTimes.Enabled = false;
                    break;
            }
        }

        //================================================================================
        // It is the process when the OK button is clicked
        // Set sampling condition
        //================================================================================
        private void button_Ok_Click(object sender, EventArgs e)
        {
            string error_string;         // Error code string
            string text_string = "";     // For updating the textbox
            int    ret;                  // For return code
            int    index;                // Index of combobox
            short  ai_level_channel;     // Level comparison channel
            float  ai_level;             // Level comparison data
            short  ai_direction;         // Level comparison direction
            short  ai_range_channel;     // Channel of In Range / Out Range comparison
            float  ai_range_level1;      // Upper limit data of In Range / Out Range comparison
            float  ai_range_level2;      // Lower limit data of In Range / Out Range comparison
            int    ai_state_times;       // Sampling times of the state-holding
            float  ai_sampling_clock;    // Sampling clock
            float  ai_scan_clock;        // Conversion speed
            int    ai_transfer_size;     // Transfer size

            //----------------------------------------
            // Set the data storage memory type to Overwrite the memory
            // Caution for modify this function (if modified, the application may not work)
            //----------------------------------------
            ret = aio.SetAiMemoryType(Form1.g_id, 1);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = string.Format("aio.SetAiMemoryType = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // Get the index of the conversion start condition setting value from combobox
            //----------------------------------------
            index = comboBox_StartCondition.SelectedIndex;
            //----------------------------------------
            // Set analog input conversion start condition
            //----------------------------------------
            ret = aio.SetAiStartTrigger(Form1.g_id, g_start_trigger[index]);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = string.Format("aio.SetAiStartTrigger = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // For Level comparison
            //----------------------------------------
            if (g_start_trigger[index] == 3)
            {
                //----------------------------------------
                // Get the Level comparison channel from textbox
                //----------------------------------------
                if (short.TryParse(textBox_StartLevelChNo.Text, out ai_level_channel) == false)
                {
                    ai_level_channel = 0;
                }
                //----------------------------------------
                // Get the Level comparison data from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StartLevelComprionValue.Text, out ai_level) == false)
                {
                    ai_level = -10;
                }
                //----------------------------------------
                // Get the Level comparison direction from textbox
                //----------------------------------------
                ai_direction = (short)comboBox_StartLevelComprionDriection.SelectedIndex;
                //----------------------------------------
                // Set the level at start of level comparison
                //----------------------------------------
                ret = aio.SetAiStartLevelEx(Form1.g_id, ai_level_channel, ai_level, ai_direction);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiStartLevelEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // For In Range comparison
            //----------------------------------------
            else if (g_start_trigger[index] == 4)
            {
                //----------------------------------------
                // Get the channel of In Range comparison from textbox
                //----------------------------------------
                if (short.TryParse(textBox_StartRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // Get the upper limit data of In Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // Get the lower limit data of In Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // Get the state-holding times from textbox
                //----------------------------------------
                if (int.TryParse(textBox_StartRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // Set In Range comparison start condition
                //----------------------------------------
                ret = aio.SetAiStartInRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiStartInRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // For Out Range comparison
            //----------------------------------------
            else if (g_start_trigger[index] == 5)
            {
                //----------------------------------------
                // Get the channel of Out Range comparison from textbox
                //----------------------------------------
                if (short.TryParse(textBox_StartRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // Get the upper limit data of Out Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // Get the lower limit data of Out Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StartRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // Get the state-holding times from textbox
                //----------------------------------------
                if (int.TryParse(textBox_StartRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // Set Out Range comparison start condition
                //----------------------------------------
                ret = aio.SetAiStartOutRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiStartOutRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // Get the index of the clock type setting value from combobox
            //----------------------------------------
            index = comboBox_ClockType.SelectedIndex;
            //----------------------------------------
            // Set analog input clock type
            //----------------------------------------
            ret = aio.SetAiClockType(Form1.g_id, g_clock_type[index]);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = string.Format("aio.SetAiClockType = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // For Internal clock
            //----------------------------------------
            if (g_clock_type[index] == 0)
            {
                //----------------------------------------
                // Get the sampling clock from textbox
                //----------------------------------------
                if (float.TryParse(textBox_SamplingClock.Text, out ai_sampling_clock) == false)
                {
                    ai_sampling_clock = 1000;
                }
                //----------------------------------------
                // Set analog input sampling clock
                //----------------------------------------
                ret = aio.SetAiSamplingClock(Form1.g_id, ai_sampling_clock);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiSamplingClock = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Set analog input transfer size
                //----------------------------------------
                ret = aio.SetAiTransferSize(Form1.g_id, (short)CaioConst.AI_TRANSFERSIZE_AUTO, 0);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiTransferSize = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // For External clock
            //----------------------------------------
            else if (g_clock_type[index] == 1)
            {
                //----------------------------------------
                // Get the index of the external clock input timing setting value from combobox
                //----------------------------------------
                index = comboBox_ExternalClockInputTiming.SelectedIndex;
                //----------------------------------------
                // Set the timing of external clock input
                //----------------------------------------
                ret = aio.SetAiClockEdge(Form1.g_id, g_clock_edge[index]);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiClockEdge = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
                //----------------------------------------
                // Get the transfer size from textbox
                //----------------------------------------
                if (int.TryParse(textBox_TransferSize.Text, out ai_transfer_size) == false)
                {
                    ai_transfer_size = 1;
                }
                //----------------------------------------
                // Set analog input transfer size
                //----------------------------------------
                ret = aio.SetAiTransferSize(Form1.g_id, (short)CaioConst.AI_TRANSFERSIZE_MANUAL, ai_transfer_size);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if ((ret != 0) && (ret != 20001))
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiTransferSize = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // When conversion speed function is available
            //----------------------------------------
            if (textBox_ScanClock.Enabled != false)
            {
                //----------------------------------------
                // Get the conversion speed from textbox
                //----------------------------------------
                if (float.TryParse(textBox_ScanClock.Text, out ai_scan_clock) == false)
                {
                    ai_scan_clock = 2;
                }
                //----------------------------------------
                // Set analog input conversion speed
                //----------------------------------------
                ret = aio.SetAiScanClock(Form1.g_id, ai_scan_clock);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiScanClock = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // Get the index of the conversion stop condition setting value from combobox
            //----------------------------------------
            index = comboBox_StopCondition.SelectedIndex;
            //----------------------------------------
            // Set analog input conversion stop condition
            //----------------------------------------
            ret = aio.SetAiStopTrigger(Form1.g_id, g_stop_trigger[index]);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                text_string = string.Format("aio.SetAiStopTrigger = {0:D} : {1}", ret, error_string);
                MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                return;
            }
            //----------------------------------------
            // For Level comparison
            //----------------------------------------
            if (g_stop_trigger[index] == 3)
            {
                //----------------------------------------
                // Get the Level comparison channel from textbox
                //----------------------------------------
                if (short.TryParse(textBox_StopLevelChNo.Text, out ai_level_channel) == false)
                {
                    ai_level_channel = 0;
                }
                //----------------------------------------
                // Get the Level comparison data from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StopLevelComprionValue.Text, out ai_level) == false)
                {
                    ai_level = -10;
                }
                //----------------------------------------
                // Get the Level comparison direction from textbox
                //----------------------------------------
                ai_direction = (short)comboBox_StopLevelComprionDriection.SelectedIndex;
                //----------------------------------------
                // Set the level at stop of level comparison
                //----------------------------------------
                ret = aio.SetAiStopLevelEx(Form1.g_id, ai_level_channel, ai_level, ai_direction);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiStopLevelEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // For In Range comparison
            //----------------------------------------
            else if (g_stop_trigger[index] == 5)
            {
                //----------------------------------------
                // Get the channel of In Range comparison from textbox
                //----------------------------------------
                if (short.TryParse(textBox_StopRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // Get the upper limit data of In Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // Get the lower limit data of In Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // Get the state-holding times from textbox
                //----------------------------------------
                if (int.TryParse(textBox_StopRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // Set In Range comparison stop condition
                //----------------------------------------
                ret = aio.SetAiStopInRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiStopInRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // For Out Range comparison
            //----------------------------------------
            else if (g_stop_trigger[index] == 6)
            {
                //----------------------------------------
                // Get the channel of Out Range comparison from textbox
                //----------------------------------------
                if (short.TryParse(textBox_StopRangeChNo.Text, out ai_range_channel) == false)
                {
                    ai_range_channel = 0;
                }
                //----------------------------------------
                // Get the upper limit data of Out Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeUpperLimitValue.Text, out ai_range_level1) == false)
                {
                    ai_range_level1 = 10;
                }
                //----------------------------------------
                // Get the lower limit data of Out Range comparison from textbox
                //----------------------------------------
                if (float.TryParse(textBox_StopRangeLowerLimitValue.Text, out ai_range_level2) == false)
                {
                    ai_range_level2 = 1;
                }
                //----------------------------------------
                // Get the state-holding times from textbox
                //----------------------------------------
                if (int.TryParse(textBox_StopRangeStateTimes.Text, out ai_state_times) == false)
                {
                    ai_state_times = 1;
                }
                //----------------------------------------
                // Set Out Range comparison stop condition
                //----------------------------------------
                ret = aio.SetAiStopOutRangeEx(Form1.g_id, ai_range_channel, ai_range_level1, ai_range_level2, ai_state_times);
                //----------------------------------------
                // Error process
                //----------------------------------------
                //--------------------
                // Exit the process if not completed successfully
                //--------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    text_string = string.Format("aio.SetAiStopOutRangeEx = {0:D} : {1}", ret, error_string);
                    MessageBox.Show(text_string, "Error", MessageBoxButtons.OK);
                    return;
                }
            }
            //----------------------------------------
            // OK process
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Terminate the dialog
        //================================================================================
        private void button_Cancel_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Cancel process
            //----------------------------------------
            this.Close();
        }
    }
}
