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

        private void btnSend_Click(object sender, EventArgs e)
        {
            bool iRet;
            byte[] result = new byte[20];

            //Send command
            iRet = PACNET.UART.SendCmd(hPort, PACNET.MISC.AnsiString(txtSendValue.Text), result);
            if (iRet == false)
            {
                MessageBox.Show("send error");
            }
            else
            {
                //Show Response
                txtResponseValue.Text = PACNET.MISC.WideString(result);
            }
        }

       
    }
}