|
|
@@ -42,8 +42,6 @@
|
|
|
#define CONTROL_MOTOR_TIMESLICE 5
|
|
|
|
|
|
|
|
|
-static char msg_pool[512];
|
|
|
-
|
|
|
static char rx_buffer[HMI_UART_RXBUFFER_SIZE + 1];
|
|
|
|
|
|
void list_mem(void);
|
|
|
@@ -72,9 +70,11 @@ rt_timer_t timer_uart2 = RT_NULL;
|
|
|
|
|
|
/* 消息队列控制块 */
|
|
|
static struct rt_messagequeue rx_mq;//接收数据消息队列
|
|
|
+static struct rt_semaphore rx_sem_uart4; /* 用于接收消息的信号量 */
|
|
|
+
|
|
|
static void HMI_Decode(void);
|
|
|
static rt_err_t uart_input(rt_device_t dev, rt_size_t size);
|
|
|
-static int HMI_uart4_init(void)
|
|
|
+int HMI_uart4_init(void)
|
|
|
{
|
|
|
serial = rt_device_find(UART_NAME);
|
|
|
/*修改串口配置参数*/
|
|
|
@@ -94,7 +94,8 @@ static int HMI_uart4_init(void)
|
|
|
//rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
|
|
|
/* 以 DMA 接收及轮询发送模式打开串口设备 */
|
|
|
#ifdef RT_USING_SERIAL_V2
|
|
|
- rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
|
|
|
+ rt_device_close(serial);
|
|
|
+ rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
|
|
|
#else
|
|
|
rt_err_t uart_res = rt_device_open(serial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
|
|
|
#endif
|
|
|
@@ -107,6 +108,12 @@ static int HMI_uart4_init(void)
|
|
|
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
+
|
|
|
+void close_uart4(void)
|
|
|
+{
|
|
|
+ rt_device_close(serial);
|
|
|
+}
|
|
|
+
|
|
|
/*串口屏写数据*/
|
|
|
void HMI_WRITE(char *fmt, ...)
|
|
|
{
|
|
|
@@ -130,19 +137,9 @@ void HMI_WRITE(char *fmt, ...)
|
|
|
/* 接收数据回调函数 */
|
|
|
static rt_err_t uart_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, &msg, sizeof(msg));
|
|
|
- if ( result == -RT_EFULL)
|
|
|
- {
|
|
|
- /* 消息队列满 */
|
|
|
- LOG_E("hmi message queue full!\n");
|
|
|
- rt_mq_control(&rx_mq, RT_IPC_CMD_RESET, RT_NULL); // 清空队列
|
|
|
- }
|
|
|
- return result;
|
|
|
+ /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
|
|
|
+ rt_sem_release(&rx_sem_uart4);
|
|
|
+ return RT_EOK;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -150,43 +147,77 @@ static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
|
|
|
static void Hmi_thread_entry(void *parameter)
|
|
|
{
|
|
|
|
|
|
- struct rx_msg msg;
|
|
|
- rt_err_t result;
|
|
|
- rt_uint32_t rx_length;
|
|
|
+ static char accum_buf[128]; // 累积缓冲区(根据实际数据量调整)
|
|
|
+ static size_t accum_len = 0; // 累积缓冲区当前长度
|
|
|
+ char ch;
|
|
|
+
|
|
|
+ // 【新增】超时计数,防止硬件持续发送不带 \r 的垃圾数据导致缓冲区填满后死锁
|
|
|
+ // 如果连续收到 N 个字节都没遇到 \r,强制清空缓冲区
|
|
|
+ const size_t MAX_NO_CR_COUNT = sizeof(accum_buf) - 1;
|
|
|
+ size_t no_cr_count = 0;
|
|
|
+
|
|
|
if(device_status.dtype==3){
|
|
|
device_status.collt_box_sta = 0;
|
|
|
device_status.run_sta = 0;
|
|
|
}
|
|
|
device_status.camsta = 1;
|
|
|
- rt_uint32_t recv_t = 0;
|
|
|
while(1)
|
|
|
{
|
|
|
|
|
|
- rt_memset(&msg, 0, sizeof(msg));
|
|
|
- /* 从消息队列中读取消息*/
|
|
|
- result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), 50);
|
|
|
- if (result == RT_EOK)
|
|
|
+ /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
|
|
|
+ while (rt_device_read(serial, -1, &ch, 1) != 1)
|
|
|
{
|
|
|
+ /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
|
|
|
+ rt_sem_take(&rx_sem_uart4, RT_WAITING_FOREVER);
|
|
|
+ }
|
|
|
+ if(ch=='\r'){
|
|
|
+ // 添加字符串结束符,方便后续字符串函数处理
|
|
|
+ if (accum_len < sizeof(accum_buf)) {
|
|
|
+ accum_buf[accum_len] = '\0';
|
|
|
+
|
|
|
+ // 只有当数据长度大于0时才处理(避免处理空的 "\r")
|
|
|
+ if (accum_len > 0) {
|
|
|
+ // 【重要】如果你的 HMI_Decode() 依赖全局变量 rx_buffer,需要拷贝过去
|
|
|
+ // 假设 rx_buffer 足够大
|
|
|
+ if (accum_len < sizeof(rx_buffer)) {
|
|
|
+ rt_memcpy(rx_buffer, accum_buf, accum_len + 1); // +1 是为了拷贝 '\0'
|
|
|
+ HMI_Decode();
|
|
|
+ } else {
|
|
|
+ LOG_W("Packet too long for global buffer");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- 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结尾
|
|
|
- HMI_Decode();
|
|
|
- recv_t = 0;
|
|
|
+ // 重置状态,准备接收下一包
|
|
|
+ accum_len = 0;
|
|
|
+ no_cr_count = 0;
|
|
|
}else{
|
|
|
- recv_t++;
|
|
|
- if(recv_t==20*60*3) //持续3分钟无数据,关闭屏幕电源
|
|
|
- {
|
|
|
- recv_t = 0;
|
|
|
- //ctr_hmi_5v(OFF);
|
|
|
+ // --- 普通字符,存入缓冲区 ---
|
|
|
+
|
|
|
+ // 【修正点】这里必须把 ch 放入 accum_buf!
|
|
|
+ if (accum_len < sizeof(accum_buf) - 1) {
|
|
|
+ accum_buf[accum_len] = ch; // <=== 关键赋值语句
|
|
|
+ accum_len++;
|
|
|
+ no_cr_count++;
|
|
|
+ } else {
|
|
|
+ // 缓冲区已满仍未遇到 \r,视为异常包(溢出)
|
|
|
+ LOG_W("HMI Buffer Overflow! Dropping packet.");
|
|
|
+ accum_len = 0; // 清空计数器
|
|
|
+ no_cr_count = 0; // 重置异常计数
|
|
|
+ // 当前这个 ch 也丢弃,等待下一个包开始
|
|
|
+ // 或者你可以选择把 ch 作为新包的第一个字节:accum_buf[0]=ch; accum_len=1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 【防御机制】如果连续收到太多字节都没有 \r,强制复位
|
|
|
+ if (no_cr_count >= MAX_NO_CR_COUNT) {
|
|
|
+ LOG_W("No \\r detected for %d bytes, force reset.", no_cr_count);
|
|
|
+ accum_len = 0;
|
|
|
+ no_cr_count = 0;
|
|
|
}
|
|
|
}
|
|
|
- /*********************************************************************************************************************/
|
|
|
- //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);
|
|
|
- //rt_kprintf("workSta.vbat.txt=%c%d.%02d%c\n",hmi_str,voltage/100,voltage%100,hmi_str);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/*******************************************************************************************/
|
|
|
static void t_uart2_callback(void *parameter)
|
|
|
{
|
|
|
@@ -918,12 +949,8 @@ int Hmi_Fresh(void)
|
|
|
//初始化串口4
|
|
|
//HMI_uart4_init();
|
|
|
rt_err_t ret = RT_EOK;
|
|
|
- /* 初始化消息队列 */
|
|
|
- rt_mq_init(&rx_mq, "rx_mq",
|
|
|
- msg_pool, /* 存放消息的缓冲区 */
|
|
|
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
|
|
|
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
|
|
|
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
|
|
+ /* 初始化信号量 */
|
|
|
+ rt_sem_init(&rx_sem_uart4, "rx_sem_uart4", 0, RT_IPC_FLAG_FIFO);
|
|
|
/* 设置接收回调函数 */
|
|
|
rt_device_set_rx_indicate(serial, uart_input);
|
|
|
/* 创建线程*/
|
|
|
@@ -1104,6 +1131,8 @@ void Check_ParamConfig(void)
|
|
|
LOG_I("PowerType:%d",device_status.Power_Type);
|
|
|
LOG_I("deviceId:%s",device_status.deviceId);
|
|
|
LOG_I("sleep_mode:%d",device_status.sleep_mode);
|
|
|
+ LOG_I("imei:%s",device_status.imei);
|
|
|
+ LOG_I("iccid:%s",device_status.iccid);
|
|
|
|
|
|
}
|
|
|
//RTC时间判断,如果当前年份小于2020,提示需要设置时间,切换为光控
|
|
|
@@ -1269,7 +1298,7 @@ void ResetParamDefault_DC(void)
|
|
|
ef_set_env_blob("shake_sec", &device_status.shake_sec, sizeof(device_status.shake_sec)); //震动时间
|
|
|
ef_set_env_blob("thsp", &device_status.thsp, sizeof(device_status.thsp));
|
|
|
ef_set_env_blob("passWord", &device_status.passWord,sizeof(device_status.passWord));
|
|
|
- ef_set_env_blob("Power_Type", &device_status.passWord,sizeof(device_status.Power_Type));
|
|
|
+ ef_set_env_blob("Power_Type", &device_status.Power_Type,sizeof(device_status.Power_Type));
|
|
|
DTU_Write("reset.mp3");
|
|
|
rt_thread_delay(1000);
|
|
|
}
|
|
|
@@ -1283,11 +1312,7 @@ void Debug_log_OFF(void)
|
|
|
{
|
|
|
rt_console_set_device("uart1");
|
|
|
finsh_set_device("uart1");
|
|
|
- rt_mq_init(&rx_mq, "rx_mq",
|
|
|
- msg_pool, /* 存放消息的缓冲区 */
|
|
|
- sizeof(struct rx_msg), /* 一条消息的最大长度 */
|
|
|
- sizeof(msg_pool), /* 存放消息的缓冲区大小 */
|
|
|
- RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
|
|
|
+
|
|
|
rt_device_set_rx_indicate(serial, uart_input);
|
|
|
hmi1 = rt_thread_create("Hmi_Fresh_thread",
|
|
|
Hmi_thread_entry,
|