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_ao_poweron_safe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadComPort();
            btnCloseCom.Enabled = false;
        }
        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;
        }
        int iAddress;
        int iCurrentCh;
        int iTotalCh;
        float fValue;

        IntPtr hPort = IntPtr.Zero;
        private void GetTxtValue()
        {
            iAddress = PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)); ; //Get slot  
            iCurrentCh = Convert.ToInt32(txtCurrentCH.Text);
            iTotalCh = Convert.ToInt32(txtTotalCH.Text);
            fValue = Convert.ToSingle(txtValue.Text);
        }

        private void btnOpenCom_Click(object sender, EventArgs e)
        {
            hPort = PACNET.UART.Open(cmbCOMPort.Text + "," + cmbBaudRate.Text);
            btnOpenCom.Enabled = false;
            btnCloseCom.Enabled = true;
            panel1.Enabled = true;
        }

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

            btnOpenCom.Enabled = true;
            btnCloseCom.Enabled = false;
            panel1.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmbCOMPort.SelectedIndex = 3;
            cmbBaudRate.SelectedIndex = 1;
            panel1.Enabled = false;
            btnCloseCom.Enabled = false;
        }

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

            //Write AO value
            bool iRet = PACNET.PAC_IO.WriteAO(hPort, iAddress, iCurrentCh, iTotalCh, fValue);

            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)
        {
            GetTxtValue();

            float data = 0;

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

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

            txtRead.Text = "Read AO: " + data.ToString();
        }

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

            bool iRet = PACNET.PAC_IO.WriteAO(hPort, iAddress, iCurrentCh, iTotalCh, 0F);

            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)
        {
            GetTxtValue();

            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)
        {
            GetTxtValue();

            //pac_GetModuleWDTConfig
            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)
        {
            GetTxtValue();

            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)
        {
            GetTxtValue();

            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)
        {
            GetTxtValue();
            float data = 0;

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

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

            txtRead.Text = "Read PowerOn ValueAO: " + data.ToString();
        }

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

            bool iRet = PACNET.PAC_IO.WriteModulePowerOnValueAO(hPort, iAddress, iCurrentCh, iTotalCh, fValue);

            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)
        {
            GetTxtValue();
            float data = 0;

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

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

            txtRead.Text = "Read Safe ValueAO: " + data.ToString();
        }

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

            bool iRet = PACNET.PAC_IO.WriteModuleSafeValueAO(hPort, iAddress, iCurrentCh, iTotalCh, fValue);

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