zhunk 10 bulan lalu
induk
melakukan
1379ed7b4d

+ 89 - 137
Application_CBD/applications/dtu_uart2.c

@@ -78,7 +78,7 @@ static int DTU_uart2_init(void)
         dtuconfig.baud_rate = BAUD_RATE_115200;  //修改波特率9600
         dtuconfig.data_bits = DATA_BITS_8;     //数据位8c
         dtuconfig.stop_bits = STOP_BITS_1;     //停止位1
-        dtuconfig.bufsz = 1024;                 //缓冲区1k
+        dtuconfig.bufsz = 2048;                 //缓冲区1k
         dtuconfig.parity = PARITY_NONE;        //奇校验
         /* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
         rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
@@ -108,18 +108,6 @@ void DTU_Write(char *fmt, ...)
 /* 接收数据回调函数 */
 static rt_err_t dtu_input(rt_device_t dev, rt_size_t size)
 {
-//        struct rx_msg msg;
-//        rt_err_t result;
-//        msg.dev = dev;
-//        msg.size = size;
-//        result = rt_mq_send(&rx_mq_dtu,&msg, sizeof(struct rx_msg));
-//        if ( result == -RT_EFULL)
-//        {
-//            /* 消息队列满 */
-//            LOG_E("rx_mq_dtu message queue full!\n");
-//            rt_mq_control(&rx_mq_dtu, RT_IPC_CMD_RESET, RT_NULL); // 清空队列
-//        }
-   /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
    rt_sem_release(&rx_sem);
    return RT_EOK;
 }
@@ -143,6 +131,7 @@ static void dtu_thread_entry_recv(void *parameter)
             // 防止超范围
             accum_len = 0;
             lwjson_stream_reset(&lwparser);
+            memset(accum_buf,0,sizeof(accum_buf));
             continue;
         }
         lwjsonr_t ret;
@@ -173,105 +162,6 @@ static void dtu_thread_entry_recv(void *parameter)
     }
 }
 
-#if 0
-/* 入口函数-指令接收*/
-static void dtu_thread_entry_recv(void *parameter)
-{
-    struct rx_msg msg;
-    rt_err_t result;
-    rt_uint32_t rx_length;
-    rt_uint32_t rx_err = 0;
-    static char str_check[2]={'{','}'};
-    static char rx_buffer[512];
-    static char rx_buffer_tmp[512];
-    static char rx_buffer_tmp2[512];
-    while(1)
-    {
-        if(device_status.update_flag==1) break;
-        led_tog(1);
-        rt_memset(&msg, 0, sizeof(msg));
-        // 从消息队列中获取被回调函数放入消息队列中的内容
-        result = rt_mq_recv(&rx_mq_dtu, &msg, sizeof(struct rx_msg), 50);
-        if(result == RT_EOK)
-        {
-            rx_err = 0;
-            rx_length =(sizeof(rx_buffer) - 1) > msg.size ?
-                    msg.size : sizeof(rx_buffer) - 1;
-            rx_length = rt_device_read(msg.dev, 0, &rx_buffer[0], rx_length);
-            rx_buffer[rx_length] = '\0';
-            LOG_D("recv data:%s",rx_buffer);
-            if(strstr(rx_buffer,"{")==RT_NULL||strstr(rx_buffer,"}")==RT_NULL)
-            {
-                if(device_status.dtype==7)
-                {
-                    HMI_WRITE("sysTest.result.txt=\"%s\"",rx_buffer);
-                }
-
-                if(device_status.dtype==3)
-                {
-                    HMI_WRITE("sysSet.va0.txt=\"%s\"",rx_buffer);
-                    rt_timer_stop(timer_uart2);
-                }
-
-            }
-            //过滤异常数据
-            char str_check_head[2];
-            char str_check_end[2];
-            rt_sprintf(str_check_head, "%c",rx_buffer[0]);
-            rt_sprintf(str_check_end, "%c",rx_buffer[rx_length-1]);
-            if(str_check_end[0]==str_check[1]&&str_check_head[0]==str_check[0])
-            {
-                  parseDtu(rx_buffer);
-                  rt_memset(rx_buffer,0,sizeof(rx_buffer));
-                  rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
-                  rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
-            }
-            else {
-                   if(rx_buffer[rx_length-1]!='}')
-                   {
-                       rt_sprintf(rx_buffer_tmp, "%s",rx_buffer);
-                   }
-                   else {
-                       rt_sprintf(rx_buffer_tmp2,"%s",rx_buffer);
-                       rt_sprintf(rx_buffer, "%s%s",rx_buffer_tmp,rx_buffer_tmp2);
-                       //rt_kprintf("rxbuffer:%s",rx_buffer);
-                       parseDtu(rx_buffer);
-                       rt_memset(rx_buffer,0,sizeof(rx_buffer));
-                       rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
-                       rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
-                   }
-                   //rt_kprintf("%s",rx_buffer);
-                 }
-         }
-         else {
-                    if(rx_err==54000)//90分钟未接收到数据
-                    {
-                        rt_memset(rx_buffer,0,sizeof(rx_buffer));
-                        if(device_status.version_type==6)
-                        {
-                            LOG_E("RK3399 transter Error!");
-                            ctr_photo(OFF);
-                        }
-                        else {
-                            ctr_RTU_DTU(OFF);
-                        }
-                    }
-                    else if (rx_err>54100&&device_status.bat_status==0)
-                    {
-                        LOG_W("通信中断,RTU重新上电");
-                        rx_err = 0;
-                        ctr_RTU_DTU(ON);
-                        ctr_photo(ON);
-                    }
-               }
-            //rt_kprintf("%s\n",rx_json_buffer);
-            // 将内容写回到串口1
-            //rt_device_write(dtuserial, 0, &rx_json_buffer[0], rx_length);
-
-    }
-}
-#endif
-
 /*入口函数-指令发送*/
 static void dtu_thread_entry_send(void *parameter)
 {
@@ -772,6 +662,57 @@ char *makeJson_Cam(char * s,char *t)
     cJSON_Delete(wJsonRoot);
     return w;
 }
