using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace _7K87K_do_poweron_safe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadComPort();
            btnClose.Enabled = false;
        }
        int iAddress;
        int iTotalCh;
        uint lDO_Value;

        IntPtr hPort = IntPtr.Zero;

        private void GetTextValue()
        {
            iAddress = PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)); ; //Get slot  
            lDO_Value = Convert.ToUInt32(txtValue.Text, 16); //Get DO Value
            iTotalCh = Convert.ToInt32(txtChannels.Text); //Get Total Channels
        }

        private void LoadComPort()
        {
            //Get number of COM port and  assign COM port name to list in cmbComPort.
            foreach (string s in SerialPort.GetPortNames())
            {
                if (s != "COM1") cmbCOMPort.Items.Add(s);
            }
            cmbCOMPort.SelectedIndex = 0;
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            hPort = PACNET.UART.Open(cmbCOMPort.Text + "," + cmbBaudRate.Text);
            btnOpen.Enabled = false;
            btnClose.Enabled = true;
            panel1.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            PACNET.UART.Close(hPort);

            btnOpen.Enabled = true;
            btnClose.Enabled = false;
            panel1.Enabled = false;
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteDO(hPort, iAddress, iTotalCh, lDO_Value); //Write DO value

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            GetTextValue();

            uint data = 0;

            bool iRet = PACNET.PAC_IO.ReadDO(hPort, iAddress, iTotalCh, ref data);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtRead.Text = "ReadDO(HEX): " + data.ToString("x");
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteDO(hPort, iAddress, iTotalCh, 0);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtValue.Text = "0";
        }

        private void btnCfgWDT_Click(object sender, EventArgs e)
        {
            GetTextValue();

            int WTDTimeout = 0;

            WTDTimeout = Convert.ToInt32(txtbWTDTimeout.Text);

            bool iRet = PACNET.PAC_IO.SetModuleWDTConfig(hPort, iAddress, 1, WTDTimeout, 0); //87k module without WTDOverwrite

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnGetWDT_Click(object sender, EventArgs e)
        {
            GetTextValue();

            int WTDOverwrite = 0;
            int WTDTimeout = 0;
            short ebStatus = 0;

            bool iRet = PACNET.PAC_IO.GetModuleWDTConfig(hPort, iAddress, ref ebStatus, ref WTDTimeout, ref WTDOverwrite);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtbWTDTimeout.Text = WTDTimeout.ToString();
        }

        private void btnRestWDT_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.ResetModuleWDT(hPort, iAddress);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnRefreshWDT_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.RefreshModuleWDT(hPort, iAddress);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnReadPower_Click(object sender, EventArgs e)
        {
            GetTextValue();

            uint lValue = 0;

            bool iRet = PACNET.PAC_IO.ReadModulePowerOnValueDO(hPort, iAddress, iTotalCh, ref lValue);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtRead.Text = "Read PowerOn ValueDO(HEX): " + lValue.ToString("x");
        }

        private void btnWritePower_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteModulePowerOnValueDO(hPort, iAddress, iTotalCh, lDO_Value);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnReadSafe_Click(object sender, EventArgs e)
        {
            GetTextValue();

            uint lValue = 0;

            bool iRet = PACNET.PAC_IO.ReadModuleSafeValueDO(hPort, iAddress, iTotalCh, ref lValue);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtRead.Text = "Read Safe ValueDO(HEX): " + lValue.ToString("x");
        }

        private void btnWriteSafe_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteModuleSafeValueDO(hPort, iAddress, iTotalCh, lDO_Value);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnRead_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            uint data = 0;

            bool iRet = PACNET.PAC_IO.ReadDO_MF(hPort, iAddress, iTotalCh, ref data);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtRead.Text = "ReadDO(HEX): " + data.ToString("x");
        }

        private void btnWrite_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteDO_MF(hPort, iAddress, iTotalCh, lDO_Value); //Write DO value

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnClear_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteDO_MF(hPort, iAddress, iTotalCh, 0);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtValue.Text = "0";
        }

        private void btnReadPower_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            uint lValue = 0;

            bool iRet = PACNET.PAC_IO.ReadModulePowerOnValueDO_MF(hPort, iAddress, iTotalCh, ref lValue);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtRead.Text = "Read PowerOn ValueDO(HEX): " + lValue.ToString("x");
        }

        private void btnWritePower_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteModulePowerOnValueDO_MF(hPort, iAddress, iTotalCh, lDO_Value);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnReadSafe_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            uint lValue = 0;

            bool iRet = PACNET.PAC_IO.ReadModuleSafeValueDO_MF(hPort, iAddress, iTotalCh, ref lValue);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }

            txtRead.Text = "Read Safe ValueDO(HEX): " + lValue.ToString("x");
        }

        private void btnWriteSafe_MF_Click(object sender, EventArgs e)
        {
            GetTextValue();

            bool iRet = PACNET.PAC_IO.WriteModuleSafeValueDO_MF(hPort, iAddress, iTotalCh, lDO_Value);

            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }
    }
}