/// <summary> /// 串口输入。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SerialInput(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { try { ReceiveBuffer += COMPort.ReadExisting(); if (ReceiveBuffer.IndexOf("\r\n") + 1 == 1)// == 1 为添加 { //If InStr(ReceiveBuffer, Chr(13)) Then //Contains at least one carridge return string[] lines = ReceiveBuffer.Split('\n'); //Dim lines() As String = Split(ReceiveBuffer, Chr(13)) for (var i = 0; i <= (lines.Length - 1) - 1; i++) { if (lines[(int)i].Length > 5) { SendSerialLineToUIThread(lines[(int)i].Trim()); } } ReceiveBuffer = lines[(lines.Length - 1)]; } else { //Data doesn't contain any line breaks if (ReceiveBuffer.Length > 4000) { ReceiveBuffer = ""; SendSerialLineToUIThread("No line breaks found in data stream."); } } } catch (Exception ex) { log.Error(ex.Message); } }
private void COMPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { labelMES.Text = COMPort.ReadExisting(); }