using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using System.Management;

namespace SerialPort
{
    ///-----------------------------------------------------------------------------------
    ///FrmMain :KW01-A1工程的窗体                                                          
    ///目    的:
    ///日    期:                                             
    ///编 程 者:
    ///-----------------------------------------------------------------------------------
    public partial class FrmMain : Form
    {
        //1. 窗体类的临时变量区
        private System.IO.Ports.SerialPort UARTport;   //声明系统工具-串口对象的变量声明
        private Uart uart1;                          //声明一个自定义的Uart类型（串口操作）的变量
        private PCNode pcNode1;                      //声明一个自定义的PCNode类型的变量


        //2. 构造函数（启动窗体最先执行程序）
        public FrmMain()
        {
            //系统生成的构造函数（初始化窗体的元素）
            InitializeComponent();
            //禁止检测跨线程调用异常(
            CheckForIllegalCrossThreadCalls = false;
            //初始化窗体类临时变量
            UARTport = null;          //初始化“系统工具-串口对象”
            uart1 = null;             //初始化“Uart类型（串口操作）对象”
            pcNode1 = new PCNode();   //
        }

        //3. 加载窗体事件程序（在构造函数之后执行的程序）
        private void FrmMain_Load(object sender, EventArgs e)
        {
           
            string s1;
            this.tb_statue.Text = "运行状态：FrmMain_Load开始,调用PCNode类内部函数，寻找PCNode...";
            this.tb_statue.Refresh();

            if (uart1 != null && uart1.IsOpen) uart1.close();
            s1 = pcNode1.findPCNode();           //调用PCNode类内部函数，寻找PCNode
            uart1 = pcNode1.PCNode_Uart1;

            this.tb_statue.Text = "运行状态：找到" + s1;
            this.tb_statue.Refresh();

            lbl_PC_Node_State.Text = s1;

            //保留编程参考：绑定DataReceived事件(即串口接收中断处理函数）
            if (uart1 != null)
            {
                UARTport = uart1.port;
                UARTport.DataReceived += new System.IO.Ports. //添加串口对象变量的接收事件的响应函数
                         SerialDataReceivedEventHandler(this.UARTport_recvData);
            }
            //uart1.setReceInt(2);           //设置触发DataRecieved事件的字节数（目的是关闭该事件）
            //uart1.sendData(B1, B1.Length); //发送握手数据

        }

        //========================以下是本窗体的各个事件响应函数==========================

        //------------------------单击“检测PC节点”按钮----------------------------------
        private void btnCheckPcNode_Click(object sender, EventArgs e)
        {

            //if (_port1.IsOpen) 

            this.tb_statue.Text = "运行状态：" + "单击“检测PC节点”按钮...";
            this.tb_statue.Refresh();

            FrmMain_Load(sender, e);

            this.tb_statue.Text = "运行状态：" + "单击“检测PC节点”按钮...退出";
        }

        //--------------------------单击窗口关闭“X”按钮---------------------------------
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (uart1 == null) return;
            //如果串口未关闭，则关闭
            if (uart1.IsOpen) uart1.close();
        }


        //-------------------------------串口接收事件的响应函数---------------------------
        private void UARTport_recvData(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            int i;
            //int tmp, brt;
            string s1 = string.Empty;

            Thread.Sleep(100);
            PublicVar.g_ReceiveByteArray = uart1.recvData();    //调用串口接收函数
            if (PublicVar.g_ReceiveByteArray == null) return;
            //if (PublicVar.g_ReceiveByteArray.Length > 0)
            //{
            //    this.Txt_recv2.Text = "Interrupt--- " + System.Text.Encoding.Default.GetString(PublicVar.g_ReceiveByteArray);
            //    if (PublicVar.g_ReceiveByteArray[2] == 'M') 
            //    {
            //        tmp = (int)(PublicVar.g_ReceiveByteArray[4] * 255 + PublicVar.g_ReceiveByteArray[3]);
            //        brt = (int)(PublicVar.g_ReceiveByteArray[6] * 255 + PublicVar.g_ReceiveByteArray[5]);

            //    }
            //}
            for (i = 0; i < PublicVar.g_ReceiveByteArray.Length; i++)
            {
                this.Txt_recv2.Text += "-" + PublicVar.g_ReceiveByteArray[i].ToString("D3") + " ";
            }

        }

