|
@@ -1,9 +1,9 @@
|
|
|
/********************************************************************************
|
|
/********************************************************************************
|
|
|
- * 串口接收支持 JSON 和 TEXT 两种数据格式:
|
|
|
|
|
|
|
+ * 串口数据接收目前支持 JSON 和 TEXT 两种报文格式:
|
|
|
* 1. JSON:以 '{' 或 '[' 作为起始字符,按 JSON 语法进行流式解析。
|
|
* 1. JSON:以 '{' 或 '[' 作为起始字符,按 JSON 语法进行流式解析。
|
|
|
* 2. TEXT:由可打印字符组成,可打印字符范围为 0x20~0x7E,但 '{' 和 '[' 保留为 JSON
|
|
* 2. TEXT:由可打印字符组成,可打印字符范围为 0x20~0x7E,但 '{' 和 '[' 保留为 JSON
|
|
|
- * 报文起始字符,因此 TEXT 中不得包含 '{' 或 '[';TEXT 以串口接收空闲超时或
|
|
|
|
|
- * 收到JSON开始字符作为报文结束标志。
|
|
|
|
|
|
|
+ * 报文起始字符,因此 TEXT 中不得包含 '{' 或 '[';TEXT 报文以接收空闲超时或
|
|
|
|
|
+ * 收到JSON开始字符作为结束标志。
|
|
|
*******************************************************************************/
|
|
*******************************************************************************/
|
|
|
#include "mcu_ctrl_board.h"
|
|
#include "mcu_ctrl_board.h"
|
|
|
#include "../../rtu_linux_modules/swapi/subjects/serial/serial.h"
|
|
#include "../../rtu_linux_modules/swapi/subjects/serial/serial.h"
|
|
@@ -105,7 +105,7 @@ static void consume_cached_text_info(const void *pSerial/*打开的串口句柄*
|
|
|
|
|
|
|
|
sw_log_debug("[%s] %s received a text(%d bytes) info: %s", MODULE_NAME, log_prefix, pTextCache->len, pTextCache->buf);
|
|
sw_log_debug("[%s] %s received a text(%d bytes) info: %s", MODULE_NAME, log_prefix, pTextCache->len, pTextCache->buf);
|
|
|
|
|
|
|
|
- char *pReply = go_handle_serial_text_info((const char *)pTextCache->buf); // 回调处理TEXT信息报文
|
|
|
|
|
|
|
+ char *pReply = go_handle_serial_text_info((const char *)pTextCache->buf); // 回调处理一条TEXT报文
|
|
|
if(pReply)
|
|
if(pReply)
|
|
|
{
|
|
{
|
|
|
int nReplyBytes = strlen(pReply);
|
|
int nReplyBytes = strlen(pReply);
|
|
@@ -132,7 +132,7 @@ static int comio_data_recv_proc(unsigned long wParam/*传递打开的串口句
|
|
|
case IDLE:
|
|
case IDLE:
|
|
|
if(ch == '{' || ch == '[')
|
|
if(ch == '{' || ch == '[')
|
|
|
{ // IDLE -> JSON
|
|
{ // IDLE -> JSON
|
|
|
-json_p:
|
|
|
|
|
|
|
+json_p: //start JSON
|
|
|
if(pTextCache->len > 0) consume_cached_text_info(pSerial, pTextCache);
|
|
if(pTextCache->len > 0) consume_cached_text_info(pSerial, pTextCache);
|
|
|
ret = lwjson_stream_parse(&pComIO->parser, ch); // JSON 1: 已开始解析
|
|
ret = lwjson_stream_parse(&pComIO->parser, ch); // JSON 1: 已开始解析
|
|
|
if(ret != lwjsonSTREAMINPROG)
|
|
if(ret != lwjsonSTREAMINPROG)
|
|
@@ -144,7 +144,7 @@ json_p:
|
|
|
}
|
|
}
|
|
|
else if(xisprint(ch))
|
|
else if(xisprint(ch))
|
|
|
{ // IDLE -> TEXT
|
|
{ // IDLE -> TEXT
|
|
|
-text_p:
|
|
|
|
|
|
|
+text_p: //parse TEXT
|
|
|
if(pTextCache->len < sizeof(pTextCache->buf)-1) pTextCache->buf[pTextCache->len++] = ch;
|
|
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); }
|
|
else { pTextCache->overflow = true; sw_log_warn("[%s] TEXT cache overflow, discard byte: 0x%02X!!", MODULE_NAME, ch); }
|
|
|
serial_clear_recv_buffer(pSerial); // 已转存到TEXT缓存, 清除串口缓存
|
|
serial_clear_recv_buffer(pSerial); // 已转存到TEXT缓存, 清除串口缓存
|
|
@@ -152,7 +152,7 @@ text_p:
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{ // IDLE -> IDLE
|
|
{ // IDLE -> IDLE
|
|
|
-drop_p:
|
|
|
|
|
|
|
+drop_p: //enter DROP
|
|
|
sw_log_warn("[%s] %s discarding invalid byte: 0x%02X!", MODULE_NAME, log_prefix, ch);
|
|
sw_log_warn("[%s] %s discarding invalid byte: 0x%02X!", MODULE_NAME, log_prefix, ch);
|
|
|
serial_clear_recv_buffer(pSerial);
|
|
serial_clear_recv_buffer(pSerial);
|
|
|
}
|
|
}
|
|
@@ -173,7 +173,7 @@ drop_p:
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 2, 解析处理 JSON
|
|
|
|
|
|
|
+ // 2, 流式解析 JSON
|
|
|
if(pComIO->recvMode != JSON) goto recv_p;
|
|
if(pComIO->recvMode != JSON) goto recv_p;
|
|
|
|
|
|
|
|
ret = lwjson_stream_parse(&pComIO->parser, ch);
|
|
ret = lwjson_stream_parse(&pComIO->parser, ch);
|
|
@@ -184,7 +184,7 @@ drop_p:
|
|
|
case lwjsonSTREAMDONE: // JSON 3: 解析已完成
|
|
case lwjsonSTREAMDONE: // JSON 3: 解析已完成
|
|
|
sw_log_debug("[%s] %s received a json(%d bytes) info: %s", MODULE_NAME, log_prefix, nRecvBytes, pRecvBuf);
|
|
sw_log_debug("[%s] %s received a json(%d bytes) info: %s", MODULE_NAME, log_prefix, nRecvBytes, pRecvBuf);
|
|
|
|
|
|
|
|
- char *pReply = go_handle_serial_json_info((const char *)pRecvBuf); // 回调处理JSON信息报文
|
|
|
|
|
|
|
+ char *pReply = go_handle_serial_json_info((const char *)pRecvBuf); // 回调处理一条JSON报文
|
|
|
if(!pReply) goto idle_p;
|
|
if(!pReply) goto idle_p;
|
|
|
|
|
|
|
|
int nReplyBytes = strlen(pReply);
|
|
int nReplyBytes = strlen(pReply);
|
|
@@ -216,8 +216,8 @@ static int comio_data_idle_proc(unsigned long wParam/*传递打开的串口句
|
|
|
|
|
|
|
|
switch(pComIO->recvMode)
|
|
switch(pComIO->recvMode)
|
|
|
{
|
|
{
|
|
|
- case JSON:
|
|
|
|
|
- xgettickcount(&nowTick);
|
|
|
|
|
|
|
+ case IDLE: break; /// 什么也不需要做
|
|
|
|
|
+ case JSON: xgettickcount(&nowTick);
|
|
|
if(pComIO->jsonLastCharTick > 0 && (nowTick - pComIO->jsonLastCharTick) > RECV_IDLE_TIMEOUT)
|
|
if(pComIO->jsonLastCharTick > 0 && (nowTick - pComIO->jsonLastCharTick) > RECV_IDLE_TIMEOUT)
|
|
|
{ // JSON -> IDLE
|
|
{ // JSON -> IDLE
|
|
|
sw_log_warn("[%s] %s timeout occurred while receiving a json message(%d bytes): %s!", MODULE_NAME, log_prefix, nRecvBytes, pRecvBuf);
|
|
sw_log_warn("[%s] %s timeout occurred while receiving a json message(%d bytes): %s!", MODULE_NAME, log_prefix, nRecvBytes, pRecvBuf);
|
|
@@ -225,8 +225,7 @@ static int comio_data_idle_proc(unsigned long wParam/*传递打开的串口句
|
|
|
pComIO->jsonLastCharTick = 0; pComIO->recvMode = IDLE;
|
|
pComIO->jsonLastCharTick = 0; pComIO->recvMode = IDLE;
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
|
- case TEXT:
|
|
|
|
|
- xgettickcount(&nowTick);
|
|
|
|
|
|
|
+ case TEXT: xgettickcount(&nowTick);
|
|
|
if(pComIO->textLastCharTick > 0 && (nowTick - pComIO->textLastCharTick) > RECV_IDLE_TIMEOUT)
|
|
if(pComIO->textLastCharTick > 0 && (nowTick - pComIO->textLastCharTick) > RECV_IDLE_TIMEOUT)
|
|
|
{ // TEXT -> IDLE
|
|
{ // TEXT -> IDLE
|
|
|
if(pTextCache->len > 0) { consume_cached_text_info(pSerial, pTextCache); }
|
|
if(pTextCache->len > 0) { consume_cached_text_info(pSerial, pTextCache); }
|
|
@@ -265,7 +264,7 @@ int MCU_ComExit()
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// 主动的发送指令给MCU控制板, 返回: 0成功, <0时失败
|
|
|
|
|
|
|
+// 主动的发送指令给MCU控制板, 返回: 发送字节数/失败
|
|
|
int MCU_ComSendCmd(const char *cmd)
|
|
int MCU_ComSendCmd(const char *cmd)
|
|
|
{
|
|
{
|
|
|
if(!s_myCom.h || !cmd) return -1;
|
|
if(!s_myCom.h || !cmd) return -1;
|
|
@@ -277,5 +276,5 @@ int MCU_ComSendCmd(const char *cmd)
|
|
|
return -2;
|
|
return -2;
|
|
|
}
|
|
}
|
|
|
sw_log_debug("[%s] sent a command(%d bytes): %s", MODULE_NAME, nBytes, cmd);
|
|
sw_log_debug("[%s] sent a command(%d bytes): %s", MODULE_NAME, nBytes, cmd);
|
|
|
- return 0;
|
|
|
|
|
|
|
+ return nBytes;
|
|
|
}
|
|
}
|