+
+
+/**
+ * @brief 创建JSON-RPC 2.0请求字符串
+ * @param method 方法名(必须)
+ * @param params 参数对象(可选)
+ * @param id 请求ID(可选,NULL表示通知)
+ * @return 返回JSON字符串,需要调用者释放,失败返回NULL
+ */
+char* makeJsonRpc2_0(const char* method, cJSON* params, int id)
+{
+    // 参数检查
+    if (method == NULL || method[0] == '\0') {
+        fprintf(stderr, "Error: method cannot be NULL or empty\n");
+        return NULL;
+    }
+
+    cJSON* root = cJSON_CreateObject();
+    if (root == NULL) {
+        fprintf(stderr, "Error: Failed to create JSON object\n");
+        return NULL;
+    }
+
+    // 添加 jsonrpc 版本
+    if (!cJSON_AddStringToObject(root, "jsonrpc", "2.0")) {
+        cJSON_Delete(root);
+        return NULL;
+    }
+
+    // 添加 method
+    if (!cJSON_AddStringToObject(root, "method", method)) {
+        cJSON_Delete(root);
+        return NULL;
+    }
+
+    // 添加 params(如果有)
+    if (params != NULL) {
+        // 注意:这里不复制params,调用者需要保证params在函数调用期间有效
+        cJSON_AddItemToObject(root, "params", params);
+    }
+
+    cJSON_AddNumberToObject(root, "id", id);
+    // 生成JSON字符串
+    char* json_str = cJSON_PrintUnformatted(root);
+
+    // 清理
+    cJSON_Delete(root);
+
+    return json_str;
+}
+
 /*生成JSON数据*/
 char * makeJson()
 {
@@ -1176,16 +1117,50 @@ int parseDtu(char *json_string)
     cJSON* tmp = NULL;
     cJSON* link_sta = NULL;
     cJSON* value_commn= NULL;
-    rt_kprintf("%s\n",json_string);
+    cJSON* JSONRPC = NULL;
+    cJSON* METHOD = NULL;
+    cJSON* PARAMS = NULL;
+    LOG_D("parse:%s\n",json_string);
     cJSON *root = cJSON_Parse(json_string);
     if(!root)
     {
         cJSON_Delete(root);
-        rt_kprintf("parse json err:%s",json_string);
+        LOG_E("parse json err:%s",json_string);
         return -1;
     }
     else
     {
+          JSONRPC = cJSON_GetObjectItem(root,"jsonrpc");
+          METHOD = cJSON_GetObjectItem(root,"method");
+          PARAMS = cJSON_GetObjectItem(root,"params");
+          if (JSONRPC != NULL && METHOD != NULL && METHOD->type == cJSON_String) {
+              // 使用 strcmp 比较字符串
+              if (strcmp(METHOD->valuestring, "upgradehmi") == 0) {
+                  if(PARAMS !=NULL&&PARAMS->type==cJSON_Object){
+                      cJSON* FS = cJSON_GetObjectItem(PARAMS, "fs");
+                      // 收到数据包
+                      cJSON* hex_payload = cJSON_GetObjectItem(PARAMS, "hex_payload");
+                      if (hex_payload && hex_payload->type == cJSON_String) {
+                          process_hex_payload(hex_payload->valuestring, 0);
+                      }
+                      if (FS != NULL && FS->type == cJSON_Number) {
+                          char post_hmi_upgrade[64];
+                          total_file_size = FS->valueint;
+                          // 安全格式化
+                          rt_snprintf(post_hmi_upgrade, sizeof(post_hmi_upgrade),
+                                     "whmi-wri %d,115200,0", FS->valueint);
+                          HMI_WRITE(post_hmi_upgrade);
+
+                          LOG_I("HMI升级命令已发送: %s", post_hmi_upgrade);
+                          thread_delete_workloop();
+                          rt_thread_delete(dtu_send_data);
+                      }
+                  }
+              }else{
+                  LOG_W("未知的命令: %s", METHOD->valuestring);
+              }
+
+          }
           item_cmd = cJSON_GetObjectItem(root,"cmd");
           item_checkself = cJSON_GetObjectItem(root,"selfcheck");
           photolink = cJSON_GetObjectItem(root,"photolink");
@@ -1706,29 +1681,6 @@ int parseDtu(char *json_string)
                                 rt_hw_rtc_init();
                             }
                       }
