niujiuru 1 ماه پیش
والد
کامیت
e53307088a
1فایلهای تغییر یافته به همراه13 افزوده شده و 11 حذف شده
  1. 13 11
      mcu_ctrl_board/mcu_ctrl_board.c

+ 13 - 11
mcu_ctrl_board/mcu_ctrl_board.c

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