using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;

namespace MultiIO
{
    public partial class Form1 : Form
    {
        private const short DIO_MAX_ACCS_PORTS = 8;         // Maxinum number of access ports in this sample
        private const short DIO_MAX_ACCS_BITS = 8;          // Maxinum number of access bits in this sample
                                                            // Additional data
        short Id;                                           // Device ID
        int Ret;                                            // Return value
        short InpPortNum;                                   // Number of input ports
        short OutPortNum;                                   // Number of output ports
        short InpBitNum;                                    // Number of input bits
        short OutBitNum;                                    // Number of output bits
        StringBuilder szError = new StringBuilder("", 256); // Error string
        string szReturnCode = new string(' ', 256);         // The string for error
        string szText = new string(' ', 100);
        TextBox[] m_InpPort = new TextBox[8];
        TextBox[] m_InpBit = new TextBox[8];
        TextBox[] m_OutPort = new TextBox[8];
        TextBox[] m_OutBit = new TextBox[8];

        public Form1()
        {
            InitializeComponent();
        }

        //---------------------------------------------------------
        // Initialize dialog
        //---------------------------------------------------------
        private void MultiIO_Load(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // Detect device
            //-----------------------------------
            short Index;
            short GroupID, UnitID, DeviceID;
            StringBuilder DeviceName = new StringBuilder(256);
            StringBuilder Device = new StringBuilder(256);
            GroupID = 0;
            UnitID = 0;
            DeviceID = 0;
            for (Index = 0; ; Index++)
            {
                Ret = DIO.Ccapdio.DioQueryDeviceName(Index, ref GroupID, ref UnitID, ref DeviceID, DeviceName, Device);
                if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
                {
                    break;
                }
                cboDeviceName.Items.Add(DeviceName);
            }
            cboDeviceName.SelectedIndex = 0;
            //-----------------------------------
            // Initialize screen items
            //-----------------------------------
            // Port input
            txtInpPortData0.Text = "";
            txtInpPortData1.Text = "";
            txtInpPortData2.Text = "";
            txtInpPortData3.Text = "";
            txtInpPortData4.Text = "";
            txtInpPortData5.Text = "";
            txtInpPortData6.Text = "";
            txtInpPortData7.Text = "";
            m_InpPort[0] = txtInpPortData0;
            m_InpPort[1] = txtInpPortData1;
            m_InpPort[2] = txtInpPortData2;
            m_InpPort[3] = txtInpPortData3;
            m_InpPort[4] = txtInpPortData4;
            m_InpPort[5] = txtInpPortData5;
            m_InpPort[6] = txtInpPortData6;
            m_InpPort[7] = txtInpPortData7;
            // Bit input
            txtInpBitData0.Text = "";
            txtInpBitData1.Text = "";
            txtInpBitData2.Text = "";
            txtInpBitData3.Text = "";
            txtInpBitData4.Text = "";
            txtInpBitData5.Text = "";
            txtInpBitData6.Text = "";
            txtInpBitData7.Text = "";
            m_InpBit[0] = txtInpBitData0;
            m_InpBit[1] = txtInpBitData1;
            m_InpBit[2] = txtInpBitData2;
            m_InpBit[3] = txtInpBitData3;
            m_InpBit[4] = txtInpBitData4;
            m_InpBit[5] = txtInpBitData5;
            m_InpBit[6] = txtInpBitData6;
            m_InpBit[7] = txtInpBitData7;
            // Port output
            txtOutPortData0.Text = "11";
            txtOutPortData1.Text = "22";
            txtOutPortData2.Text = "33";
            txtOutPortData3.Text = "44";
            txtOutPortData4.Text = "55";
            txtOutPortData5.Text = "66";
            txtOutPortData6.Text = "77";
            txtOutPortData7.Text = "88";
            m_OutPort[0] = txtOutPortData0;
            m_OutPort[1] = txtOutPortData1;
            m_OutPort[2] = txtOutPortData2;
            m_OutPort[3] = txtOutPortData3;
            m_OutPort[4] = txtOutPortData4;
            m_OutPort[5] = txtOutPortData5;
            m_OutPort[6] = txtOutPortData6;
            m_OutPort[7] = txtOutPortData7;
            // Bit output
            txtOutBitData0.Text = "1";
            txtOutBitData1.Text = "0";
            txtOutBitData2.Text = "1";
            txtOutBitData3.Text = "0";
            txtOutBitData4.Text = "1";
            txtOutBitData5.Text = "0";
            txtOutBitData6.Text = "1";
            txtOutBitData7.Text = "0";
            m_OutBit[0] = txtOutBitData0;
            m_OutBit[1] = txtOutBitData1;
            m_OutBit[2] = txtOutBitData2;
            m_OutBit[3] = txtOutBitData3;
            m_OutBit[4] = txtOutBitData4;
            m_OutBit[5] = txtOutBitData5;
            m_OutBit[6] = txtOutBitData6;
            m_OutBit[7] = txtOutBitData7;
            // Return value
            txtReturnCode.Text = "";
        }

