private static void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] ReDatas = new byte[ComDevice.BytesToRead]; ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据 foreach (byte item in ReDatas) { RecvBuffer[RecvBufferIndex] = item; // check packet complete if (RecvBufferIndex > 2 && item == '+' && RecvBuffer[RecvBufferIndex - 1] == '+' && RecvBuffer[RecvBufferIndex - 2] == '+') { SerialType = (SERIAL_TYPE)RecvBuffer[0]; ParseContent(); SerialType = SERIAL_TYPE.SERIAL_NONE; RecvBufferIndex = 0; } else { RecvBufferIndex++; } if (RecvBufferIndex == 30) { RecvBufferIndex = 0; } } }
private void ComDevice_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] ReDatas = new byte[ComDevice.BytesToRead]; ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据 rcnt += ReDatas.Length; //foreach as in foreach (byte a in ReDatas) { //state_machine(a); this.txtR.Invoke(new Action(() => { lblSts.Text = "Tx:" + tcnt.ToString() + "; Rx:" + rcnt.ToString(); if (rb2.Checked) { txtR.Text += a.ToString("X2") + ' '; } else { txtR.Text += "0x" + a.ToString("X2") + ", "; } })); } Thread.Sleep(5); }
/// <summary> /// 串口接收中断处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (ComDevice.IsOpen) { try { //ShowMsg("接收成功"); int Rx_Length = ComDevice.BytesToRead; //要接收的数据长度 byte[] RxBuffer = new byte[Rx_Length]; //接收数据数组 ComDevice.Read(RxBuffer, 0, Rx_Length); for (int i = 0; i < Rx_Length; i++) { ReceiveAndCheck(RxBuffer[i]); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] ReDatas = new byte[ComDevice.BytesToRead]; ComDevice.Read(ReDatas, 0, ReDatas.Length); //读取数据 this.AddData(ReDatas); //输出数据 }