-//                      static char year_s[5],month_s[3],day_s[3],hour_s[3],min_s[3],sec_s[3];
-//                      strncpy(year_s, datetime->valuestring, 4);
-//                      strncpy(month_s,datetime->valuestring+4,2);
-//                      strncpy(day_s,datetime->valuestring+6,2);
-//                      strncpy(hour_s,datetime->valuestring+8,2);
-//                      strncpy(min_s,datetime->valuestring+10,2);
-//                      strncpy(sec_s,datetime->valuestring+12,2);
-//                      if(atoi(year_s)>=2024&&atoi(year_s)<3000&&atoi(month_s)>0&&atoi(month_s)<13&&atoi(day_s)>0&&atoi(day_s)<32)
-//                      {
-//                          if(set_date(atoi(year_s),atoi(month_s),atoi(day_s))==RT_EOK)
-//                          {
-//                              LOG_I("设置日期:%d年%d月%d日",atoi(year_s),atoi(month_s),atoi(day_s));
-//                              if(atoi(hour_s)>0&&atoi(hour_s)<=24&&atoi(min_s)>=0&&atoi(min_s)<=60&&atoi(sec_s)>=0&&atoi(sec_s)<=60)
-//                              {
-//                                  LOG_I("设置时间:%d时%d分%d秒",atoi(hour_s),atoi(min_s),atoi(sec_s));
-//                                  set_time(atoi(hour_s),atoi(min_s),atoi(sec_s));
-//                              }
-//                          }
-//                          else {
-//                              LOG_E("set_date failed!");
-//                              rt_hw_rtc_init();
-//                          }
-//                      }
                   }
 
                   LOG_I("参数同步:lat:%s lng:%s",lat->valuestring,lng->valuestring);

