|
|
@@ -20,15 +20,15 @@ static const char MODULE_NAME[] = "MCUCtrlBoard";
|
|
|
// 定义串口数据接收模式
|
|
|
typedef enum
|
|
|
{
|
|
|
- IDLE = 0, // IDLE空闲状态
|
|
|
- JSON = 1, // JSON数据接收
|
|
|
- TEXT = 2 // TEXT数据接收
|
|
|
+ IDLE = 0, /// IDLE空闲状态
|
|
|
+ JSON = 1, /// JSON数据接收
|
|
|
+ TEXT = 2 /// TEXT数据接收
|
|
|
} ERecvMode;
|
|
|
|
|
|
// 定义TEXT数据接收缓存
|
|
|
typedef struct
|
|
|
{
|
|
|
- unsigned char buf[1*1024];
|
|
|
+ unsigned char buf[1024];
|
|
|
int len;
|
|
|
bool overflow; // 是否溢出
|
|
|
} STextCache;
|
|
|
@@ -37,7 +37,7 @@ typedef struct
|
|
|
typedef struct
|
|
|
{
|
|
|
void *h; // 打开的串口句柄
|
|
|
- ERecvMode recvMode; // 当前串口的数据接收模式
|
|
|
+ ERecvMode recvMode; // 当前串口数据的接收模式
|
|
|
lwjson_stream_parser_t parser; // JSON流式解析器(按字节实时动态解析JSON数据报文帧)
|
|
|
unsigned long jsonLastCharTick; // JSON数据包最后接收数据时的单调计时, 用于超时判断
|
|
|
unsigned long textLastCharTick; // TEXT数据包最后接收数据时的单调计时, 用于超时判断
|
|
|
@@ -89,8 +89,8 @@ TEXT + TIMEOUT ───────────────→ IDLE
|
|
|
********************************************************************************/
|
|
|
|
|
|
// 声明Go中处理串口请求并返回应答结果的函数(供C调用)
|
|
|
-extern char *go_handle_serial_json_info(const char *jsonStr);
|
|
|
-extern char *go_handle_serial_text_info(const char *textStr);
|
|
|
+extern char *go_handle_serial_json_info(const char *jsonStr); // 处理一条JSON报文
|
|
|
+extern char *go_handle_serial_text_info(const char *textStr); // 处理一条TEXT报文
|
|
|
|
|
|
// 消费一次来自MCU控制板的纯文本消息, (串口数据处理)
|
|
|
static void consume_cached_text_info(const void *pSerial/*打开的串口句柄*/, STextCache *pTextCache /*缓存地址*/)
|
|
|
@@ -138,9 +138,11 @@ json_p: //start JSON
|
|
|
if(ret != lwjsonSTREAMINPROG)
|
|
|
{
|
|
|
sw_log_error("[%s] failed to start JSON parsing, lwjson_stream_parse() returned: %d!!", MODULE_NAME, ret);
|
|
|
- serial_clear_recv_buffer(pSerial); lwjson_stream_reset(&pComIO->parser); goto recv_p;
|
|
|
+ serial_clear_recv_buffer(pSerial); lwjson_stream_reset(&pComIO->parser);
|
|
|
}
|
|
|
- pComIO->recvMode = JSON; xgettickcount(&pComIO->jsonLastCharTick); goto recv_p;
|
|
|
+ else
|
|
|
+ { pComIO->recvMode = JSON; xgettickcount(&pComIO->jsonLastCharTick); }
|
|
|
+ goto recv_p;
|
|
|
}
|
|
|
else if(xisprint(ch))
|
|
|
{ // IDLE -> TEXT
|
|
|
@@ -148,7 +150,7 @@ text_p: //cache TEXT
|
|
|
if(pTextCache->len < sizeof(pTextCache->buf)-1) pTextCache->buf[pTextCache->len++] = ch;
|
|
|
else { pTextCache->overflow = true; sw_log_warn("[%s] TEXT cache overflow, discard byte: 0x%02X!!", MODULE_NAME, ch); }
|
|
|
serial_clear_recv_buffer(pSerial); // 已转存到TEXT缓存, 清除串口缓存
|
|
|
- pComIO->recvMode = TEXT; xgettickcount(&pComIO->textLastCharTick);
|
|
|
+ { pComIO->recvMode = TEXT; xgettickcount(&pComIO->textLastCharTick); }
|
|
|
}
|
|
|
else
|
|
|
{ // IDLE -> IDLE
|
|
|
@@ -216,7 +218,6 @@ static int comio_data_idle_proc(unsigned long wParam/*传递打开的串口句
|
|
|
|
|
|
switch(pComIO->recvMode)
|
|
|
{
|
|
|
- case IDLE: break; /// 什么也不需要做
|
|
|
case JSON: xgettickcount(&nowTick);
|
|
|
if(pComIO->jsonLastCharTick > 0 && (nowTick - pComIO->jsonLastCharTick) > RECV_IDLE_TIMEOUT)
|
|
|
{ // JSON -> IDLE
|
|
|
@@ -232,6 +233,7 @@ static int comio_data_idle_proc(unsigned long wParam/*传递打开的串口句
|
|
|
pComIO->textLastCharTick = 0; pComIO->recvMode = IDLE;
|
|
|
}
|
|
|
break;
|
|
|
+ default: break; //// 什么也不需要做
|
|
|
}
|
|
|
|
|
|
return 1;
|