最近接到一個ASCII碼通訊的協議。以‘<’號開始,‘>’號結束,每個字段以豎線隔開
例如收到下面這串字符讓設備工作就行了,時間爲10分鐘。
<typedef|A|10>
編程思路大概如下
#include <stdio.h>
#include <stdlib.h> //pause
#include <string.h> //strcpy
//需要解析的命令
const unsigned char WIFI_4G_Cmd[1][20] = {
"<typedef|A|10>", //運行時間10min
};
unsigned char TSET_BUF[20] = "<typedef|A|10>"; //UART接收到的BUF
int main()
{
unsigned char i;
char* p = NULL;
unsigned int RunTime = 0;
for (i = 0; i < sizeof(WIFI_4G_Cmd) / sizeof(WIFI_4G_Cmd[0]); i++) //循環查詢
{
if ((p = strstr((char*)TSET_BUF, WIFI_4G_Cmd[i])) != NULL) //查詢成功
{
switch (i)
{
case 0: //開機啓動
{
p += strlen("<typedef|A|");
while (*p != '>')
{
if ((*p < '0') || (*p > '9')) //獲得時間
{
break;
}
RunTime = RunTime * 10 + (*p - '0');
p++;
}
break;
}
default:
break;
}
}
}
printf("RunTime = %d\n", RunTime);
system("pause");
}