/// <summary> /// 串口接收数据处理 /// </summary> /// <param name="sender">串口助手类对象</param> /// <param name="arrData">接收数据数组</param> private void SerialPortDataReceivedProcess(object sender, byte[] arrData) { this.Invoke(new Action(() => { if (SerialData.IsBytesToString(arrData)) { Console.WriteLine("接收数据:" + SerialData.ToHexString(arrData)); Console.WriteLine("接收数据:" + SerialData.ToString(arrData)); txtDataReceived.AppendText(SerialData.ToString(arrData) + "\r\n"); } else { Console.WriteLine("接收数据:" + SerialData.ToHexString(arrData)); txtDataReceived.AppendText(SerialData.ToHexString(arrData) + "\r\n"); } })); }
/// <summary> /// 串口接收数据处理 /// </summary> /// <param name="arrData">接收数据数组</param> private void SerialPortDataReceivedProcess(byte[] arrData) { this.Invoke(new Action(() => { if (chkShowWrite.Checked) { txtDataReceived.AppendText("[接收]"); } if (chkShowTime.Checked) { txtDataReceived.AppendText("[" + DateTime.Now.ToString() + "] "); } if (!SerialData.IsBytesToString(arrData)) { Console.WriteLine("接收数据:" + SerialData.ToHexString(arrData)); txtDataReceived.AppendText(SerialData.ToHexString(arrData) + "\r\n"); try { deviceTest.BytesReceviedDataProcess(arrData); bleLinkTest.SerialData(arrData); OutputBleLog("连接蓝牙成功"); } catch (Exception) { } } else { string strData = SerialData.ToString(arrData); if (chkLogHex.Checked) { txtDataReceived.AppendText(SerialData.ToHexString(arrData) + "\r\n"); } else { txtDataReceived.AppendText(strData + "\r\n"); } Console.WriteLine("接收数据:" + strData); if (strData.IndexOf("OK") >= 0) { serialBle.ReceiveSerialBle(strData); } } })); }
/// <summary> /// 串口接收数据事件 /// </summary> /// <param name="arrDataReceived"></param> private void SerialPortDataReceivedProcess(byte[] arrData) { this.Invoke(new Action(() => { if (!SerialData.IsBytesToString(arrData)) { Console.WriteLine("接收数据:" + SerialData.ToHexString(arrData)); } else { string strData = SerialData.ToString(arrData); Console.WriteLine("接收数据:" + strData); if (strData.IndexOf("OK") >= 0) { serialBle.ReceiveSerialBle(strData); } } })); }
/// <summary> /// 串口接收数据处理 /// </summary> /// <param name="arrData">接收数据数组</param> private void SerialPortDataReceivedProcess(object sender, byte[] arrData) { SerialPortHelper spb = (SerialPortHelper)sender; try { this.Invoke(new Action(() => { SerialFormat sf = (spb.SerialMark == "串口1") ? ComboToSerialFormat(cbReceiveFormat1) : ComboToSerialFormat(cbReceiveFormat2); if ((sf == SerialFormat.String) || ((sf == SerialFormat.None) && SerialData.IsBytesToString(arrData))) { OutputInfo(SerialData.ToString(arrData), "接收", spb.SerialMark); } else { OutputInfo(SerialData.ToHexString(arrData), "接收", spb.SerialMark); } })); } catch (Exception e) { Console.WriteLine(spb.SerialMark + ":接收数据异常," + e.Message); } }
/// <summary> /// 添加发送数据 /// </summary> /// <param name="arrData">byte数组数据</param> private void AddSerialWrite(byte[] arrData) { try { if (arrData.Length < 1 || !serialPortHelper.IsOpen) { return; } } catch (Exception) { return; } serialPortHelper.Write(arrData); if (chkShowWrite.Checked) { this.Invoke(new Action(() => { txtDataReceived.AppendText("[发送]"); if (chkShowTime.Checked) { txtDataReceived.AppendText("[" + DateTime.Now.ToString() + "] "); } if (chkLogHex.Checked || !SerialData.IsBytesToString(arrData)) { txtDataReceived.AppendText(SerialData.ToHexString(arrData) + "\r\n"); } else { txtDataReceived.AppendText(SerialData.ToString(arrData) + "\r\n"); } })); } }