+ 14 - 1
Application_CBD/applications/dtu_uart2.h

@@ -9,9 +9,21 @@
  */
 #ifndef APPLICATIONS_INC_DTU_UART2_H_
 #define APPLICATIONS_INC_DTU_UART2_H_
+#include <cJSON.h>
 
 #define DTU_UART_NAME "uart2"  /*串口设备名称*/
-#define MAX_DTU_SIZE 512
+#define MAX_DTU_SIZE 2048
+
+
+
+#define JSONRPC_SUCCESS          0
+#define JSONRPC_ERR_NULL_PARAM   -1
+#define JSONRPC_ERR_ALLOC_FAIL   -2
+#define JSONRPC_ERR_BUFFER_SMALL -3
+#define JSONRPC_ERR_JSON_FAIL    -4
+#define JSONRPC_ERR_INVALID_TYPE -5
+
+
 typedef struct{
     char msg_pool[MAX_DTU_SIZE];
 }DataTemp;
@@ -42,4 +54,5 @@ char *makeJson_Config_BDH(char * deviceId);
 char *makeJson_selfCheck_(uint8_t sta);
 char *makeJson_Cam(char * s,char *t);
 char *makeJson_C01_Warn(uint16_t code,double process);
+char* makeJsonRpc2_0(const char* method, cJSON* params, int id);
 #endif /* APPLICATIONS_INC_DTU_UART2_H_ */

+ 257 - 17
Application_CBD/applications/hmi.c

@@ -67,6 +67,19 @@ rt_device_t serial;   /*串口设备句柄*/
 
 rt_timer_t timer_uart2 = RT_NULL;
 
+
+static upgrade_state_t current_state = STATE_IDLE;
+uint32_t total_file_size = 0;
+static uint32_t bytes_received = 0;
+static uint32_t bytes_sent_to_hmi = 0;
+
+static uint8_t recv_buffer[RECV_BUFFER_SIZE];
+static uint32_t recv_write_pos = 0;
+static uint32_t recv_read_pos = 0;
+
+static uint8_t send_buffer[SEND_BUFFER_SIZE];
+static uint32_t send_filled = 0;
+
 /* 消息队列控制块 */
 static struct rt_messagequeue rx_mq;//接收数据消息队列
 static void HMI_Decode(void);
@@ -89,22 +102,28 @@ static int HMI_uart4_init(void)
     return RT_EOK;
 }
 /*串口屏写数据*/
+// 在你的代码基础上最小改动
 void HMI_WRITE(char *fmt, ...)
 {
+    if (serial == NULL || fmt == NULL) {
+        return;
+    }
+
     va_list ap;
-    //末尾需要拼接的3个ff
-    const char hmi_hex[3]={0xff,0xff,0xff};
-    static char str[MAX_SIZE];
-    memset(str,'\0',sizeof(str));
+    char str[256];  // 合理的大小
     rt_size_t str_length;
-    va_start(ap,fmt);
-    str_length = rt_vsnprintf(str, sizeof(str) - 1, fmt, ap);
-    if (str_length > MAX_SIZE - 1)
-        str_length = MAX_SIZE - 1;
 
-    rt_device_write(serial, 0, str, str_length);
+    va_start(ap, fmt);
+    str_length = rt_vsnprintf(str, sizeof(str), fmt, ap);
     va_end(ap);
-    rt_device_write(serial, 0,hmi_hex,3);
+
+    if (str_length > 0 && str_length < sizeof(str)) {
+        // 发送命令
+        rt_device_write(serial, 0, str, str_length);
+        // 发送结束符
+        static const char terminator[3] = {0xFF, 0xFF, 0xFF};
+        rt_device_write(serial, 0, terminator, 3);
+    }
 }
 /* 接收数据回调函数 */
 static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
@@ -124,6 +143,201 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
     return result;
 }
 
