Exemple #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 = "协议信息:";
            }
        }
        //======================================================================
        //函数名称: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 = "协议信息:";
            }
        }