//======================================================================
        //函数名称:rdo_uart_CheckedChanged
        //函数返回:无
        //参数说明:无
        //功能概要:点击“串口更新”单选按钮,显示串口更新页面并使用串口方式连接终端
        //======================================================================
        private void rdo_uart_CheckedChanged(object sender, EventArgs e)
        {
            //(1)变量申明
            string com;

            //(2)若串口更新未选中,直接退出
            if (this.rdo_uart.Checked == false)
            {
                return;
            }

            //(3)串口更新页面显现
            this.pnl_uart.Visible     = true;
            fmain.lbl_mainstatus.Text = "运行状态:选择串口更新";
            //(4)寻找并连接终端
            if (uart != null && uart.IsOpen)
            {
                uart.close();
            }
            pcNode = new PCNode();//pc节点初始化
            com    = pcNode.findPCNode();
            uart   = pcNode.PCNode_Uart;
            this.lbl_uartstate.Text = com;
            //(5)绑定DataReceived事件(即串口接收中断处理函数)
            if (uart != null)
            {
                //(5.1)连接串口成功
                uartPort = uart.port;
                uartPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.uartPort_recvData); //先移除委托函数,防止多次触发
                uartPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.uartPort_recvData);
                uart.setReceInt(500);                                                                                //相当于关闭串口中断

                this.btn_autoUpdate.Enabled = true;                                                                  //允许串口更新操作
                //修改主页面提示信息
                this.lbl_uartstate.Text     = "成功连接" + com;
                fmain.lbl_mainstatus.Text   = "运行状态:找到" + com;
                fmain.lbl_protocal.Text     = "协议:串口";
                fmain.lbl_protocalinfo.Text = "协议信息:端口" + com + ",波特率115200";
            }
            else
            {
                //(5.2)连接串口失败
                this.btn_autoUpdate.Enabled = false;  //禁止串口更新操作
                //修改主页面提示信息
                fmain.lbl_mainstatus.Text   = "运行状态:未成功连接终端," + com;
                fmain.lbl_protocal.Text     = "协议:";
                fmain.lbl_protocalinfo.Text = "协议信息:";
            }
        }
        //======================================================================
        //函数名称:btn_uartCheck_Click
        //函数返回:无
        //参数说明:无
        //功能概要:“重新连接”按钮点击事件,重新连接终端
        //======================================================================
        private void btn_uartCheck_Click(object sender, EventArgs e)
        {
            //(1)变量申明
            string com;

            //(2)提示重新连接串口
            fmain.lbl_mainstatus.Text = "运行状态:单击“重新连接”按钮...";
            //(3)重新遍历串口,寻找终端
            if (uart != null && uart.IsOpen)
            {
                uart.close();
            }
            pcNode = new PCNode();//pc节点初始化
            com    = pcNode.findPCNode();
            uart   = pcNode.PCNode_Uart;
            this.lbl_uartstate.Text = com;
            //(4)绑定DataReceived事件(即串口接收中断处理函数)
            if (uart != null)
            {
                //(4.1)连接串口成功
                uartPort = uart.port;
                uartPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.uartPort_recvData); //先移除委托函数,防止多次触发
                uartPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.uartPort_recvData);
                uart.setReceInt(500);                                                                                //相当于关闭串口中断

                this.btn_autoUpdate.Enabled = true;                                                                  //允许串口更新操作
                //修改主页面提示信息
                this.lbl_uartstate.Text     = "成功连接" + com;
                fmain.lbl_mainstatus.Text   = "运行状态:找到" + com;
                fmain.lbl_protocal.Text     = "协议:串口";
                fmain.lbl_protocalinfo.Text = "协议信息:端口" + com + ",波特率115200";
            }
            else
            {
                //(4.2)连接串口失败
                this.btn_autoUpdate.Enabled = false;  //禁止串口更新操作
                //修改主页面提示信息
                fmain.lbl_mainstatus.Text   = "运行状态:未成功找到串口," + com;
                fmain.lbl_protocal.Text     = "协议:";
                fmain.lbl_protocalinfo.Text = "协议信息:";
            }
        }
