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_dio
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            btnClosePort.Enabled = false;
            LoadComPort();
        }
        IntPtr hPort;
        private void btnOpenPort_Click(object sender, EventArgs e)
        {
            //Open com port
            hPort = PACNET.UART.Open(cmbComPort.Text + "," + txtBaudRate.Text);
            if (hPort == (IntPtr)(-1))
                MessageBox.Show("Open port is error,error code " + PACNET.ErrHandling.GetLastError());
            else
            {
                btnClosePort.Enabled = true;
                btnOpenPort.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;
        }

        private void btnClosePort_Click(object sender, EventArgs e)
        {
            //Close com port
            PACNET.UART.Close(hPort);
            btnOpenPort.Enabled = true;
            btnClosePort.Enabled = false;
        }

        private void btnReadDI_Click(object sender, EventArgs e)
        {
            uint DiValue = 0;
            uint DoValue = 0;
            bool iRet;

            //Read DI data
            iRet = PACNET.PAC_IO.ReadDIO(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)), Convert.ToInt32(txtDIChannel.Text), Convert.ToInt32(txtDOChannel.Text), ref DiValue, ref DoValue);
            if (iRet == false)
                MessageBox.Show("read di error, error code " + PACNET.ErrHandling.GetLastError());
            else
                txtDIValue.Text = DiValue.ToString();

        }

        private void btnWriteDO_Click(object sender, EventArgs e)
        {
            bool iRet;

            //Write DO data
            iRet = PACNET.PAC_IO.WriteDO(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)), Convert.ToInt32(txtDOChannel.Text), Convert.ToUInt32(txtDOValue.Text, 16));
            if (iRet == false)
                MessageBox.Show("write do error, error code " + PACNET.ErrHandling.GetLastError());
        }

        private void btnReadDI_MF_Click(object sender, EventArgs e)
        {
            uint DiValue = 0;
            uint DoValue = 0;
            bool iRet;

            //Read DI data
            iRet = PACNET.PAC_IO.ReadDIO_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)), Convert.ToInt32(txtDIChannel.Text), Convert.ToInt32(txtDOChannel.Text), ref DiValue, ref DoValue);
            if (!iRet)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
            else
                txtDIValue.Text = DiValue.ToString("X");
        }

        private void btnWriteDO_MF_Click(object sender, EventArgs e)
        {
            bool iRet;

            //Write DO data
            iRet = PACNET.PAC_IO.WriteDO_MF(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)), Convert.ToInt32(txtDOChannel.Text), Convert.ToUInt32(txtDOValue.Text, 16));

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