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
{
    public partial class Form1 : Form
    {
        IntPtr hPort;
        public Form1()
        {
            InitializeComponent();
            LoadComPort();
            btnCloseCom.Enabled = false;
        }

        private void btnOpenCom_Click(object sender, EventArgs e)
        {
            //Open 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
            {
                btnCloseCom.Enabled = true;
                btnOpenCom.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 btnCloseCom_Click(object sender, EventArgs e)
        {
            //Close port
            PACNET.UART.Close(hPort);
            btnCloseCom.Enabled = false;
            btnOpenCom.Enabled = true;
        }

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

            //Write AO data
            iRet = PACNET.PAC_IO.WriteAO(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt32(txtAddress.Text)), Convert.ToInt32(txtWriteChannel.Text), Convert.ToInt32(txtChannel.Text), Convert.ToSingle(txtWriteData.Text));

            if (iRet == false)
                MessageBox.Show("Write data error");
        }
    }
}