        private void btnVerifyRF_Click(object sender, EventArgs e)
        {

            // 1. 临时变量声明
            byte n;             //测试数据字节数
            int i;
            //byte len;           //[命令+数据]字节数（总帧长度-1B帧头 - 1B len - 1B帧尾）
            string s1;
            byte hw_Addr;

            //this.Txt_recv1.Text = "";
            this.Txt_recv2.Text = "";

            //this.Txt_recv1.Refresh();
            this.Txt_recv2.Refresh();
            s1 = "运行状态：" + "单击“验证RF收发”按钮...";
            this.tb_statue.Text = s1;
            this.tb_statue.Refresh();

            btnVerifyRF.Enabled = false;

            // 2. 临时变量赋值
            n = Convert.ToByte(tbTxtDataLen.Text);
            hw_Addr = Convert.ToByte(hwAddr.Text);

            byte[] writeDataArray = System.Text.Encoding.Default.GetBytes(tbWriteData2.Text); //String转化为Byte[]

            if (n > 50||n<1)
            {
                this.tb_statue.Text = "发送字节数不合适，合理范围是1~50";
                return;
            }

            //len = (byte)(n + 1);
            //len = (byte)(n + 1);    //增加一个是否检测能量场的字节

            byte[] SendByteArray = new byte[n + 9];

            // 组帧
            try
            {
                SendByteArray[0] = (byte)'P';      //帧头

                SendByteArray[1] = (byte)(n + 6);            //帧长

                SendByteArray[2] = hw_Addr;        //被访问的硬件过滤地址

                SendByteArray[3] = 0;        //软件地址网段号高字节
                SendByteArray[4] = 0xFF;     //软件地址网段号低字节
                SendByteArray[5] = 0;        //软件地址网内地址高字节
                SendByteArray[6] = 0xFF;     //软件地址网内地址低字节
                SendByteArray[7] = 0x02;     //RF收发测试命令字

                for (i = 8; i <= 7 + n; i++)
                {
                    SendByteArray[i] = writeDataArray[i - 8];
                }


                SendByteArray[n + 8] = (byte)'C';      //帧尾

                //
                //发送、接收、显示
                //sendRecv(SendByteArray,1,5500);
                uart1.setReceInt(100);   //测试串口接收中断使用
                //uart1.sendData(SendByteArray, SendByteArray.Length);  // 通过串口发送数据
                sendRecv(SendByteArray, 2, 300);

                //System.Threading.Thread.Sleep(time);

                s1 = s1 + "已经发送,等待接收中断...收到TargetNode回送信息！";
                this.tb_statue.Text = s1;
                this.tb_statue.Refresh();


                btnVerifyRF.Enabled = true;
            }
            catch
            {
                //this.Txt_recv1.Text = "有误:可能没串口！";
                this.tb_statue.Text = "运行状态：" + "单击“验证RF收发”按钮...没有找到TargetNode！";
                this.tb_statue.Refresh();
                btnVerifyRF.Enabled = true;
            } 
        }

        //-------------------------
        private void sendRecv(byte[] SendByteArray, int cnt, int time)
        {
            byte[] recvData;
            byte[] data;//用于保存有效数据的字符数组
            String str = String.Empty;
            int i;
            this.tb_statue.Text += "正在发送...";
            this.tb_statue.Refresh();
            recvData = pcNode1.PCNodeSendReturn(SendByteArray, cnt, time);
            data = new byte[recvData.Length - 9];//长度为接收字符数组中的有效字符数
            if (recvData.Length <= 0)
            {
                this.tb_statue.Text += "无数据返回";
                this.tb_statue.Refresh();
            }
            else
            {
                this.tb_statue.Text += "有数据返回";
                this.tb_statue.Refresh();
                this.Txt_recv2.Text = string.Empty;
                for (i = 8; i < recvData.Length-1; i++)
                    // this.Txt_recv.Text += "-" + recvData[i].ToString("D3").ToUpper() + " ";
                  //this.Txt_recv2.Text += recvData[i].ToString("X2").ToUpper() + " ";
                    data[i - 8] = recvData[i];//有效数据字符数组
                //this.Txt_recv2.Text = System.Text.Encoding.Default.GetString(recvData);
                str = Encoding.Default.GetString(data);//转换为字符串
                this.Txt_recv2.Text = str;  
            }
        }


        //---------------------------单击“清空显示框”按钮-------------------------------
        private void btnClear1_Click(object sender, EventArgs e)
        {
            this.tb_statue.Text = "运行状态：" + "单击“清空显示框”按钮。";
            this.tb_statue.Refresh();

            //this.Txt_recv1.Text = "";
            this.Txt_recv2.Text = "";
        }

        private void tbWriteData2_TextChanged(object sender, EventArgs e)
        {
            tbTxtDataLen.Text = tbWriteData2.Text.Length.ToString();
            if(tbWriteData2.Text.Length<51)
                btnVerifyRF.Enabled = true;
        }

  
    }
}
