﻿using CdioCs;

namespace MultiIO
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.TextBox[] m_InPort = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.TextBox[] m_InBit = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.TextBox[] m_OutPort = new System.Windows.Forms.TextBox[8];
        private System.Windows.Forms.TextBox[] m_OutBit = new System.Windows.Forms.TextBox[8];

        /// <summary>
        /// Create instance Cdio class
        /// </summary>
        Cdio dio = new Cdio();

        public short m_Id;
        public short m_InPortNum;
        public short m_OutPortNum;
        public Form1()
        {
            InitializeComponent();

            m_InPort[0] = textBox_MultiPortInput_0;
            m_InPort[1] = textBox_MultiPortInput_1;
            m_InPort[2] = textBox_MultiPortInput_2;
            m_InPort[3] = textBox_MultiPortInput_3;
            m_InPort[4] = textBox_MultiPortInput_4;
            m_InPort[5] = textBox_MultiPortInput_5;
            m_InPort[6] = textBox_MultiPortInput_6;
            m_InPort[7] = textBox_MultiPortInput_7;

            m_InBit[0] = textBox_MultiBitInput_0;
            m_InBit[1] = textBox_MultiBitInput_1;
            m_InBit[2] = textBox_MultiBitInput_2;
            m_InBit[3] = textBox_MultiBitInput_3;
            m_InBit[4] = textBox_MultiBitInput_4;
            m_InBit[5] = textBox_MultiBitInput_5;
            m_InBit[6] = textBox_MultiBitInput_6;
            m_InBit[7] = textBox_MultiBitInput_7;

            m_OutPort[0] = textBox_MultiPortOutput_0;
            m_OutPort[1] = textBox_MultiPortOutput_1;
            m_OutPort[2] = textBox_MultiPortOutput_2;
            m_OutPort[3] = textBox_MultiPortOutput_3;
            m_OutPort[4] = textBox_MultiPortOutput_4;
            m_OutPort[5] = textBox_MultiPortOutput_5;
            m_OutPort[6] = textBox_MultiPortOutput_6;
            m_OutPort[7] = textBox_MultiPortOutput_7;

            m_OutBit[0] = textBox_MultiBitOutput_0;
            m_OutBit[1] = textBox_MultiBitOutput_1;
            m_OutBit[2] = textBox_MultiBitOutput_2;
            m_OutBit[3] = textBox_MultiBitOutput_3;
            m_OutBit[4] = textBox_MultiBitOutput_4;
            m_OutBit[5] = textBox_MultiBitOutput_5;
            m_OutBit[6] = textBox_MultiBitOutput_6;
            m_OutBit[7] = textBox_MultiBitOutput_7;
        }

        private void button_DioInit_Click(object sender, System.EventArgs e)
        {
            // Initialization handling
            int Ret;
            Ret = dio.Init(textBox_DeviceName.Text, out m_Id);

            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Get all ports number
            //-----------------------------
            Ret = dio.GetMaxPorts(m_Id, out m_InPortNum, out m_OutPortNum);

            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            // This sample must not exceed 8 ports
            if (m_InPortNum > 8)
            {
                m_InPortNum = 8;
            }
            if (m_OutPortNum > 8)
            {
                m_OutPortNum = 8;
            }
            //-----------------------------
            // Mask screen
            //-----------------------------
            for (short InPort = m_InPortNum; InPort < 8; InPort++)
            {
                m_InPort[InPort].Enabled = false;
            }
            for (short OutPort = m_OutPortNum; OutPort < 8; OutPort++)
            {
                m_OutPort[OutPort].Enabled = false;
            }
        }

        private void button_DioInpMutiByte_Click(object sender, System.EventArgs e)
        {
            short[] PortNo = new short[8];
            byte[] Data = new byte[8];
            //-----------------------------
            // Set Port No.
            //-----------------------------
            for (short InPort = 0; InPort < m_InPortNum; InPort++)
            {
                PortNo[InPort] = InPort;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            int Ret;
            Ret = dio.InpMultiByte(m_Id, PortNo, m_InPortNum, Data);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Data express
            //-----------------------------
            for (short InPort = 0; InPort < m_InPortNum; InPort++)
            {
                m_InPort[InPort].Text = System.Convert.ToString(Data[InPort], 16);
            }
        }

        private void button_DioInpMutiBit_Click(object sender, System.EventArgs e)
        {
            short[] BitNo = new short[8];
            byte[] Data = new byte[8];
            //-----------------------------
            // Set Bit No.
            //-----------------------------
            for (short InBit = 0; InBit < 8; InBit++)
            {
                BitNo[InBit] = InBit;
            }
            //-----------------------------
            // Bit input
            //-----------------------------
            int Ret;
            Ret = dio.InpMultiBit(m_Id, BitNo, 8, Data);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Data express
            //-----------------------------
            for (short InBit = 0; InBit < 8; InBit++)
            {
                m_InBit[InBit].Text = System.Convert.ToString(Data[InBit], 16);
            }
        }

        private void button_DioOutMultiByte_Click(object sender, System.EventArgs e)
        {
            short[] PortNo = new short[8];
            byte[] Data = new byte[8];
            //-----------------------------
            // Get data
            //-----------------------------
            for (short OutPort = 0; OutPort < m_OutPortNum; OutPort++)
            {
                try
                {
                    Data[OutPort] = System.Convert.ToByte(m_OutPort[OutPort].Text, 16);
                }
                catch
                {
                    Data[OutPort] = 0;
                }
            }
            //-----------------------------
            // Set Port No.
            //-----------------------------
            for (short OutPort = 0; OutPort < m_OutPortNum; OutPort++)
            {
                PortNo[OutPort] = OutPort;
            }
            //-----------------------------
            // Port output
            //-----------------------------
            int Ret;
            Ret = dio.OutMultiByte(m_Id, PortNo, m_OutPortNum, Data);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        private void button_DioOutMutiBit_Click(object sender, System.EventArgs e)
        {
            short[] BitNo = new short[8];
            byte[] Data = new byte[8];
            //-----------------------------
            // Get data
            //-----------------------------
            for (short OutBit = 0; OutBit < 8; OutBit++)
            {
                try
                {
                    Data[OutBit] = System.Convert.ToByte(m_OutBit[OutBit].Text, 16);
                }
                catch
                {
                    Data[OutBit] = 0;
                }
            }
            //-----------------------------
            // Set Bit No.
            //-----------------------------
            for (short OutBit = 0; OutBit < 8; OutBit++)
            {
                BitNo[OutBit] = OutBit;
            }
            //-----------------------------
            // Bit output
            //-----------------------------
            int Ret;
            Ret = dio.OutMultiBit(m_Id, BitNo, 8, Data);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
            if (Ret != (int)CdioConst.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        private void button_DioExit_Click(object sender, System.EventArgs e)
        {
            // Exit handling of device
            int Ret;
            Ret = dio.Exit(m_Id);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }

        private void button_Exit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void button_DioSetIoDirection_Click(object sender, System.EventArgs e)
        {
            uint dwDir;

            //-----------------------------
            // Get IoDirection
            //-----------------------------
            try
            {
                dwDir = System.Convert.ToByte(textBox_DirData.Text, 16);
            }
            catch
            {
                dwDir = 0;
            }
            //-----------------------------
            // Set IoDirection
            //-----------------------------
            int Ret;
            Ret = dio.SetIoDirection(m_Id, dwDir);
            //-----------------------------
            // Error process
            //-----------------------------
            string ErrorString;
            dio.GetErrorString(Ret, out ErrorString);
            textBox_ReturnCode.Text = "Ret = " + System.Convert.ToString(Ret) + " : " + ErrorString;
        }
    }
}