using System.Globalization;
using System.Text;

namespace SimpleIO
{
    public partial class Form1 : Form
    {
        // Additional data
        short Id;                                               // Device ID
        int Ret;                                                // Return value
        StringBuilder szError = new StringBuilder("", 256);     // Error string
        string szReturnCode = new string(' ', 256);             // The string for error
        string szText = new string(' ', 100);

        public Form1()
        {
            InitializeComponent();
        }

        //---------------------------------------------------------
        // Initialize dialog
        //---------------------------------------------------------
        private void SimpleIO_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
            //-----------------------------------
            txtInpPortNo.Text = "0";
            txtInpBitNo.Text = "0";
            txtOutPortNo.Text = "0";
            txtOutBitNo.Text = "0";
            txtOutPortData.Text = "55";
            txtOutBitData.Text = "1";
            txtReturnCode.Text = "";
        }

        //---------------------------------------------------------
        // Initialization
        //---------------------------------------------------------
        private void btnDioInit_Click(object sender, System.EventArgs e)
        {
            string szDeviceName = new string(' ', 100);
            //-----------------------------
            // 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;
        }

        //---------------------------------------------------------
        // 1 port input
        //---------------------------------------------------------
        private void btnInpByte_Click(object sender, System.EventArgs e)
        {
            short InpPortNo;
            byte InpPortData;
            InpPortData = 0;
            //-----------------------------
            // Get port No. from screen
            //-----------------------------
            szText = txtInpPortNo.Text;
            try
            {
                InpPortNo = Convert.ToInt16(szText);
            }
            catch (System.FormatException)
            {
                InpPortNo = 0;
            }
            //-----------------------------
            // Port input
            //-----------------------------
            Ret = DIO.Ccapdio.DioInpByte(Id, InpPortNo, ref InpPortData);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInpByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Display data
            //-----------------------------
            szText = string.Format("{0:X2}", InpPortData);
            txtInpPortData.Text = szText;
        }

        //---------------------------------------------------------
        // 1 bit input
        //---------------------------------------------------------
        private void btnInpBit_Click(object sender, System.EventArgs e)
        {
            short InpBitNo;
            byte InpBitData;
            InpBitData = 0;
            //-----------------------------
            // Get bit No. from screen
            //-----------------------------
            szText = txtInpBitNo.Text;
            try
            {
                InpBitNo = Convert.ToInt16(szText);
            }
            catch (System.FormatException)
            {
                InpBitNo = 0;
            }
            //-----------------------------
            // Bit input
            //-----------------------------
            Ret = DIO.Ccapdio.DioInpBit(Id, InpBitNo, ref InpBitData);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioInpBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Display data
            //-----------------------------
            szText = string.Format("{0:X}", InpBitData);
            txtInpBitData.Text = szText;
        }

        //---------------------------------------------------------
        // 1 port output
        //---------------------------------------------------------
        private void btnOutByte_Click(object sender, System.EventArgs e)
        {
            short OutPortNo;
            byte OutPortData;
            uint dwTmp;
            //-----------------------------
            // Get the port No. and the output data from screen
            //-----------------------------
            szText = txtOutPortNo.Text;
            try
            {
                OutPortNo = Convert.ToInt16(szText);
            }
            catch (System.FormatException)
            {
                OutPortNo = 0;
            }
            szText = txtOutPortData.Text;
            try
            {
                dwTmp = uint.Parse(szText, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                dwTmp = 0;
            }
            OutPortData = (byte)dwTmp;
            //-----------------------------
            // Port output
            //-----------------------------
            Ret = DIO.Ccapdio.DioOutByte(Id, OutPortNo, OutPortData);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioOutByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        //---------------------------------------------------------
        // 1 bit output
        //---------------------------------------------------------
        private void btnOutBit_Click(object sender, System.EventArgs e)
        {
            short OutBitNo;
            byte OutBitData;
            uint dwTmp;
            //-----------------------------
            // Get the bit No. and the output data from screen
            //-----------------------------
            szText = txtOutBitNo.Text;
            try
            {
                OutBitNo = Convert.ToInt16(szText);
            }
            catch (System.FormatException)
            {
                OutBitNo = 0;
            }
            szText = txtOutBitData.Text;
            try
            {
                dwTmp = uint.Parse(szText, NumberStyles.HexNumber);
            }
            catch (System.FormatException)
            {
                dwTmp = 0;
            }
            OutBitData = (byte)dwTmp;
            //-----------------------------
            // Bit output
            //-----------------------------
            Ret = DIO.Ccapdio.DioOutBit(Id, OutBitNo, OutBitData);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioOutBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
        }

        //---------------------------------------------------------
        // 1 port EchoBack
        //---------------------------------------------------------
        private void btnEchoBackByte_Click(object sender, System.EventArgs e)
        {
            short OutPortNo;
            byte OutPortData;
            OutPortData = 0;
            //-----------------------------
            // Get port No. from screen
            //-----------------------------
            szText = txtOutPortNo.Text;
            try
            {
                OutPortNo = Convert.ToInt16(szText);
            }
            catch (System.FormatException)
            {
                OutPortNo = 0;
            }
            //-----------------------------
            // Port EchoBack
            //-----------------------------
            Ret = DIO.Ccapdio.DioEchoBackByte(Id, OutPortNo, ref OutPortData);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioEchoBackByte : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Display data
            //-----------------------------
            szText = string.Format("{0:X2}", OutPortData);
            txtOutPortData.Text = szText;
        }

        //---------------------------------------------------------
        // 1 Bit EchoBack
        //---------------------------------------------------------
        private void btnEchoBackBit_Click(object sender, System.EventArgs e)
        {
            short OutBitNo;
            byte OutBitData;
            OutBitData = 0;
            //-----------------------------
            // Get bit No. from screen
            //-----------------------------
            szText = txtOutBitNo.Text;
            try
            {
                OutBitNo = Convert.ToInt16(szText);
            }
            catch (System.FormatException)
            {
                OutBitNo = 0;
            }
            //-----------------------------
            // Bit EchoBack
            //-----------------------------
            Ret = DIO.Ccapdio.DioEchoBackBit(Id, OutBitNo, ref OutBitData);
            //-----------------------------
            // Process error
            //-----------------------------
            DIO.Ccapdio.DioGetErrorString(Ret, szError);
            szReturnCode = "DioEchoBackBit : " + szError;
            txtReturnCode.Text = szReturnCode;
            if (Ret != DIO.Ccapdio.DIO_ERR_SUCCESS)
            {
                return;
            }
            //-----------------------------
            // Display data
            //-----------------------------
            szText = string.Format("{0:X}", OutBitData);
            txtOutBitData.Text = szText;
        }

        //---------------------------------------------------------
        // 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 SimpleIO_Closed(object sender, System.EventArgs e)
        {
            //-----------------------------
            // Termination
            //-----------------------------
            Ret = DIO.Ccapdio.DioExit(Id);
        }
    }
}