//====================================================================== //函数名称:btn_uartcheck_Click //函数返回:无 //参数说明:无 //功能概要:“重新连接”按钮点击事件,实现上位机与终端的重新连接操作 //====================================================================== private void btn_uartcheck_Click(object sender, EventArgs e) { //(1)变量申明 string com; //(2)更新状态提示信息 fmain.lbl_status.Text = "运行状态:单机“重新连接”按钮..."; //(3)重新遍历串口,寻找下位机 if (uart != null && uart.IsOpen) { uart.close(); } com = pcNode.findPCNode(); uart = pcNode.PCNode_Uart; fmain.lbl_status.Text = "运行状态:找到" + com; lbl_uart_state.Text = com; //(4)重新绑定DataReceived事件(即串口接收中断处理函数) if (uart != null) { 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; //使能连接终端操作 } else { this.btn_autoUpdate.Enabled = false; //使能连接终端操作 } //(5)修改其他提示信息 fmain.lbl_protocal.Text = "协议:串口"; fmain.lbl_protocalinfo.Text = "协议信息:端口" + com + ",波特率9600"; }
//====================================================================== //函数名称: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"; }
public bool PCNodeSendReturn(byte[] SendByteArray, out byte[] RecvData, int cnt, int time) { int i; bool sendflag = false; RecvData = null; try { uart1.setReceInt(1000); //测试串口接收中断使用 for (i = 0; i < cnt; i++) { sendflag = uart1.sendData(SendByteArray, SendByteArray.Length); // 通过串口发送数据 System.Threading.Thread.Sleep(time); RecvData = uart1.recvData(); // 通过串口接收数据 if (RecvData.Length >= 1) { break; } } } catch { return(false); //产生错误,返回false } return(sendflag); // 返回接收数据数据的首地址 }
//====================================================================== //函数名称: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 = "协议信息:"; } }
/// ------------------------------------------------------------------------------ /// <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); }