none
微软的大神呢?c#串口接收解析10ms无帧头帧尾数据?(比如:[float][float][crc]) RRS feed

  • 问题

  • public delegate void RecEventHandler(object sender, byte[] data); public partial class Form1 : Form { Stopwatch sw = new Stopwatch(); TimeSpan now = new TimeSpan(0); Thread thr; public event RecEventHandler Rec; private long timeOut = 9; Stream s; public Form1() { InitializeComponent(); serialPort1.Open(); s = serialPort1.BaseStream; thr = new Thread(() => { while (true) { Thread.Sleep(0); if (sw.ElapsedMilliseconds > timeOut) { int len = serialPort1.BytesToRead; byte[] buffer = new byte[len]; s.Read(buffer, 0, len); if (buffer.Length > 0) { Rec?.Invoke(this, buffer); } } } }); thr.IsBackground = true; thr.Start(); this.Rec += Form1_Rec; } private void Form1_Rec(object sender, byte[] data) { new Thread(() => { DataOperation dor = new DataOperation(); string temp = dor.ByteArrayToHexString(data); this.Invoke((EventHandler)delegate { richTextBox1.AppendText(temp + "\n"); }); }).Start(); } private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { sw.Reset(); sw.Start(); } }


    c#串口接收解析10ms无帧头帧尾数据?(比如:[float][float][crc])

    我这么写就偶尔会出现两帧或者三四帧连一块的情况?请微软大神给出答案



    2016年6月21日 4:09