/// <summary> /// 用来切换串口接收或者串口不接收 /// </summary> private void SwitchReceiveBTN_Click(object sender, EventArgs e) { if (SwitchReceiveBTN.Text == "停止接收") { NowUartReceiveStatus = UARTReceiveStatus.DataReceiveFinish; } if (NowUartReceiveStatus == UARTReceiveStatus.SerialPortClosed || NowUartSendStatus == UARTSendStatus.SerialPortClosed) { Uart1.SetSerialPort(PortCB.Text, int.Parse(BaudCB.Text), int.Parse(DataBitsCB.Text), int.Parse(StopBitsCB.Text)); if (Uart1.SwtichSP(true) == true)//打开成功 { NowUartReceiveStatus = UARTReceiveStatus.SerialPortOpen; NowUartSendStatus = UARTSendStatus.SerialPortOpen; } else { PortCBFresh(); } } if (NowUartReceiveStatus == UARTReceiveStatus.SerialPortOpen) { Uart1.sp.Encoding = Encoding.GetEncoding("GB2312"); //使用委托以及多线程进行 #region 打开多线程定时器 //创建代理对象TimerCallback,该代理将被定时调用 TimerCallback timerDelegate = new TimerCallback(TimerRecive); //创建一个时间间隔为1s的定时器 System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, s, 0, 100); s.tmr = timer; #endregion SwitchReceiveBTN.Text = "停止接收"; NowUartReceiveStatus = UARTReceiveStatus.DataReceiving; } }
/// <summary> /// 用来将发送区文本框的文本通过串口发送 /// </summary> private void SendBTN_Click(object sender, EventArgs e) { if (NowUartSendStatus == UARTSendStatus.SerialPortClosed) { Uart1.SetSerialPort(PortCB.Text, int.Parse(BaudCB.Text), int.Parse(DataBitsCB.Text), int.Parse(StopBitsCB.Text)); if (Uart1.SwtichSP(true) == true)//打开成功 { NowUartSendStatus = UARTSendStatus.SerialPortOpen; NowUartReceiveStatus = UARTReceiveStatus.SerialPortOpen; } else { PortCBFresh(); } } if (NowUartSendStatus == UARTSendStatus.SerialPortOpen) { NowUartSendStatus = UARTSendStatus.DataSending; string strbuff = SendTB.Text; if (HexSendCB.Checked == true) { ///检测是否符合格式 for (int i = 0; i < strbuff.Length; i += 3) { try { int var1 = Convert.ToInt16(strbuff[i]); if (var1 < 48 || var1 > 57) { throw new Exception(); } var1 = Convert.ToInt16(strbuff[i + 1]); } catch (Exception) { MessageBox.Show("不符合Hex的发送格式!\r\n格式为:3A 2B\r\n最后没有空格!!!"); return; } if (i + 2 < strbuff.Length && strbuff[i + 2] != ' ') { MessageBox.Show("不符合Hex的发送格式!\r\n格式为:3A 2B\r\n最后没有空格!!!"); return; } } ///正式发送 int size = (strbuff.Length + 1) / 3; if ((strbuff.Length + 1) % 3 != 0) { MessageBox.Show("不符合Hex的发送格式!\r\n格式为:3A 2B\r\n最后没有空格!!!"); return; } if (size <= 0) { MessageBox.Show("至少要一个16进制数!"); return; } byte[] sendbuff = new byte[size]; for (int i = 0; i < strbuff.Length; i += 3) { byte num1 = Convert.ToByte(Convert.ToByte(strbuff[i] - '0') * 16); byte num2 = Convert.ToByte(Convert.ToByte(strbuff[i + 1] - '0')); sendbuff[i / 3] = Convert.ToByte(num1 + num2); } Uart1.sp.Write(sendbuff, 0, size); } else { Uart1.sp.Write(strbuff); } NowUartSendStatus = UARTSendStatus.DataSendFinish; NowUartSendStatus = UARTSendStatus.SerialPortOpen; } }
/// <summary> /// 串口接收用定时器函数 /// </summary> /// <param name="state"></param> void TimerRecive(Object state) { TimerExampleState s = (TimerExampleState)state; if (NowUartReceiveStatus == UARTReceiveStatus.DataReceiveFinish) { if (Uart1.SwtichSP(false) == true)//关闭成功 { SwitchReceiveBTN.Text = "开始接收"; NowUartReceiveStatus = UARTReceiveStatus.SerialPortClosed; NowUartSendStatus = UARTSendStatus.SerialPortClosed; } else { MessageBox.Show("关闭串口失败!"); } s.tmr.Dispose(); s.tmr = null; return; } try { if (HexCB.Checked == true) { strRecieve = ""; byte[] Receivebuff = new byte[500]; int ReceiveNum = Uart1.sp.Read(Receivebuff, 0, Uart1.sp.BytesToRead); if (ReceiveNum > 0) { for (int i = 0; i < ReceiveNum; i++) { int hexnum1 = Receivebuff[i] / 16; int hexnum2 = Receivebuff[i] % 16; strRecieve += hexnum1.ToString(); strRecieve += hexnum2.ToString(); strRecieve += " "; //strRecieve += Receivebuff[i]; } Console.WriteLine(strRecieve); ReceiveTB.AppendText(strRecieve); } //Console.WriteLine(Receivebuff.ToString()); } else { strRecieve = Uart1.sp.ReadExisting(); if (strRecieve != "") { Console.WriteLine(strRecieve); ReceiveTB.AppendText(strRecieve); if (ReceiveTB.Text.Length >= 10000) { NowUartReceiveStatus = UARTReceiveStatus.DataReceiveFinish; SwitchReceiveBTN.Text = "开始接收"; } } } } catch (Exception ex) { } //} }