日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于串口接收数据不全的问题

發(fā)布時(shí)間:2025/3/21 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于串口接收数据不全的问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 /// <summary> 2 /// 字節(jié)緩沖器 3 /// </summary> 4 public class ByteQueue 5 { 6 private List<byte> m_buffer = new List<byte>(); 7 public bool Find() 8 { 9 if (m_buffer.Count == 0) 10 return false; 11 int HeadIndex = m_buffer.FindIndex(o => o == 0xAA); 12 13 if (HeadIndex == -1) 14 { 15 m_buffer.Clear(); 16 return false; //沒找到AA 17 } 18 19 else if (HeadIndex != 0) //不為開頭移掉之前的字節(jié) 20 { 21 if (HeadIndex > 1) 22 m_buffer.RemoveRange(0, HeadIndex); 23 } 24 25 int length= GetLength(); 26 27 if (m_buffer.Count <length) 28 { 29 return false; 30 } 31 32 int TailIndex = m_buffer.FindIndex(o => o == 0x55); //查找55的位置 33 34 if (TailIndex == -1) 35 { 36 //這一步為防止連發(fā)一個(gè)AA開頭的包后,沒發(fā)55,而又發(fā)了一個(gè)AA 37 int head = m_buffer.FindLastIndex(o => o == 0xAA); 38 if (head > -1) 39 { 40 m_buffer.RemoveRange(0, head); 41 } 42 return false; 43 } 44 else if (TailIndex + 1 != length) //計(jì)算包尾是否與包長度相等 45 { 46 m_buffer.RemoveRange(0, TailIndex); 47 return false; 48 } 49 50 return true; 51 } 52 53 /// <summary> 54 /// 命令類型 55 /// </summary> 56 /// <returns></returns> 57 public byte Cmd() 58 { 59 if (m_buffer.Count >= 2) 60 { 61 return m_buffer[1]; 62 } 63 return 0; 64 } 65 66 /// <summary> 67 /// 序號(hào) 68 /// </summary> 69 /// <returns></returns> 70 public byte Number() 71 { 72 if (m_buffer.Count >= 3) 73 { 74 return m_buffer[2]; 75 } 76 return 0; 77 } 78 79 /// <summary> 80 /// 包長度 81 /// </summary> 82 /// <returns></returns> 83 public int GetLength() 84 { 85 int len = 5;//AA 命令類型 序號(hào) 校驗(yàn)和 55 86 if (m_buffer.Count >= 3) 87 { 88 switch (m_buffer[2]) //第三字節(jié)為序號(hào) 89 { 90 case 0x00: //序號(hào) 91 return len + 16; 92 case 0x01: //序號(hào) 93 return len + 10; 94 case 0x02: //序號(hào) 95 return len + 12; 96 } 97 } 98 return 0; 99 } 100 /// <summary> 101 /// 提取數(shù)據(jù) 102 /// </summary> 103 public void Dequeue(byte[] buffer, int offset,int size) 104 { 105 m_buffer.CopyTo(0,buffer,offset,size); 106 m_buffer.RemoveRange(0, size); 107 } 108 109 /// <summary> 110 /// 隊(duì)列數(shù)據(jù) 111 /// </summary> 112 /// <param name="buffer"></param> 113 public void Enqueue(byte[] buffer) 114 { 115 m_buffer.AddRange(buffer); 116 } 117 } 1 private ByteQueue queue = new ByteQueue(); 2 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 3 { 4 int len = serialPort1.BytesToRead; 5 if (len > 0) 6 { 7 byte[] temp = new byte[len]; 8 serialPort1.Read(temp, 0, len); 9 queue.Enqueue(temp); 10 while (queue.Find()) //while可處理同時(shí)接收到多個(gè)AA ... 55 ,AA...55的包 11 { 12 int length = queue.GetLength(); 13 byte[] readBuffer = new byte[len]; 14 queue.Dequeue(readBuffer, 0, length); 15 OnReceiveData(readBuffer); //<這里自己寫一個(gè)委托吧就OK了 16 } 17 18 } 19 20 } private List<byte> buffer = new List<byte>(4096); private void sp_DataReceived(objectsender, EventArgs e) //sp是串口控件 { int n = sp.BytesToRead; byte[] buf = new byte[n]; sp.Read(buf, 0, n);//1.緩存數(shù)據(jù) buffer.AddRange(buf); //2.完整性判斷 while (buffer.Count >= 4) //至少包含幀頭(2字節(jié))、長度(1字節(jié))、校驗(yàn)位(1字節(jié));根據(jù)設(shè)計(jì)不同而不同 { //2.1 查找數(shù)據(jù)頭 if (buffer[0] == 0x01) //傳輸數(shù)據(jù)有幀頭,用于判斷 { int len = buffer[2]; if (buffer.Count < len + 4) //數(shù)據(jù)區(qū)尚未接收完整 { break; } //得到完整的數(shù)據(jù),復(fù)制到ReceiveBytes中進(jìn)行校驗(yàn) buffer.CopyTo(0, ReceiveBytes, 0, len + 4); byte jiaoyan; //開始校驗(yàn) jiaoyan = this.JY(ReceiveBytes); if (jiaoyan != ReceiveBytes[len+3]) //校驗(yàn)失敗,最后一個(gè)字節(jié)是校驗(yàn)位 { buffer.RemoveRange(0, len + 4); MessageBox.Show("數(shù)據(jù)包不正確!"); continue; } buffer.RemoveRange(0, len + 4); /執(zhí)行其他代碼,對數(shù)據(jù)進(jìn)行處理。 } else //幀頭不正確時(shí),記得清除 { buffer.RemoveAt(0); } } }

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/laowengdiaodayu/p/4553394.html

總結(jié)

以上是生活随笔為你收集整理的关于串口接收数据不全的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。