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;                 // ̃Tv̍őANZX|[g
        private const short DIO_MAX_ACCS_BITS  = 8;                 // ̃Tv̍őANZXrbg
        // ǉf[^
        short               Id;                                     // foCXID
        int                 Ret;                                    // ߂l
        short               InpPortNum;                             // ̓|[g
        short               OutPortNum;                             // o̓|[g
        short               InpBitNum;                              // ̓rbg
        short               OutBitNum;                              // o̓rbg
        StringBuilder       szError = new StringBuilder("", 256);   // G[
        string              szReturnCode = new string(' ', 256);    // \G[
        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();
        }

        //---------------------------------------------------------
        // _CAO
        //---------------------------------------------------------
        private void MultiIO_Load(object sender, System.EventArgs e)
        {
            //-----------------------------------
            // foCX̌o
            //-----------------------------------
            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;
            //-----------------------------------
            // ʃACȅ
            //-----------------------------------
            // |[g
            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;
            // rbg
            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;
            // |[go
            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;
            // rbgo
            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;
            // ߂l
            txtReturnCode.Text = "";
        }

        //---------------------------------------------------------
        // 
        //---------------------------------------------------------
        private void btnDioInit_Click(object sender, System.EventArgs e)
        {
            string szDeviceName = new string(' ', 100);
            short  i;
            //-----------------------------
            // ʂfoCX擾
            //-----------------------------
            szDeviceName = cboDeviceName.Text;
            //-----------------------------
            // sȂAID擾
            //-----------------------------
            Ret = DIO.Ccapdio.DioInit(szDeviceName, ref Id);
            //-----------------------------
            // G[
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // S|[g̎擾
            //-----------------------------
            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;
            }
            // ̃Tv͍ő8|[g܂
            if (InpPortNum > DIO_MAX_ACCS_PORTS)
            {
                InpPortNum = DIO_MAX_ACCS_PORTS;
            }
            if (OutPortNum > DIO_MAX_ACCS_PORTS)
            {
                OutPortNum = DIO_MAX_ACCS_PORTS;
            }
            //-----------------------------
            // Srbg̎擾
            //-----------------------------
            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;
            }
            // ̃Tv͍ő8rbg܂
            if (InpBitNum > DIO_MAX_ACCS_BITS)
            {
                InpBitNum = DIO_MAX_ACCS_BITS;
            }
            if (OutBitNum > DIO_MAX_ACCS_BITS)
            {
                OutBitNum = DIO_MAX_ACCS_BITS;
            }
            //-----------------------------
            // ʂ}XN
            //-----------------------------
            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;
                }
            }
        }

        //---------------------------------------------------------
        // |[g
        //---------------------------------------------------------
        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;
            //-----------------------------
            // |[gԍZbg
            //-----------------------------
            for (i = 0; i < InpPortNum; i++)
            {
                InpPortNo[i] = i;
            }
            //-----------------------------
            // |[g
            //-----------------------------
            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();
            //-----------------------------
            // G[
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInpMultiByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // f[^\
            //-----------------------------
            for (i = 0; i < InpPortNum; i++)
            {
                szText = string.Format("{0:X2}", InpPortData[i]);
                m_InpPort[i].Text = szText;
            }
        }

        //---------------------------------------------------------
        // rbg
        //---------------------------------------------------------
        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;
            //-----------------------------
            // rbgԍZbg
            //-----------------------------
            for (i = 0; i < InpBitNum; i++)
            {
                InpBitNo[i] = i;
            }
            //-----------------------------
            // rbg
            //-----------------------------
            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();
            //-----------------------------
            // G[
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInpMultiBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // f[^\
            //-----------------------------
            for (i = 0; i < InpBitNum; i++)
            {
                szText = string.Format("{0:X}", InpBitData[i]);
                m_InpBit[i].Text = szText;
            }
        }

        //---------------------------------------------------------
        // |[go
        //---------------------------------------------------------
        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;
            //-----------------------------
            // |[gԍA|[gf[^Zbg
            //-----------------------------
            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;
            }
            //-----------------------------
            // |[go
            //-----------------------------
            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();
            //-----------------------------
            // G[
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioOutMultiByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        //---------------------------------------------------------
        // rbgo
        //---------------------------------------------------------
        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;
            //-----------------------------
            // rbgԍArbgf[^Zbg
            //-----------------------------
            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;
            }
            //-----------------------------
            // rbgo
            //-----------------------------
            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();
            //-----------------------------
            // G[
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioOutMultiBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

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

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

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