Example #3
0
        //======================================================================
        //函数名称:sendRecv
        //函数返回:true:数据帧发送成功并接收到返回值;
        //          false:数据帧发送后未接收到返回值或返回值解析失败
        //参数说明:sendBytes:待发送的数据;cnt:重复发送次数;
        //          time:发送后等待接收返回值的时间
        //功能概要:串口发送数据,并等待接收返回值
        //======================================================================
        private bool sendRecv(byte[] sendBytes, int cnt, int time)
        {
            //(1)变量声明
            byte[] recvData;
            bool   rv;
            bool   sendFlag;
            string com;

            rv = true;
            //(2)更新状态栏,显示当前操作
            txtShow("\r\n正在发送...\r\n");
            //(3)发送数据帧到下位机
            uart.recvData();//在发送数据帧之前清空接收缓冲区
            sendFlag = pcNode.PCNodeSendReturn(sendBytes, out recvData, cnt, time);
            //(4)若发送失败,尝试自动重连一次,并且重发
            if (sendFlag == false)
            {
                fmain.lbl_status.Text = "运行状态:数据发送失败,重新连接终端并重发...";
                txtShow("数据发送失败,重新连接终端并重发...\r\n");
                //重连串口
                if (uart != null && uart.IsOpen)
                {
                    uart.close();
                }
                com  = pcNode.findPCNode();
                uart = pcNode.PCNode_Uart;
                fmain.lbl_status.Text = "运行状态:找到" + com;
                lbl_uart_state.Text   = com;
                //重新发送数据
                sendFlag = pcNode.PCNodeSendReturn(sendBytes, out recvData, cnt, time);
            }
            //(5)未接收到数据则更新状态栏
            if (recvData == null || recvData.Length <= 0)
            {
                fmain.lbl_status.Text = "运行状态:无数据返回";
                txtShow("无数据返回\r\n");

                rv = false;
                goto sendRecv_EXIT;
            }
            //(6)成功接收数据,提示并进行数据解析
            txtShow("有数据返回,数据解析中...\r\n");

            if (System.Text.Encoding.Default.GetString(recvData) == "Start Update")
            {
                fmain.lbl_status.Text = "运行状态:程序进入bootloader,开始更新";
                txtShow("程序进入bootloader,开始更新\r\n");

                rv = true;
                goto sendRecv_EXIT;
            }
            else if (update != null && update.updateRecv(recvData) != 0)  //数据解析失败
            {
                fmain.lbl_status.Text = "运行状态:数据解析失败";
                txtShow("数据解析失败\r\n");

                rv = false;
                goto sendRecv_EXIT;
            }
            //(7)至此,数据接收成功
            fmain.lbl_status.Text = "运行状态:数据解析成功,准备下一帧";
            txtShow("数据解析成功,准备下一帧\r\n");

sendRecv_EXIT:
            return(rv);
        }
        /// ------------------------------------------------------------------------------
        /// <summary>
        /// 功    能:寻找PCNode。
        /// 形    参:无
        /// <returns>无</returns>
        /// -----------------------------------------------------------------------------
        public string findPCNode()
        {
            int    i, j;
            string s1;
            string recvstr;

            int[] uartNoArray;   //
            System.IO.Ports.SerialPort uartport;
            //1.临时变量

            bool result = false;     //

            byte[] recvData = null;  //串口接收返回的信息


            byte[] shakeframe = { (byte)'s', (byte)'h', (byte)'a', (byte)'k', (byte)'e' };

            byte[] frame = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            //初始化内部调用函数临时变量
            s1 = string.Empty;    //初始化临时字符串(置空)

            //创建_port1对象
            uartport = new System.IO.Ports.SerialPort();
            //查询所有串口,串口数在uartNoArray[0]中

            uartNoArray = Uart.findCOM();
            //没有找到串口情况
            if (uartNoArray[0] == 0)
            {
                return(s1 = "没有找到USB串口");
            }

            //有找到串口情况,遍历搜寻串口
            for (i = uartNoArray[0]; i >= 1; i--)
            {
                string comName = "COM" + uartNoArray[i].ToString(); //串口名称
                uart1 = new Uart(uartport, comName, 115200);        //初始化串口工具类
                //2.打开串口
                result = uart1.open();
                if (!result)
                {
                    continue;
                }
                uart1.setReceInt(200);                                  //设置触发DataRecieved事件的字节数(目的是关闭该事件)
                uart1.sendData(frame, frame.Length);                    //预发送数据,防止下位机组帧出错
                System.Threading.Thread.Sleep(100);                     //线程休眠0.1s
                result = uart1.sendData(shakeframe, shakeframe.Length); //发送握手数据
                if (!result)
                {
                    uart1.close(); continue;
                }
                System.Threading.Thread.Sleep(100);   //线程休眠0.1s
                recvData = uart1.recvData();          //接收数据

                //if (recvData == null || recvData.Length == 0)
                //   { uart1.close(); continue; }

                //将握手成功,数组转换成字符串,返回
                recvstr = System.Text.Encoding.Default.GetString(recvData);
                if (!recvstr.Contains("shake:"))
                {
                    uart1.close(); continue;
                }
                j  = recvstr.IndexOf(":");
                s1 = recvstr.Substring(j + 1, recvstr.Length - j - 1);
                //s1 = comName;
                break;
            }
            if (i == 0)
            {
                s1    = "有USB串口,但未连上终端";
                uart1 = null;
            }

            return(s1);
        }
 /// ------------------------------------------------------------------------------
 /// <summary>
 /// 名    称:(0)Find_PC_Node:构造函数
 /// 功    能: 创建KW01寻节点实例
 /// 内部参数: uart1:  串口工具类
 /// </summary>
 /// <param name="uart1">串口工具类</param>
 /// <returns>true_串口初始化成功 false_初始化失败</returns>
 /// ------------------------------------------------------------------------------
 public PCNode()
 {
     uart1 = null;
 }