|
|
@@ -27,6 +27,7 @@
|
|
|
#include <fal.h>
|
|
|
#include "selfcheck.h"
|
|
|
#include <user_mb_app.h>
|
|
|
+#include "sys_monitor.h"
|
|
|
#define DBG_ENABLE
|
|
|
#define DBG_COLOR
|
|
|
#define DBG_TAG "hmi"
|
|
|
@@ -52,6 +53,7 @@ void list_mem(void);
|
|
|
int mb_master_samlpe();
|
|
|
/**/
|
|
|
Device_Status_typedef device_status;
|
|
|
+System_stats_t system_status;
|
|
|
|
|
|
extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
|
|
|
|
|
|
@@ -72,6 +74,12 @@ 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 uint8_t recv_buffer[RECV_BUFFER_SIZE];
|
|
|
+static uint32_t recv_write_pos = 0;
|
|
|
+
|
|
|
/* 消息队列控制块 */
|
|
|
static struct rt_messagequeue rx_mq;//接收数据消息队列
|
|
|
static void HMI_Decode(void);
|
|
|
@@ -135,6 +143,7 @@ void HMI_WRITE(char *fmt, ...)
|
|
|
memcpy(str + str_length, hmi_hex, sizeof(hmi_hex));
|
|
|
rt_device_write(serial, 0, str, str_length + sizeof(hmi_hex));
|
|
|
rt_thread_mdelay(1); //2025-0308
|
|
|
+ system_status.uart4_tx_bytes+=str_length + sizeof(hmi_hex);
|
|
|
}
|
|
|
/* 接收数据回调函数 */
|
|
|
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
|
|
|
@@ -154,6 +163,124 @@ 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);
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
/*入口函数*/
|
|
|
static void Hmi_thread_entry(void *parameter)
|
|
|
@@ -174,12 +301,23 @@ static void Hmi_thread_entry(void *parameter)
|
|
|
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
|
|
|
if (result == RT_EOK)
|
|
|
{
|
|
|
-
|
|
|
- rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
|
|
|
- rx_buffer[rx_length] = '\0';
|
|
|
- //LOG_I("%s",rx_buffer);
|
|
|
- //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
|
|
|
+ rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
|
|
|
+ system_status.uart4_rx_bytes+=rx_length;
|
|
|
+ rx_buffer[rx_length] = '\0';
|
|
|
+ LOG_I("%s",rx_buffer);
|
|
|
+ //\r 对应的hex为0x0d,0x0a,占两个字节,屏幕发送的数据都以\r结尾
|
|
|
+ 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);
|
|
|
@@ -1371,6 +1509,7 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
{
|
|
|
ctr_cangmen_up(OFF);
|
|
|
num_up_open = 200;
|
|
|
+ system_status.upper_door_open_count +=1;
|
|
|
}
|
|
|
num_up_close = 200;
|
|
|
}
|
|
|
@@ -1382,6 +1521,7 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
{
|
|
|
ctr_cangmen_up(OFF);
|
|
|
num_up_close= 200;
|
|
|
+ system_status.upper_door_close_count +=1;
|
|
|
}
|
|
|
num_up_open = 200;
|
|
|
}
|
|
|
@@ -1405,6 +1545,7 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
{
|
|
|
ctr_cangmen_down(OFF);
|
|
|
num_down_open = 200;
|
|
|
+ system_status.lower_door_open_count +=1;
|
|
|
}
|
|
|
num_down_close = 200;
|
|
|
}
|
|
|
@@ -1417,6 +1558,7 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
{
|
|
|
ctr_cangmen_down(OFF);
|
|
|
num_down_close = 200;
|
|
|
+ system_status.lower_door_close_count += 1;
|
|
|
}
|
|
|
num_down_open = 200;
|
|
|
}
|
|
|
@@ -1435,6 +1577,7 @@ static void Ctr_Mortor_entry(void* parameter)
|
|
|
//打开补光灯
|
|
|
if(photo_led_sec==800)
|
|
|
{
|
|
|
+ system_status.photo_taken_count+=1;
|
|
|
LOG_I("测试拍照!");
|
|
|
#if 1
|
|
|
char *takephotojson = makeJson_takephoto();
|
|
|
@@ -1700,7 +1843,7 @@ void Check_ParamConfig(void)
|
|
|
{
|
|
|
device_status.dattim = 10;
|
|
|
}
|
|
|
-
|
|
|
+ if(device_status.thsp==0) device_status.thsp = 300;
|
|
|
if(device_status.clt_time==0) device_status.clt_time = 10;
|
|
|
if(device_status.heattime==0) device_status.heattime = 10;
|
|
|
if(device_status.tph==0&&device_status.tpl==0){ //参数丢失
|
|
|
@@ -1788,6 +1931,7 @@ void Check_ParamConfig(void)
|
|
|
rt_sprintf(device_status.net,"1");
|
|
|
rt_sprintf(device_status.csq,"0");
|
|
|
rt_sprintf(device_status.sim,"1");
|
|
|
+ Sys_Motor_Task();
|
|
|
#ifdef RT_USING_ALARM
|
|
|
alarm_app_router();
|
|
|
#endif
|