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_cnt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            LoadComPort();
            btnCloseCom.Enabled = false;
        }
        IntPtr hPort;
        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 btnCloseCom_Click(object sender, EventArgs e)
        {
            //Close com port
            PACNET.UART.Close(hPort);
            btnOpenCom.Enabled = true;
            btnCloseCom.Enabled = false;
        }

        private void btnOpenCom_Click(object sender, EventArgs e)
        {
            //Open com port
            this.hPort = PACNET.UART.Open(cmbComPort.Text + "," + txtBaurdRate.Text);

            if (hPort == (IntPtr)(-1))
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
            else
            {
                btnOpenCom.Enabled = false;
                btnCloseCom.Enabled = true;
            }
        }

        private void btnReadCount_Click(object sender, EventArgs e)
        {
            int Address;
            uint IDValue = 0;
            bool iRet;

            //Get slot data
            Address = Convert.ToInt16(txtAddress.Text);

            //Read data
            iRet = PACNET.PAC_IO.ReadCNT(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Address), Convert.ToInt16(txtChannel.Text), ref IDValue);

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

        private void btnClearCount_Click(object sender, EventArgs e)
        {
            int Address;
            bool iRet;

            //Get slot data
            Address = Convert.ToInt16(txtAddress.Text);

            //Clear data
            iRet = PACNET.PAC_IO.ClearCNT(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Address), Convert.ToInt16(txtChannel.Text));

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

        private void btnReadCountOverflow_Click(object sender, EventArgs e)
        {
            int Address;
            int IDValue = 0;
            bool iRet;

            //Get slot data
            Address = Convert.ToInt16(txtAddress.Text);

            //Read data
            iRet = PACNET.PAC_IO.ReadCNTOverflow(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Address), Convert.ToInt16(txtChannel.Text), ref IDValue);

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