+// 预计算转换表
+static const int8_t hex_to_byte[256] = {
+    [0 ... 255] = -1,
+    ['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, ['4'] = 4,
+    ['5'] = 5, ['6'] = 6, ['7'] = 7, ['8'] = 8, ['9'] = 9,
+    ['A'] = 10, ['B'] = 11, ['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15,
+    ['a'] = 10, ['b'] = 11, ['c'] = 12, ['d'] = 13, ['e'] = 14, ['f'] = 15
+};
+
+/**
+ * @brief 快速HEX字符串转二进制
+ * @param hex HEX字符串
+ * @param bin 输出缓冲区
+ * @param max_len 最大输出长度
+ * @return 转换的字节数,<0表示错误
+ */
+static inline int fast_hex_to_bin(const char* hex, uint8_t* bin, int max_len)
+{
+    if (!hex || !bin || max_len <= 0) {
+        return -1;
+    }
+
+    int hex_len = strlen(hex);
+    if (hex_len % 2 != 0) {
+        return -1;  // HEX长度必须是偶数
+    }
+
+    int bin_len = hex_len / 2;
+    if (bin_len > max_len) {
+        return -1;  // 输出缓冲区太小
+    }
+
+    for (int i = 0; i < bin_len; i++) {
+        int high = hex_to_byte[(unsigned char)hex[i * 2]];
+        int low = hex_to_byte[(unsigned char)hex[i * 2 + 1]];
+
+        if (high < 0 || low < 0) {
+            return -1;  // 无效HEX字符
+        }
+
+        bin[i] = (high << 4) | low;
+    }
+
+    return bin_len;
+}
+
+
+// 发送原始数据到HMI
+static int send_raw_to_hmi(const uint8_t* data, uint32_t size)
+{
+    if (!serial || !data || size == 0) return -1;
+
+    rt_size_t sent = rt_device_write(serial, 0, data, size);
+    return (sent == size) ? 0 : -1;
+}
+
+
+// 最简洁版本 - 每4096字节直接清空
+void process_hex_payload(const char* hex_str, int req_id) {
+    if (!hex_str) return;
+    current_state = STATE_RECEIVING;
+    // 转换HEX
+    uint8_t temp[JSON_CHUNK_SIZE];
+    int data_len = fast_hex_to_bin(hex_str, temp, JSON_CHUNK_SIZE);
+    if (data_len <= 0) return;
+
+    // 添加到缓冲区
+    if (recv_write_pos + data_len > sizeof(recv_buffer)) {
+        LOG_E("缓冲区溢出");
+        return;
+    }
+
+    memcpy(recv_buffer + recv_write_pos, temp, data_len);
+    recv_write_pos += data_len;
+    bytes_received += data_len;
+
+    LOG_I("收到 %d 字节,缓冲: %u/%u",
+          data_len, recv_write_pos, sizeof(recv_buffer));
+
+    // 每4096字节发送一次
+    if (recv_write_pos >= 4096) {
+        LOG_I("发送 4096 字节到HMI...");
+
+        if (send_raw_to_hmi(recv_buffer, 4096) == 0) {
+            LOG_I("发送成功");
+            // ✅ 直接清空缓冲区
+            recv_write_pos = 0;
+        } else {
+            LOG_E("发送失败");
+        }
+        return;
+    }
+
+    // 检查完成
+    if (bytes_received >= total_file_size && total_file_size > 0) {
+        if (recv_write_pos > 0) {
+            LOG_I("发送最后 %u 字节", recv_write_pos);
+            send_raw_to_hmi(recv_buffer, recv_write_pos);
+            recv_write_pos = 0;  // 清空
+        }
+        current_state = STATE_COMPLETED;
+        LOG_I("升级完成!");
+        return;
+    }
+
+    // 发送进度
+    int progress = total_file_size > 0 ?
+                  (bytes_received * 100 / total_file_size) : 0;
+
+    char progress_json[256];
+    rt_snprintf(progress_json, sizeof(progress_json),
+               "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
+               "\"params\":{\"received\":%u,\"total\":%u,\"progress\":%d},\"id\":%d}",
+               bytes_received, total_file_size, progress, req_id);
+
+    DTU_Write(progress_json);
+}
+#if 0
+// 处理HEX数据包
+void process_hex_payload(const char* hex_str, int req_id)
+{
+    if (!hex_str) return;
+    uint8_t binary_data[JSON_CHUNK_SIZE];
+    int data_len = fast_hex_to_bin(hex_str, binary_data, JSON_CHUNK_SIZE);
+
+    if (data_len <= 0) {
+        LOG_E("HEX转换失败\n");
+        return;
+    }
+
+    // 检查缓冲区是否足够
+    if (recv_write_pos + data_len > RECV_BUFFER_SIZE) {
+        LOG_E("接收缓冲区已满\n");
+        return;
+    }
+
+    // 复制到接收缓冲区
+    memcpy(recv_buffer + recv_write_pos, binary_data, data_len);
+    recv_write_pos += data_len;
+    bytes_received += data_len;
+
+    rt_kprintf("收到数据: %d字节, 累计: %d/%d\n",
+               data_len, bytes_received, total_file_size);
+
+    uint32_t available = recv_write_pos - recv_read_pos;
+    while (available >= 4096) {
+            // 发送4096字节到HMI
+            if (send_raw_to_hmi(recv_buffer + recv_read_pos, 4096) != 0) {
+                LOG_E("发送到HMI失败");
+                return;
+            }
+
+            // 更新读取位置
+            recv_read_pos += 4096;
+            available -= 4096;
+
+            LOG_I("已发送 4096 字节,缓冲剩余: %d 字节", available);
+            return;
+    }
+
+    // 3. 如果缓冲区有剩余数据,且所有数据已接收完成
+        if (bytes_received >= total_file_size && total_file_size > 0 && available > 0) {
+            // 发送剩余数据
+            if (send_raw_to_hmi(recv_buffer + recv_read_pos, available) != 0) {
+                LOG_E("发送最后一包失败");
+                return;
+            }
+
+            LOG_I("发送最后一包: %d 字节,升级完成!", available);
+
+            // 重置缓冲区
+            recv_read_pos = 0;
+            recv_write_pos = 0;
+
+            // 发送完成通知
+            char complete_json[256];
+            rt_snprintf(complete_json, sizeof(complete_json),
+                       "{\"jsonrpc\":\"2.0\",\"method\":\"upgrade_complete\","
+                       "\"params\":{\"total\":%d},\"id\":%d}",
+                       total_file_size, req_id);
+
+            DTU_Write(complete_json);
+            return;
+        }
+    // 发送进度响应
+    int progress = total_file_size > 0 ? (bytes_received * 100 / total_file_size) : 0;
+    char progress_json[256];
+    rt_snprintf(progress_json, sizeof(progress_json),
+               "{\"jsonrpc\":\"2.0\",\"method\":\"upgradehmi\","
+               "\"params\":{\"received\":%d,\"total\":%d,\"progress\":%d},\"id\":%d}",
+               bytes_received, total_file_size, progress, req_id);
+
+    DTU_Write(progress_json);
+}
+#endif
 
 /*入口函数*/
 static void Hmi_thread_entry(void *parameter)
@@ -150,7 +364,18 @@ static void Hmi_thread_entry(void *parameter)
                 rx_buffer[rx_length] = '\0';
                 rt_kprintf("%s\n",rx_buffer);
                 //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
-                HMI_Decode();
+                if(rx_length==1&&rx_buffer[rx_length-1]==0x05){
+                    LOG_W("屏幕响应信号!");
+                    if(current_state==STATE_COMPLETED) return;
+                    //RTU可以发送升级文件了
+                    char* upgrade_hmi = makeJsonRpc2_0("upgradehmi",NULL,1);
+                    if(upgrade_hmi){
+                        DTU_Write(upgrade_hmi);
+                        free(upgrade_hmi);
+                    }
+                }else {
+                    HMI_Decode();
+                }
         }
         /*********************************************************************************************************************/
         //rt_kprintf("%04d-%02d-%02d %02d:%02d:%02d\n",tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
@@ -185,7 +410,6 @@ static void HMI_Decode(void)
     gmtime_r(&now, &tm);
     char *json_temp;
     char *var=NULL;
-    rt_free(var);
     if(device_status.refresh_num==0)  device_status.refresh_num = 10;
     if(strstr(rx_buffer,"getFullData") != NULL){
         char *readinfo =  makeJson_ReadInfo();
@@ -197,7 +421,7 @@ static void HMI_Decode(void)
             device_status.hmi_fresh = 1;
             rt_thread_delay(100);
         }
-        //在解析getinfo中清除超时
+        //在解析getinfo中清除超时0
         if(device_status.refresh_timeout==30)
         {
               rt_sprintf(device_status.sim,"1");
@@ -1466,6 +1690,7 @@ static void HMI_Decode(void)
         device_status.selfcheck_ws = 2;
         LOG_W("开始自检:%d",device_status.selfcheck_ws);
     }
+    memset(rx_buffer,0,sizeof(rx_buffer));
 }
 
 
