串口不定長(zhǎng)數(shù)據(jù)包
1、背景
為什么要實(shí)現(xiàn)串口不定長(zhǎng)數(shù)據(jù)包的接收?
在使用串口進(jìn)行數(shù)據(jù)接時(shí),有時(shí)候我們并不知道對(duì)方的串口數(shù)據(jù)協(xié)議格式,于是沒(méi)辦法按照協(xié)議進(jìn)行解析,也就無(wú)法提取完整的數(shù)據(jù)包出來(lái),而應(yīng)用層則需要完整的數(shù)據(jù)包進(jìn)行處理,就迫使我們需要實(shí)現(xiàn)一種不定長(zhǎng)數(shù)據(jù)包提取的實(shí)現(xiàn)方式;
舉個(gè)例子,如果我們串口對(duì)接的是設(shè)備的串口的日志,由于日志并不是固定長(zhǎng)度的,那么我們?nèi)绾蝸?lái)提取一行日志呢(通過(guò)判斷換行符是一種方法)?如果對(duì)方的日志是不可見(jiàn)字符呢?
通過(guò)實(shí)現(xiàn)串口不定長(zhǎng)數(shù)據(jù)包的接收,可以提高系統(tǒng)的靈活性和適應(yīng)性,使其能夠更好地應(yīng)對(duì)不同的數(shù)據(jù)傳輸需求。
一句話(huà): 本文實(shí)現(xiàn)了esp32串口不定長(zhǎng)數(shù)據(jù)接收方法,當(dāng)接收數(shù)據(jù)超過(guò)512個(gè)字節(jié)自動(dòng)按512個(gè)字節(jié)自動(dòng)打包,如果不足512個(gè)字節(jié),則通過(guò)兩個(gè)數(shù)據(jù)的時(shí)間間隔差自動(dòng)拆分包;
不想聽(tīng)我啰嗦,公眾號(hào)后臺(tái)私信: 代碼分享 即可獲取完整工程;
2、實(shí)現(xiàn)
在STM32平臺(tái)有一個(gè)串口空閑的標(biāo)志位,我們通過(guò)判斷串口的空閑標(biāo)志位來(lái)實(shí)現(xiàn),但是如果使用ESP32,則很遺憾,我并沒(méi)有見(jiàn)到有此標(biāo)志位可以使用,那么本文將通過(guò)軟件方式來(lái)實(shí)現(xiàn)串口不定長(zhǎng)數(shù)據(jù)的接收實(shí)現(xiàn);
2.1、實(shí)現(xiàn)平臺(tái)
idf版本: esp-idf-v5.2.0
硬件: esp32-c3
2.2、實(shí)現(xiàn)的邏輯
在串口接收到數(shù)據(jù)時(shí),啟動(dòng)串口數(shù)據(jù)監(jiān)聽(tīng)和串口數(shù)據(jù)包處理兩個(gè)線(xiàn)程;
監(jiān)聽(tīng)線(xiàn)程監(jiān)聽(tīng)到數(shù)據(jù):判斷接收到的數(shù)據(jù)長(zhǎng)度是否大于512個(gè)字節(jié),如果是則把512個(gè)字節(jié)打包為一個(gè)數(shù)據(jù)包,如果數(shù)據(jù)不足512個(gè)字節(jié),則先緩存起來(lái),然后記錄此時(shí)的時(shí)間為T(mén)1;
如果打包之后還有數(shù)據(jù),則繼續(xù)運(yùn)行第1步,如果剩余的數(shù)據(jù)不足512個(gè)字節(jié),則先緩存起來(lái),然后記錄此時(shí)的時(shí)間T1;
定時(shí)器周期檢測(cè)T1與當(dāng)前時(shí)間的差值,如果差值大于100ms,則把剩余的數(shù)據(jù)打包為一個(gè)數(shù)據(jù)包;
如果還有新的數(shù)據(jù)監(jiān)測(cè)到,則重新開(kāi)始第1步;
串口數(shù)據(jù)包處理現(xiàn)場(chǎng)監(jiān)聽(tīng)到有數(shù)據(jù)包處理則進(jìn)行處理;
2.3、邏輯流程圖
串口不定長(zhǎng)流程圖
3實(shí)現(xiàn)效果測(cè)試數(shù)據(jù)
3.1、 數(shù)據(jù)小于512個(gè)字節(jié)
串口輸入(20個(gè)字節(jié)):"30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30"
代碼識(shí)別:達(dá)到預(yù)期效果
uart_rx_ap|229|[uart]***********************0020**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
串口輸入(20個(gè)字節(jié)):"30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30"(150ms周期發(fā)送)
代碼識(shí)別(自動(dòng)拆分為多次多個(gè)數(shù)據(jù)包,每個(gè)數(shù)據(jù)包還是21個(gè)字節(jié)):達(dá)到預(yù)期效果
uart_rx_ap|229|[uart]***********************0020**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]***********************0020**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]***********************0020**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
串口輸入(20個(gè)字節(jié)):"30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30"(15ms周期發(fā)送)
uart_rx_ap|229|[uart]***********************0512**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]303030303030303030303030
uart_rx_ap|229|[uart]***********************0176**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]30303030303030303030303030303030
- 代碼識(shí)別:達(dá)到預(yù)期效果(按512個(gè)字節(jié)打包,不足的按間隔打包)
3.2、數(shù)據(jù)等于于512個(gè)字節(jié)
- 串口輸入(512個(gè)字節(jié)):"30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 ... ... 30"
- 代碼識(shí)別:達(dá)到預(yù)期效果
uart_rx_ap|229|[uart]***********************0512**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]303030303030303030303030
3.3、數(shù)據(jù)大于512個(gè)字節(jié)
串口輸入(565個(gè)字節(jié)):"30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 ... ... 30"
代碼識(shí)別:達(dá)到預(yù)期效果
uart_rx_ap|229|[uart]***********************0512**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]303030303030303030303030
uart_rx_ap|229|[uart]***********************0053**********************
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]3030303030303030303030303030303030303030
uart_rx_ap|229|[uart]30303030303030303030303030
溫馨提示: 公眾號(hào)后臺(tái)私信發(fā)送: 代碼分享 即可獲取完整工程;
-
串口
+關(guān)注
關(guān)注
14文章
1554瀏覽量
76508 -
數(shù)據(jù)包
+關(guān)注
關(guān)注
0文章
261瀏覽量
24391 -
數(shù)據(jù)接收
+關(guān)注
關(guān)注
0文章
12瀏覽量
7813 -
ESP32
+關(guān)注
關(guān)注
18文章
971瀏覽量
17255
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論