Пример #1
0
        //======================================================================
        //函数名称:frm_uart_Load
        //函数返回:无
        //参数说明:无
        //功能概要:窗体加载事件,窗体加载时自动调用
        //======================================================================
        private void frm_uart_Load(object sender, EventArgs e)
        {
            //(1)变量申明
            string com;

            //(1)设置允许跨线程操作UI控件
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            //(2)获得主页面窗体
            fmain = (frm_main)this.MdiParent;
            //(3)更新状态提示信息
            fmain.lbl_status.Text = "运行状态:frm_uart_Load函数开始,寻找终端节点...";
            //(4)调用pcNode类中的函数,寻找下位机,并更新设备连接状态信息
            if (uart != null && uart.IsOpen)
            {
                uart.close();
            }
            com  = pcNode.findPCNode();
            uart = pcNode.PCNode_Uart;
            fmain.lbl_status.Text = "运行状态:找到" + com;
            lbl_uart_state.Text   = com;
            //(5)绑定DataReceived事件(即串口接收中断处理函数)
            if (uart != null)
            {
                uartPort = uart.port;
                uartPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.uartPort_recvData);
                uart.setReceInt(500);  //相当于关闭串口中断
                this.btn_autoUpdate.Enabled = true;
            }
            //(6)修改其他提示信息
            fmain.lbl_protocal.Text     = "协议:串口";
            fmain.lbl_protocalinfo.Text = "协议信息:端口" + com + ",波特率9600";
        }
        //======================================================================
        //函数名称: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 = "协议信息:";
            }
        }
Пример #3
0
        /// ------------------------------------------------------------------------------
        /// <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);
        }