基于對串口FIFO的通信資料幀進行接收和分析處理
(關于串口FIFO使用可以參見作者另一篇博文)
演算法流程:
- 串口中斷函式接收資料到FIFO,
- 根據通信協議GetInterUARTMessage()函式對資料幀進行判斷獲取,
- 根據通信協議AnalyzeInterUARTMessage()函式對資料幀進行分類處理,
重點分析:
memcmp()函式:
對資料幀類別進行判斷,并做后續處理,
變數ucInterHead 和 usInterPos :
對資料幀幀頭和幀尾位置進行定位,
串口FIFO中的InterRxBufferRead():
讀取FIFO中一個位元組資料,
void AnalyzeInterUARTMessage(uint8_t *ucaBuf, uint16_t usLen)
{
if(memcmp(ucaBuf, "SetSleepTime", 12) == 0)
{
#if 1
char string[20];
memcpy(string,ucaBuf,usLen);
string[usLen] = '\0';
printf("%s\r\n",string);
#endif
}
if(memcmp(ucaBuf, "SaveData", 6) == 0)
{
#if 1
char string[20];
memcpy(string,ucaBuf,usLen);
string[usLen] = '\0';
printf("%s\r\n",string);
#endif
}
}
void GetInterUARTMessage(void)
{
uint8_t ucData;
static uint8_t ucInterHead = 0;
static uint8_t ucaInterBuf[512];
static uint16_t usInterPos = 0;
while (1)
{
if(InterRxBufferRead(&ucData))
{
if (ucInterHead == 0)
{
if (ucData == '$')
{
ucInterHead = 1;
usInterPos = 0;
}
}
else
{
if (usInterPos < sizeof(ucaInterBuf))
{
ucaInterBuf[usInterPos++] = ucData;
if (ucData == '@')
{
AnalyzeInterUARTMessage(ucaInterBuf, usInterPos-1);
ucInterHead = 0;
}
}
else
{
ucInterHead = 0;
}
}
continue;
}
break;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/19259.html
標籤:其他
上一篇:ESP32 開發筆記(三)原始碼示例 15_WIFI_AP 創建軟AP示例
下一篇:鴻蒙OS代碼正式開源