@@ -2014,6 +2239,7 @@ void Check_ParamConfig(void)
         {
             device_status.dattim = 10;
         }
+        if(device_status.sunBar==0) device_status.sunBar = 4;
         /*设备类型,7孢子仪,3测报灯,默认设置测报灯*/
         if(device_status.dtype!=7)
         {
@@ -2098,11 +2324,25 @@ void Check_ParamConfig(void)
         LOG_W("heat_sw加热开关:%d",device_status.heat_sw);
     }
     //RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
-    if(device_status.update_flag==1)
+    if(device_status.update_flag==1||device_status.update_flag==2)
     {
-        device_status.update_flag = 0;
-        ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
-        ymodem_ota(1,0);
+        if(device_status.update_flag==1){
+            device_status.update_flag = 0;
+            ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
+            ymodem_ota(1,0);
+        }else{
+            device_status.update_flag = 0;
+            ef_set_env_blob("update_flag", &device_status.update_flag, sizeof(device_status.update_flag));
+            // 准备参数
+            static char *args[] = {
+                "ymodem_ota",
+                "-p",
+                "hmi_area"
+            };
+            // 调用
+            ymodem_ota(3, args);
+        }
+
     }
     else
     {

+ 59 - 0
Application_CBD/applications/hmi.h

@@ -23,6 +23,65 @@ extern uint8_t DBG_EN;  //是否使能调试打印
 
 extern rt_timer_t timer_uart2;
 
+
+
+// 常量定义
+// 数据缓冲区
+#define RECV_BUFFER_SIZE   (10 * 1024)  // 10k接收缓冲区
+#define SEND_BUFFER_SIZE   4096           // 发送到HMI的缓冲区
+#define HMI_CHUNK_SIZE          4096    // 发送到屏幕的块大小
+#define JSON_CHUNK_SIZE         512    // JSON接收的块大小
+#define HMI_DEFAULT_BAUDRATE    115200
+#define HMI_ACK_BYTE            0x05
+#define HMI_TERMINATOR          "\xFF\xFF\xFF"
+#define HMI_RESPONSE_TIMEOUT    1000    // 响应超时(ms)
+#define HMI_TRANSFER_TIMEOUT    30000   // 传输超时(ms)
+uint32_t total_file_size;
+// 升级状态
+typedef enum {
+    STATE_IDLE = 0,
+    STATE_WAIT_SIZE,
+    STATE_WAIT_HMI_ACK,
+    STATE_RECEIVING,
+    STATE_SENDING_TO_HMI,
+    STATE_COMPLETED,
+    STATE_FAILED
+} upgrade_state_t;
+
+// 数据包管理
+typedef struct {
+    uint8_t *buffer;             // 数据缓冲区
+    uint32_t size;               // 缓冲区大小
+    uint32_t write_pos;          // 写入位置
+    uint32_t read_pos;           // 读取位置
+    uint32_t chunk_size;         // 分块大小(4096字节)
+} data_buffer_t;
+
+
+// 升级控制结构
+typedef struct {
+    rt_device_t rtu_serial;      // 连接RTU的串口
+    rt_device_t hmi_serial;      // 连接HMI的串口
+    rt_mutex_t lock;             // 互斥锁
+
+    upgrade_state_t state;           // 当前状态
+    uint32_t total_file_size;    // 总文件大小
+    uint32_t bytes_received;     // 已接收字节数
+    uint32_t bytes_sent_to_hmi;  // 已发送到HMI的字节数
+    uint32_t baudrate;           // 波特率
+
+    data_buffer_t recv_buffer;   // 接收缓冲区
+    data_buffer_t send_buffer;   // 发送缓冲区
+
+    rt_tick_t start_time;        // 开始时间
+    rt_tick_t timeout;           // 超时时间
+
+    uint8_t current_chunk[4096]; // 当前发送块
+    uint32_t chunk_filled;       // 当前块已填充大小
+} hmi_upgrade_protocol_t;
+void process_hex_payload(const char* hex_str, int req_id);
+
+
 void Check_ParamConfig(void);
 void HMI_WRITE(char *fmt,...);
 void Fresh_Hmi(void);

+ 1 - 1
Application_CBD/applications/lwjson_opt.h

@@ -135,7 +135,7 @@ extern "C" {
  * Primitives are all numbers and logical values (null, true, false)
  */
 #ifndef LWJSON_CFG_STREAM_PRIMITIVE_MAX_LEN
-#define LWJSON_CFG_STREAM_PRIMITIVE_MAX_LEN 32
+#define LWJSON_CFG_STREAM_PRIMITIVE_MAX_LEN 1024
 #endif
 
 /**

+ 1 - 1
Application_CBD/applications/lwjson_opts.h

@@ -35,7 +35,7 @@
 #define LWJSON_OPTS_HDR_H
 
 /* Rename this file to "lwjson_opts.h" for your application */
-
+#define LWJSON_CFG_STREAM_STRING_MAX_LEN 2048
 /*
  * Open "include/lwjson/lwjson_opt.h" and
  * copy & replace here settings you want to change values

+ 4 - 4
Application_CBD/drivers/fal_cfg.h

@@ -42,11 +42,11 @@ extern const struct fal_flash_dev stm32f1_onchip_flash;
 */
 #define FAL_PART_TABLE                                                               \
 {                                                                                    \
-    {FAL_PART_MAGIC_WORD,        "bl",     "onchip_flash",                           0,                       200*1024,       0}, \
-    {FAL_PART_MAGIC_WORD,        "app",     "onchip_flash",                          200*1024,                312*1024,       0}, \
+    {FAL_PART_MAGIC_WORD,        "bl",     "onchip_flash",                           0,                       200*1024,      0}, \
+    {FAL_PART_MAGIC_WORD,        "app",     "onchip_flash",                          200*1024,                312*1024,      0}, \
     {FAL_PART_MAGIC_WORD, "easyflash",     FAL_USING_NOR_FLASH_DEV_NAME,             0,                       3*1024*1024,   0}, \
-    {FAL_PART_MAGIC_WORD,  "fm_area",     FAL_USING_NOR_FLASH_DEV_NAME,             3*1024*1024,             512*1024,      0}, \
-    {FAL_PART_MAGIC_WORD,  "df_area",     FAL_USING_NOR_FLASH_DEV_NAME,             3*1024*1024 + 512*1024,  512*1024,      0}, \
+    {FAL_PART_MAGIC_WORD,  "fm_area",     FAL_USING_NOR_FLASH_DEV_NAME,             3*1024*1024,              512*1024,      0}, \
+    {FAL_PART_MAGIC_WORD,  "df_area",     FAL_USING_NOR_FLASH_DEV_NAME,             3*1024*1024 + 512*1024,   512*1024,      0}, \
 }
 #endif /* FAL_PART_HAS_TABLE_CFG */
 

+ 1 - 1
Application_CBD/readme.md

@@ -4,7 +4,7 @@
 	#define VERSION_ZP          "1.61.1(ZP)"            //1.0转盘
 	#define VERSION_GK          "1.61.1(GK)"            //1.0高空
 	1. 串口驱动更新,引入lwjson库接收json数据;
-
+	2. 增加屏幕固件更新功能;
 
 # 2025-10-21 更新说明
 	清扫推杆输出时间由40s改为50s