﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CaioCs;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

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

        //----------------------------------------
        // Macro definition
        //----------------------------------------
        public const int MAX_AO_RANGE_NUM = 31;    // Maximum number of elements in analog output range array

        //----------------------------------------
        // Declaration of global variables
        //----------------------------------------
        public static short g_id;                                                                  // ID for device access
        public string       g_file_name = "GeneratingData.csv";                                    // Generating data output file name
        public int          g_generating_times;                                                    // Generating times
        public float[]      g_generating_data;                                                     // Output data
        public GCHandle     gCh;                                                                   // GCHandle for g_generating_data array
        public short[]      g_ao_range = new short[MAX_AO_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.P1TO5 };     // 1 ~ 5V
        public Form1()
        {
            InitializeComponent();
        }

        private void AoGeneratingInfiniteDev_Load(object sender, EventArgs e)
        {
            //----------------------------------------
            // Set the default button
            //----------------------------------------
            AcceptButton = button_Close;
            //----------------------------------------
            // Update the initial display of device name
            //----------------------------------------
            textBox_DeviceName.Text = "AIO000";
            //----------------------------------------
            // Update the initial display of number of channels for analog output
            //----------------------------------------
            textBox_ChNum.Text = "1";
            //----------------------------------------
            // Update the initial display of output data generating times
            //----------------------------------------
            textBox_GeneratingTimes.Text = "1000";
            //----------------------------------------
            // Update the initial display of the generating times when "Event that the specified number of data are output" is used
            //----------------------------------------
            textBox_EventGeneratingTimes.Text = "500";
            //----------------------------------------
            // Create the combobox for analog output range
            //----------------------------------------
            comboBox_RangeValue.Items.Add("-10 - +10V");
            comboBox_RangeValue.Items.Add("-5 - +5V");
            comboBox_RangeValue.Items.Add("-2.5 - +2.5V");
            comboBox_RangeValue.Items.Add("-1.25 - +1.25V");
            comboBox_RangeValue.Items.Add("-1 - +1V");
            comboBox_RangeValue.Items.Add("-0.625 - +0.625V");
            comboBox_RangeValue.Items.Add("-0.5 - +0.5V");
            comboBox_RangeValue.Items.Add("-0.3125 - +0.3125V");
            comboBox_RangeValue.Items.Add("-0.25 - +0.25V");
            comboBox_RangeValue.Items.Add("-0.125 - +0.125V");
            comboBox_RangeValue.Items.Add("-0.1 - +0.1V");
            comboBox_RangeValue.Items.Add("-0.05 - +0.05V");
            comboBox_RangeValue.Items.Add("-0.025 - +0.025V");
            comboBox_RangeValue.Items.Add("-0.0125 - +0.0125V");
            comboBox_RangeValue.Items.Add("-0.01 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +10V");
            comboBox_RangeValue.Items.Add("0 - +5V");
            comboBox_RangeValue.Items.Add("0 - +4.095V");
            comboBox_RangeValue.Items.Add("0 - +2.5V");
            comboBox_RangeValue.Items.Add("0 - +1.25V");
            comboBox_RangeValue.Items.Add("0 - +1V");
            comboBox_RangeValue.Items.Add("0 - +0.5V");
            comboBox_RangeValue.Items.Add("0 - +0.25V");
            comboBox_RangeValue.Items.Add("0 - +0.1V");
            comboBox_RangeValue.Items.Add("0 - +0.05V");
            comboBox_RangeValue.Items.Add("0 - +0.025V");
            comboBox_RangeValue.Items.Add("0 - +0.0125V");
            comboBox_RangeValue.Items.Add("0 - +0.01V");
            comboBox_RangeValue.Items.Add("0 - +20mA");
            comboBox_RangeValue.Items.Add("4 - +20mA");
            comboBox_RangeValue.Items.Add("1 - +5V");
            //----------------------------------------
            // Change the selection of analog output range combobox to 0
            //----------------------------------------
            comboBox_RangeValue.SelectedIndex = 0;
            //----------------------------------------
            // Initialize global variables
            //----------------------------------------
            g_id = 0;
        }

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

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

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

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

        //================================================================================
        // It is the process when the AioSetAoRangeAll button is clicked
        // Set the output range of analog output
        //================================================================================
        private void button_SetAoRangeAll_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            int    index;           // ndex of combobox

            //----------------------------------------
            // Update GUI information
            //----------------------------------------
            this.Refresh();
            //----------------------------------------
            // Get the index of range setting value from combobox
            //----------------------------------------
            index = comboBox_RangeValue.SelectedIndex;
            //----------------------------------------
            // Set the analog output range (all channels)
            //----------------------------------------
            ret = aio.SetAoRangeAll(g_id, g_ao_range[index]);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAoRangeAll = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the AioSetAoChannels button is clicked
        // Set the number of analog output channels used for generating
        //================================================================================
        private void button_SetAoChannels_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code
            short  ao_channels;     // Number of channels

            //----------------------------------------
            // Get the number of channels from textbox
            //----------------------------------------
            if (short.TryParse(textBox_ChNum.Text, out ao_channels) == false)
            {
                ao_channels = 1;
            }
            //----------------------------------------
            // Set the number of analog output channels used for conversion
            //----------------------------------------
            ret = aio.SetAoChannels(g_id, ao_channels);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAoChannels = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Condition Set button is clicked
        // Open the condition setting dialog
        //================================================================================
        private void button_ConditionSettings_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            int    ret;             // For return code

            //----------------------------------------
            // Open the condition setting dialog
            //----------------------------------------
            Condition condition_dlg = new Condition();
            if (condition_dlg.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------------
                // Set the event factor to 'Event that device operation end', 'Event that the specified number of data are output', 
                // 'Event that sampling clock error', and 'Event that DA conversion error'.
                // Caution for modify this function (if modified, the application may not work)
                //----------------------------------------
                ret = aio.SetAoEvent(g_id, Handle, (int)(CaioConst.AOE_END | CaioConst.AOE_DATA_NUM | CaioConst.AOE_SCERR | CaioConst.AOE_DAERR));
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Condition Set: aio.SetAoEvent = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display the message that condition setting is completed
                //----------------------------------------
                textBox_ReturnCode.Text = "Condition Set: Successful completion";
            }
            condition_dlg.Dispose();
        }

        //================================================================================
        // It is the process when the Generate Output Data button is clicked
        // Generate output data file
        //================================================================================
        private void button_OutputDataGenerate_Click(object sender, EventArgs e)
        {
            string       error_string;    // Error code string
            string       text_string;     // For updating the textbox
            StreamWriter fp;              // File pointer
            long         i, j;            // Loop count
            int          ret;             // For return code
            short        ao_channels;     // Number of channels
            short        ao_range;        // Range
            float        gain, offset;    // Coefficient of sine waveform

            //----------------------------------------
            // Get the number of channels
            //----------------------------------------
            ret = aio.GetAoChannels(g_id, out ao_channels);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generate Output Data: aio.GetAoChannels = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Get range (channel 0 only)
            //----------------------------------------
            ret = aio.GetAoRange(g_id, 0, out ao_range);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generate Output Data: aio.GetAoRange = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Get generating times from textbox
            //----------------------------------------
            if (int.TryParse(textBox_GeneratingTimes.Text, out g_generating_times) == false)
            {
                g_generating_times = 1000;
            }
            //----------------------------------------
            // Output data process
            //----------------------------------------
            //--------------------
            // Allocate an array for data storage
            //--------------------
            if (g_generating_data != null)
            {
                g_generating_data = null;
            }
            try
            {
                g_generating_data = new float[g_generating_times * ao_channels];
            }
            catch
            {
                textBox_ReturnCode.Text = "Generate Output Data: Failed to allocate an array for storing data";
                return;
            }
            //----------------------------------------
            // Specify Pinned handle to g_generating_data
            //----------------------------------------
            if (gCh.IsAllocated == false)
            {
                gCh = GCHandle.Alloc(g_generating_data, GCHandleType.Pinned);
            }
            //----------------------------------------
            // Create analog output data: sine waveform
            //----------------------------------------
            switch (ao_range)
            {
                case (short)CaioConst.PM10:
                    gain   = 10;
                    offset = 0;
                    break;
                case (short)CaioConst.PM5:
                    gain   = 5;
                    offset = 0;
                    break;
                case (short)CaioConst.PM25:
                    gain   = 2.5f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM125:
                    gain   = 1.25f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM1:
                    gain   = 1;
                    offset = 0;
                    break;
                case (short)CaioConst.PM0625:
                    gain   = 0.625f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM05:
                    gain   = 0.5f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM03125:
                    gain   = 0.3125f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM025:
                    gain   = 0.25f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM0125:
                    gain   = 0.125f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM01:
                    gain   = 0.1f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM005:
                    gain   = 0.05f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM0025:
                    gain   = 0.025f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM00125:
                    gain   = 0.0125f;
                    offset = 0;
                    break;
                case (short)CaioConst.PM001:
                    gain   = 0.01f;
                    offset = 0;
                    break;
                case (short)CaioConst.P10:
                    gain   = 5;
                    offset = 5;
                    break;
                case (short)CaioConst.P5:
                    gain   = 2.5f;
                    offset = 2.5f;
                    break;
                case (short)CaioConst.P4095:
                    gain   = 2.0475f;
                    offset = 2.0475f;
                    break;
                case (short)CaioConst.P25:
                    gain   = 1.25f;
                    offset = 1.25f;
                    break;
                case (short)CaioConst.P125:
                    gain   = 0.625f;
                    offset = 0.625f;
                    break;
                case (short)CaioConst.P1:
                    gain   = 0.5f;
                    offset = 0.5f;
                    break;
                case (short)CaioConst.P05:
                    gain   = 0.25f;
                    offset = 0.25f;
                    break;
                case (short)CaioConst.P025:
                    gain   = 0.125f;
                    offset = 0.125f;
                    break;
                case (short)CaioConst.P01:
                    gain   = 0.05f;
                    offset = 0.05f;
                    break;
                case (short)CaioConst.P005:
                    gain   = 0.025f;
                    offset = 0.025f;
                    break;
                case (short)CaioConst.P0025:
                    gain   = 0.0125f;
                    offset = 0.0125f;
                    break;
                case (short)CaioConst.P00125:
                    gain   = 0.00625f;
                    offset = 0.00625f;
                    break;
                case (short)CaioConst.P001:
                    gain   = 0.005f;
                    offset = 0.005f;
                    break;
                case (short)CaioConst.P20MA:
                    gain   = 10;
                    offset = 10;
                    break;
                case (short)CaioConst.P4TO20MA:
                    gain   = 8;
                    offset = 12;
                    break;
                case (short)CaioConst.P1TO5:
                    gain   = 2;
                    offset = 3;
                    break;
                default:
                    gain   = 10;
                    offset = 0;
                    break;
            }
            for (i = 0; i < g_generating_times; i++)
            {
                for (j = 0; j < ao_channels; j++)
                {
                    g_generating_data[i * ao_channels + j] = (float)(gain * Math.Sin(2 * 3.14159265358979 * i / g_generating_times + 3.14159265358979 / 2 * (j % 4)) + offset);
                }
            }
            //----------------------------------------
            // Open file
            //----------------------------------------
            try
            {
                fp = File.CreateText(g_file_name);
            }
            catch
            {
                textBox_ReturnCode.Text = "Generate Output Data: File.CreateText = file open error.";
                return;
            }
            //----------------------------------------
            // Create file
            //----------------------------------------
            for (i = 0; i < g_generating_times; 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 < ao_channels; j++)
                {
                    text_string += string.Format(" {0,10:F5}", g_generating_data[i * ao_channels + j]);
                    //--------------------
                    // Add newline character for the last channel
                    //--------------------
                    if (j == (ao_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_ReturnCode.Text = "Generate Output Data: fp.Write = file write error.";
                    fp.Close();
                    return;
                }
            }
            //----------------------------------------
            // Close file
            //----------------------------------------
            fp.Close();
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "Generate Output Data: Successful completion";
        }

        //================================================================================
        // It is the process when the AioSetAoEventSamplingTimes button is clicked
        // Set the generating times when "Event that the specified number of data are output" is used
        //================================================================================
        private void button_SetAoEventSamplingTimes_Click(object sender, EventArgs e)
        {
            string error_string;        // Error code string
            int    ret;                 // For return code
            int    generating_times;    // Generating times

            //----------------------------------------
            // Get the generating times from textbox
            //----------------------------------------
            if (int.TryParse(textBox_EventGeneratingTimes.Text, out generating_times) == false)
            {
                generating_times = 500;
            }
            //----------------------------------------
            // Set the generating times when "Event that the specified number of data are output" is used
            //----------------------------------------
            ret = aio.SetAoEventSamplingTimes(g_id, generating_times);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.SetAoEventSamplingTimes = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Generating Start button is clicked
        // After clearing the memory and status, start generating.
        //================================================================================
        private void button_GeneratingStart_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ao_status;       // Analog output status
            long   i, j;            // Loop count
            int    ret;             // For return code
            short  ao_channels;     // Number of channels
            short  ao_range;        // Range
            float  gain, offset;    // Coefficient of sine waveform

            //----------------------------------------
            // Reset driver memory
            //----------------------------------------
            ret = aio.ResetAoMemory(g_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generating Start: aio.ResetAoMemory = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Reset the analog output status
            //----------------------------------------
            ret = aio.ResetAoStatus(g_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generating Start: aio.ResetAoStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Get the generating times from textbox
            //----------------------------------------
            if (int.TryParse(textBox_GeneratingTimes.Text, out g_generating_times) == false)
            {
                g_generating_times = 1000;
            }
            //----------------------------------------
            // If the output data file does not exist
            //----------------------------------------
            if (File.Exists(g_file_name) == false)
            {
                //----------------------------------------
                // Get the number of channels
                //----------------------------------------
                ret = aio.GetAoChannels(g_id, out ao_channels);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Generating Start: aio.GetAoChannels = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Get range (channel 0 only)
                //----------------------------------------
                ret = aio.GetAoRange(g_id, 0, out ao_range);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Generating Start: aio.GetAoRange = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Output data process
                //----------------------------------------
                //--------------------
                // Allocate an array for data storage
                //--------------------
                if (g_generating_data != null)
                {
                    g_generating_data = null;
                }
                try
                {
                    g_generating_data = new float[g_generating_times * ao_channels];
                }
                catch
                {
                    textBox_ReturnCode.Text = "Generating Start: Failed to allocate an array for storing data";
                    return;
                }
                //----------------------------------------
                // Specify Pinned handle to g_generating_data
                //----------------------------------------
                if (gCh.IsAllocated == false)
                {
                    gCh = GCHandle.Alloc(g_generating_data, GCHandleType.Pinned);
                }
                //----------------------------------------
                // Create analog output data: sine waveform
                //----------------------------------------
                switch (ao_range)
                {
                    case (short)CaioConst.PM10:
                        gain   = 10;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM5:
                        gain   = 5;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM25:
                        gain   = 2.5f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM125:
                        gain   = 1.25f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM1:
                        gain   = 1;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM0625:
                        gain   = 0.625f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM05:
                        gain   = 0.5f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM03125:
                        gain   = 0.3125f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM025:
                        gain   = 0.25f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM0125:
                        gain   = 0.125f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM01:
                        gain   = 0.1f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM005:
                        gain   = 0.05f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM0025:
                        gain   = 0.025f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM00125:
                        gain   = 0.0125f;
                        offset = 0;
                        break;
                    case (short)CaioConst.PM001:
                        gain   = 0.01f;
                        offset = 0;
                        break;
                    case (short)CaioConst.P10:
                        gain   = 5;
                        offset = 5;
                        break;
                    case (short)CaioConst.P5:
                        gain   = 2.5f;
                        offset = 2.5f;
                        break;
                    case (short)CaioConst.P4095:
                        gain   = 2.0475f;
                        offset = 2.0475f;
                        break;
                    case (short)CaioConst.P25:
                        gain   = 1.25f;
                        offset = 1.25f;
                        break;
                    case (short)CaioConst.P125:
                        gain   = 0.625f;
                        offset = 0.625f;
                        break;
                    case (short)CaioConst.P1:
                        gain   = 0.5f;
                        offset = 0.5f;
                        break;
                    case (short)CaioConst.P05:
                        gain   = 0.25f;
                        offset = 0.25f;
                        break;
                    case (short)CaioConst.P025:
                        gain   = 0.125f;
                        offset = 0.125f;
                        break;
                    case (short)CaioConst.P01:
                        gain   = 0.05f;
                        offset = 0.05f;
                        break;
                    case (short)CaioConst.P005:
                        gain   = 0.025f;
                        offset = 0.025f;
                        break;
                    case (short)CaioConst.P0025:
                        gain   = 0.0125f;
                        offset = 0.0125f;
                        break;
                    case (short)CaioConst.P00125:
                        gain   = 0.00625f;
                        offset = 0.00625f;
                        break;
                    case (short)CaioConst.P001:
                        gain   = 0.005f;
                        offset = 0.005f;
                        break;
                    case (short)CaioConst.P20MA:
                        gain   = 10;
                        offset = 10;
                        break;
                    case (short)CaioConst.P4TO20MA:
                        gain   = 8;
                        offset = 12;
                        break;
                    case (short)CaioConst.P1TO5:
                        gain   = 2;
                        offset = 3;
                        break;
                    default:
                        gain   = 10;
                        offset = 0;
                        break;
                }
                for (i = 0; i < g_generating_times; i++)
                {
                    for (j = 0; j < ao_channels; j++)
                    {
                        g_generating_data[i * ao_channels + j] = (float)(gain * Math.Sin(2 * 3.14159265358979 * i / g_generating_times + 3.14159265358979 / 2 * (j % 4)) + offset);
                    }
                }
            }
            //----------------------------------------
            // Set analog output data
            //----------------------------------------
            ret = aio.SetAoSamplingDataEx(g_id, g_generating_times, g_generating_data);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generating Start: aio.SetAoSamplingDataEx = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Start generating
            //----------------------------------------
            ret = aio.StartAo(g_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            //--------------------
            // Exit the process if not completed successfully
            //--------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generating Start: aio.StartAo = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Get status
            //----------------------------------------
            ret = aio.GetAoStatus(g_id, out ao_status);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generating Start: aio.GetAoStatus = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display status
            //----------------------------------------
            text_string = string.Format("{0:x8}H (", ao_status);
            //--------------------
            // When status 'Device is running' is ON
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
            {
                text_string += "Operating";
            }
            //--------------------
            // When status 'Device is running' is OFF
            //--------------------
            else
            {
                text_string += "Stop";
            }
            //--------------------
            // When status 'Wait the start trigger' is ON
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
            {
                text_string += ", Wait the start trigger";
            }
            //--------------------
            // When status 'The specified number of data are output' is ON
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
            {
                text_string += ", The specified number of data are output";
            }
            //--------------------
            // When status 'Sampling clock error' is ON
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
            {
                text_string += ", Sampling clock error";
            }
            //--------------------
            // When status 'DA conversion error' is ON
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
            {
                text_string += ", DA conversion error";
            }
            //--------------------
            // When status 'Driver spec error' is ON
            //--------------------
            if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
            {
                text_string += ", Driver spec error";
            }
            text_string += ")";
            textBox_Status.Text = text_string;
            //----------------------------------------
            // Update the total generating count
            //----------------------------------------
            textBox_TotalGeneratingCount.Text = "0";
            //----------------------------------------
            //  Update the setting generating count
            //----------------------------------------
            textBox_SettingGeneratingCount.Text = "0";
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "Generating Start: Successful completion";
        }

        //================================================================================
        // It is the process when the AioGetAoSamplingCount button is clicked
        // Get the generating count and display it on the dialog
        //================================================================================
        private void button_GetAoSamplingCount_Click(object sender, EventArgs e)
        {
            string error_string;           // Error code string
            int    ret;                    // For return code
            int    ao_generating_count;    // Generating count

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

        //================================================================================
        // It is the process when the AioGetAoSamplingTimes button is clicked
        // Acquire the remaining output data amount by the generating count and display it on the dialog
        //================================================================================
        private void button_GetAoSamplingTimes_Click(object sender, EventArgs e)
        {
            string error_string;           // Error code string
            int    ret;                    // For return code
            int    ao_generating_times;    // Generating count

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

        //================================================================================
        // It is the process when the AioGetAoStatus button is clicked
        // Get the analog output status and display it on the dialog
        //================================================================================
        private void button_GetAoStatus_Click(object sender, EventArgs e)
        {
            string error_string;    // Error code string
            string text_string;     // For updating the textbox
            int    ao_status;       // Analog output status
            int    ret;             // For return code

            //----------------------------------------
            // Get status
            //----------------------------------------
            ret = aio.GetAoStatus(g_id, out ao_status);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.GetAoStatus = " + ret.ToString() + " : " + error_string;
            //----------------------------------------
            // When the status acquisition function is completed successfully
            //----------------------------------------
            if (ret == 0)
            {
                //----------------------------------------
                // Display status
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // When status 'Device is running' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "Operating";
                }
                //--------------------
                // When status 'Device is running' is OFF
                //--------------------
                else
                {
                    text_string += "Stop";
                }
                //--------------------
                // When status 'Wait the start trigger' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", Wait the start trigger";
                }
                //--------------------
                // When status 'The specified number of data are output' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", The specified number of data are output";
                }
                //--------------------
                // When status 'Sampling clock error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", Sampling clock error";
                }
                //--------------------
                // When status 'DA conversion error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA conversion error";
                }
                //--------------------
                // When status 'Driver spec error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", Driver spec error";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
            }
        }

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

            //----------------------------------------
            // Stop generating
            //----------------------------------------
            ret = aio.StopAo(g_id);
            //----------------------------------------
            // Error process
            //----------------------------------------
            if (ret != 0)
            {
                //--------------------
                // Get error string
                //--------------------
                aio.GetErrorString(ret, out error_string);
                //--------------------
                // Display message
                //--------------------
                textBox_ReturnCode.Text = "Generating Stop: aio.StopAo = " + ret.ToString() + " : " + error_string;
                return;
            }
            //----------------------------------------
            // Display the message that generating stop is completed
            //----------------------------------------
            textBox_ReturnCode.Text = "Generating Stop: Successful completion";
        }

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

            //----------------------------------------
            // Release the output data buffer
            //----------------------------------------
            if (g_generating_data != null)
            {
                g_generating_data = null;
            }
            //----------------------------------------
            // Exit process
            //----------------------------------------
            ret = aio.Exit(g_id);
            //----------------------------------------
            // Get error string
            //----------------------------------------
            aio.GetErrorString(ret, out error_string);
            //----------------------------------------
            // Display message
            //----------------------------------------
            textBox_ReturnCode.Text = "aio.Exit = " + ret.ToString() + " : " + error_string;
        }

        //================================================================================
        // It is the process when the Close button is clicked
        // Perform the exit process of device and terminate the application
        //================================================================================
        private void button_Close_Click(object sender, EventArgs e)
        {
            //----------------------------------------
            // Release the output data buffer
            //----------------------------------------
            if (g_generating_data != null)
            {
                g_generating_data = null;
            }
            //----------------------------------------
            // GCHandle free
            //----------------------------------------
            if (gCh.IsAllocated == true)
            {
                gCh.Free();
            }
            //----------------------------------------
            // Exit process
            //----------------------------------------
            aio.Exit(g_id);
            //----------------------------------------
            // Close dialog
            //----------------------------------------
            this.Close();
        }

        //================================================================================
        // Operation when event occurred
        // Process for each event occurred
        //================================================================================
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            string error_string;           // Error code string
            string text_string;            // For updating the textbox
            int    ao_generating_count;    // Generating count
            int    ao_generating_times;    // Generating times
            int    ao_status;              // Status
            int    ret;                    // For return code

            //----------------------------------------
            // Event that the specified number of data are output
            // Acquire and display data
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_DATA_NUM)
            {
                //----------------------------------------
                // Get status
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Event that the specified number of data are output Process: aio.GetAoStatus = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display status
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // When status 'Device is running' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "Operating";
                }
                //--------------------
                // When status 'Device is running' is OFF
                //--------------------
                else
                {
                    text_string += "Stop";
                }
                //--------------------
                // When status 'Wait the start trigger' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", Wait the start trigger";
                }
                //--------------------
                // When status 'The specified number of data are output' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", The specified number of data are output";
                }
                //--------------------
                // When status 'Sampling clock error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", Sampling clock error";
                }
                //--------------------
                // When status 'DA conversion error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA conversion error";
                }
                //--------------------
                // When status 'Driver spec error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", Driver spec error";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // Set analog output data
                //----------------------------------------
                ret = aio.SetAoSamplingDataEx(g_id, g_generating_times, g_generating_data);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Event that the specified number of data are output Process: aio.SetAoSamplingDataEx = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Get the current generating count
                //----------------------------------------
                ret = aio.GetAoSamplingCount(g_id, out ao_generating_count);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                   aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Event that the specified number of data are output Process: aio.GetAoSamplingCount = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display the total generating count
                //----------------------------------------
                textBox_TotalGeneratingCount.Text = ao_generating_count.ToString();
                //----------------------------------------
                // Get the current generating count
                //----------------------------------------
                ret = aio.GetAoSamplingTimes(g_id, out ao_generating_times);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Event that the specified number of data are output Process: aio.GetAoSamplingTimes = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display the setting generating count
                //----------------------------------------
                textBox_SettingGeneratingCount.Text = ao_generating_times.ToString();
                //----------------------------------------
                // Display the message that event processing is completed
                //----------------------------------------
                textBox_ReturnCode.Text = "Event that the specified number of data are output Process: Successful completion";
            }
            //----------------------------------------
            // Event that device operation end
            // Acquire and display data
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_END)
            {
                //----------------------------------------
                // Get status
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Device Operation End Event Process: aio.GetAoStatus = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display status
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // When status 'Device is running' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "Operating";
                }
                //--------------------
                // When status 'Device is running' is OFF
                //--------------------
                else
                {
                    text_string += "Stop";
                }
                //--------------------
                // When status 'Wait the start trigger' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", Wait the start trigger";
                }
                //--------------------
                // When status 'The specified number of data are output' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", The specified number of data are output";
                }
                //--------------------
                // When status 'Sampling clock error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", Sampling clock error";
                }
                //--------------------
                // When status 'DA conversion error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA conversion error";
                }
                //--------------------
                // When status 'Driver spec error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", Driver spec error";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // Get the current generating count
                //----------------------------------------
                ret = aio.GetAoSamplingCount(g_id, out ao_generating_count);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Device Operation End Event Process: aio.GetAoSamplingCount = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display the total generating count
                //----------------------------------------
                textBox_TotalGeneratingCount.Text = ao_generating_count.ToString();
                //----------------------------------------
                // Get the current generating count
                //----------------------------------------
                ret = aio.GetAoSamplingTimes(g_id, out ao_generating_times);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Device Operation End Event Process: aio.GetAoSamplingTimes = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display the setting generating count
                //----------------------------------------
                textBox_SettingGeneratingCount.Text = ao_generating_times.ToString();
                //----------------------------------------
                // Display the message that event processing is completed
                //----------------------------------------
                textBox_ReturnCode.Text = "Device Operation End Event Process: Successful completion";
            }
            //----------------------------------------
            // Event that sampling clock error
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_SCERR)
            {
                //----------------------------------------
                // Get status
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: aio.GetAoStatus = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display status
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // When status 'Device is running' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "Operating";
                }
                //--------------------
                // When status 'Device is running' is OFF
                //--------------------
                else
                {
                    text_string += "Stop";
                }
                //--------------------
                // When status 'Wait the start trigger' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", Wait the start trigger";
                }
                //--------------------
                // When status 'The specified number of data are output' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", The specified number of data are output";
                }
                //--------------------
                // When status 'Sampling clock error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", Sampling clock error";
                }
                //--------------------
                // When status 'DA conversion error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA conversion error";
                }
                //--------------------
                // When status 'Driver spec error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", Driver spec error";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // Display the message that event processing is completed
                //----------------------------------------
                textBox_ReturnCode.Text = "Sampling Clock Error Occurrence Process: Successful completion";
            }
            //----------------------------------------
            // Event that DA conversion error
            // Display the status
            //----------------------------------------
            if (m.Msg == (int)CaioConst.AIOM_AOE_DAERR)
            {
                //----------------------------------------
                // Get status
                //----------------------------------------
                ret = aio.GetAoStatus(g_id, out ao_status);
                //----------------------------------------
                // Error process
                //----------------------------------------
                if (ret != 0)
                {
                    //--------------------
                    // Get error string
                    //--------------------
                    aio.GetErrorString(ret, out error_string);
                    //--------------------
                    // Display message
                    //--------------------
                    textBox_ReturnCode.Text = "DA Conversion Error Occurrence Process: aio.GetAoStatus = " + ret.ToString() + " : " + error_string;
                    return;
                }
                //----------------------------------------
                // Display status
                //----------------------------------------
                text_string = string.Format("{0:x8}H (", ao_status);
                //--------------------
                // When status 'Device is running' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_BUSY) == (int)CaioConst.AOS_BUSY)
                {
                    text_string += "Operating";
                }
                //--------------------
                // When status 'Device is running' is OFF
                //--------------------
                else
                {
                    text_string += "Stop";
                }
                //--------------------
                // When status 'Wait the start trigger' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_START_TRG) == (int)CaioConst.AOS_START_TRG)
                {
                    text_string += ", Wait the start trigger";
                }
                //--------------------
                // When status 'The specified number of data are output' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DATA_NUM) == (int)CaioConst.AOS_DATA_NUM)
                {
                    text_string += ", The specified number of data are output";
                }
                //--------------------
                // When status 'Sampling clock error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_SCERR) == (int)CaioConst.AOS_SCERR)
                {
                    text_string += ", Sampling clock error";
                }
                //--------------------
                // When status 'DA conversion error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_AOERR) == (int)CaioConst.AOS_AOERR)
                {
                    text_string += ", DA conversion error";
                }
                //--------------------
                // When status 'Driver spec error' is ON
                //--------------------
                if ((ao_status & (int)CaioConst.AOS_DRVERR) == (int)CaioConst.AOS_DRVERR)
                {
                    text_string += ", Driver spec error";
                }
                text_string += ")";
                textBox_Status.Text = text_string;
                //----------------------------------------
                // Display the message that event processing is completed
                //----------------------------------------
                textBox_ReturnCode.Text = "DA Conversion Error Occurrence Process: Successful completion";
            }
            base.WndProc(ref m);
        }
    }
}