        //---------------------------------------------------------
        // Initialization
        //---------------------------------------------------------
        private void btnDioInit_Click(object sender, System.EventArgs e)
        {
            string szDeviceName = new string(' ', 100);
            short i;
            //-----------------------------
            // Get device name from screen
            //-----------------------------
            szDeviceName = cboDeviceName.Text;
            //-----------------------------
            // Initialize and get ID
            //-----------------------------
            Ret = DIO.Ccapdio.DioInit(szDeviceName, ref Id);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Get maximum number of ports
            //-----------------------------
            Ret = DIO.Ccapdio.DioGetMaxPorts(Id, ref InpPortNum, ref OutPortNum);
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                DIO.Ccapdio.DioGetErrorString(Ret, szError);
                szReturnCode = "DioGetMaxPorts : " + szError;
                txtReturnCode.Text = szReturnCode;
                return;
            }
            // Up to 8 ports in this sample
            if (InpPortNum > DIO_MAX_ACCS_PORTS)
            {
                InpPortNum = DIO_MAX_ACCS_PORTS;
            }
            if (OutPortNum > DIO_MAX_ACCS_PORTS)
            {
                OutPortNum = DIO_MAX_ACCS_PORTS;
            }
            //-----------------------------
            // Get maximum number of bits
            //-----------------------------
            Ret = DIO.Ccapdio.DioGetMaxBits(Id, ref InpBitNum, ref OutBitNum);
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                DIO.Ccapdio.DioGetErrorString(Ret, szError);
                szReturnCode = "DioGetMaxBits : " + szError;
                txtReturnCode.Text = szReturnCode;
                return;
            }
            // Up to 8 bits in this sample
            if (InpBitNum > DIO_MAX_ACCS_BITS)
            {
                InpBitNum = DIO_MAX_ACCS_BITS;
            }
            if (OutBitNum > DIO_MAX_ACCS_BITS)
            {
                OutBitNum = DIO_MAX_ACCS_BITS;
            }
            //-----------------------------
            // Mask screen
            //-----------------------------
            for (i = 0; i < DIO_MAX_ACCS_PORTS; i++)
            {
                if (i < InpPortNum)
                {
                    m_InpPort[i].Enabled = true;
                }
                else
                {
                    m_InpPort[i].Enabled = false;
                }
            }
            for (i = 0; i < DIO_MAX_ACCS_BITS; i++)
            {
                if (i < InpBitNum)
                {
                    m_InpBit[i].Enabled = true;
                }
                else
                {
                    m_InpBit[i].Enabled = false;
                }
            }
            for (i = 0; i < DIO_MAX_ACCS_PORTS; i++)
            {
                if (i < OutPortNum)
                {
                    m_OutPort[i].Enabled = true;
                }
                else
                {
                    m_OutPort[i].Enabled = false;
                }
            }
            for (i = 0; i < DIO_MAX_ACCS_BITS; i++)
            {
                if (i < OutBitNum)
                {
                    m_OutBit[i].Enabled = true;
                }
                else
                {
                    m_OutBit[i].Enabled = false;
                }
            }
        }

        //---------------------------------------------------------
        // MultiPort input
        //---------------------------------------------------------
        private void btnInpMultiByte_Click(object sender, System.EventArgs e)
        {
            short[] InpPortNo = new short[DIO_MAX_ACCS_PORTS];
            byte[] InpPortData = new byte[DIO_MAX_ACCS_PORTS];
            short i;
            //-----------------------------
            // Set port No.
            //-----------------------------
            for (i = 0; i < InpPortNum; i++)
            {
                InpPortNo[i] = i;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            GCHandle PterPNo = GCHandle.Alloc(InpPortNo, GCHandleType.Pinned);
            GCHandle PterPData = GCHandle.Alloc(InpPortData, GCHandleType.Pinned);
            Ret = DIO.Ccapdio.DioInpMultiByte(Id, InpPortNo, InpPortNum, InpPortData);
            PterPNo.Free();
            PterPData.Free();
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInpMultiByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Display data
            //-----------------------------
            for (i = 0; i < InpPortNum; i++)
            {
                szText = string.Format("{0:X2}", InpPortData[i]);
                m_InpPort[i].Text = szText;
            }
        }

        //---------------------------------------------------------
        // MultiBit input
        //---------------------------------------------------------
        private void btnInpMultiBit_Click(object sender, System.EventArgs e)
        {
            short[] InpBitNo = new short[DIO_MAX_ACCS_BITS];
            byte[] InpBitData = new byte[DIO_MAX_ACCS_BITS];
            short i;
            //-----------------------------
            // Set bit No.
            //-----------------------------
            for (i = 0; i < InpBitNum; i++)
            {
                InpBitNo[i] = i;
            }
            //-----------------------------
            // Bit input
            //-----------------------------
            GCHandle PterBNo = GCHandle.Alloc(InpBitNo, GCHandleType.Pinned);
            GCHandle PterBData = GCHandle.Alloc(InpBitData, GCHandleType.Pinned);
            Ret = DIO.Ccapdio.DioInpMultiBit(Id, InpBitNo, InpBitNum, InpBitData);
            PterBNo.Free();
            PterBData.Free();
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInpMultiBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Display data
            //-----------------------------
            for (i = 0; i < InpBitNum; i++)
            {
                szText = string.Format("{0:X}", InpBitData[i]);
                m_InpBit[i].Text = szText;
            }
        }

        //---------------------------------------------------------
        // MultiPort output
        //---------------------------------------------------------
        private void btnOutMultiByte_Click(object sender, System.EventArgs e)
        {
            short[] OutPortNo = new short[DIO_MAX_ACCS_PORTS];
            byte[] OutPortData = new byte[DIO_MAX_ACCS_PORTS];
            short i;
            uint dwTmp;
            //-----------------------------
            // Set port No. and port data
            //-----------------------------
            for (i = 0; i < OutPortNum; i++)
            {
                OutPortNo[i] = i;
                szText = m_OutPort[i].Text;
                try
                {
                    dwTmp = uint.Parse(szText, NumberStyles.HexNumber);
                }
                catch (System.FormatException)
                {
                    dwTmp = 0;
                }
                OutPortData[i] = (byte)dwTmp;
            }
            //-----------------------------
            // Port output
            //-----------------------------
            GCHandle PterPNo = GCHandle.Alloc(OutPortNo, GCHandleType.Pinned);
            GCHandle PterPData = GCHandle.Alloc(OutPortData, GCHandleType.Pinned);
            Ret = DIO.Ccapdio.DioOutMultiByte(Id, OutPortNo, OutPortNum, OutPortData);
            PterPNo.Free();
            PterPData.Free();
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioOutMultiByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        //---------------------------------------------------------
        // MultiBit output
        //---------------------------------------------------------
        private void btnOutMultiBit_Click(object sender, System.EventArgs e)
        {
            short[] OutBitNo = new short[DIO_MAX_ACCS_BITS];
            byte[] OutBitData = new byte[DIO_MAX_ACCS_BITS];
            short i;
            uint dwTmp;
            //-----------------------------
            // Set bit No. and bit data
            //-----------------------------
            for (i = 0; i < OutBitNum; i++)
            {
                OutBitNo[i] = i;
                szText = m_OutBit[i].Text;
                try
                {
                    dwTmp = uint.Parse(szText, NumberStyles.HexNumber);
                }
                catch (System.FormatException)
                {
                    dwTmp = 0;
                }
                OutBitData[i] = (byte)dwTmp;
            }
            //-----------------------------
            // Bit output
            //-----------------------------
            GCHandle PterBNo = GCHandle.Alloc(OutBitNo, GCHandleType.Pinned);
            GCHandle PterBData = GCHandle.Alloc(OutBitData, GCHandleType.Pinned);
            Ret = DIO.Ccapdio.DioOutMultiBit(Id, OutBitNo, OutBitNum, OutBitData);
            PterBNo.Free();
            PterBData.Free();
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioOutMultiBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        //---------------------------------------------------------
        // Termination
        //---------------------------------------------------------
        private void btnDioExit_Click(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Termination
            //-----------------------------
            Ret = DIO.Ccapdio.DioExit(Id);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioExit : " + szError;
            txtReturnCode.Text = szReturnCode;
        }

        //---------------------------------------------------------
        // Exit dialog
        //---------------------------------------------------------
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Termination
            //-----------------------------
            Ret = DIO.Ccapdio.DioExit(Id);
            //-----------------------------
            // Exit dialog
            //-----------------------------
            this.Close();
        }

        private void MultiIO_Closed(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Termination
            //-----------------------------
            Ret = DIO.Ccapdio.DioExit(Id);
        }
    }
}