zhunk 8 months atrás
parent
commit
4387a0ee93

+ 22 - 6
Application_cbd_apm32f1/applications/control.c

@@ -114,11 +114,13 @@ void ctr_hmi_5v(uint8_t sw)
         rt_pin_write(POWER_5V,ON);
         rt_pin_write(SIGNAL_OUTPUT_LED,OFF);
         LOG_W("打开5V电源!");
+        device_status.hmi_status = 1;
     }
     else {
         rt_pin_write(POWER_5V,OFF);
         rt_pin_write(SIGNAL_OUTPUT_LED,ON);
         LOG_W("关闭5V电源!");
+        device_status.hmi_status = 0;
     }
 }
 
@@ -147,11 +149,13 @@ void ctr_RTU_12V(uint8_t sw)
     {
         LOG_W("RTU电源启动!!!");
         rt_pin_write(RTU_12V,ON);
+        device_status.rtu_status = 1;
     }
     else
     {
         LOG_W("RTU电源关闭!!!");
         rt_pin_write(RTU_12V,OFF);
+        device_status.rtu_status = 0;
     }
 }
 
@@ -180,8 +184,9 @@ void ResetInit(void)
     LOG_I("ResetInit...");
     //1.打开路由器供电
     ctr_router(ON);
-    //2. 打开RTU供电
-    //ctr_RTU_12V(ON);
+    ctr_hmi_5v(ON);
+    //2. RTU供电默认是开的,不能调用ctr_RTU_12V(ON);
+    device_status.rtu_status = 1;
     //3.打开光栅供电
     ctr_led(OFF);
     ctr_gaoya_12v(OFF);
@@ -291,8 +296,8 @@ void guanshan_on(void *args)
 void button_on(void *args)
 {
     LOG_W("button_on!");
-    if(device_status.sleep_mode==1) ctr_RTU_12V(ON);
-    ctr_hmi_5v(ON);
+    if(device_status.sleep_mode==1&&device_status.rtu_status==0) ctr_RTU_12V(ON);
+    if(device_status.hmi_status==0) ctr_hmi_5v(ON);
     //rt_event_send(event_485_play, 0x01);
 }
 
@@ -365,11 +370,9 @@ void WorkTask_XYV3(void)
             if(device_status.sleep_mode==1){
                 if(cal_n==100){
                     ctr_RTU_12V(ON);
-                    ctr_hmi_5v(ON);
                 }
                 else if (cal_n==3400) {
                     ctr_RTU_12V(OFF);
-                    ctr_hmi_5v(OFF);
                 }
             }
             if(gaoya_on){
@@ -416,8 +419,21 @@ static void Check_Self_thread_entry(void *parameter)
 {
 
     static int selfcheck_cnt = 0;
+    rt_uint32_t e;
+    event_485_play = rt_event_create("event", RT_IPC_FLAG_FIFO);
+    if (event_485_play == RT_NULL)
+    {
+        LOG_E("Event create failed!");
+        return;
+    }
     while(1)
     {
+        //等待事件
+        if(rt_event_recv(event_485_play, 0x01, RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e)==RT_EOK)
+        {
+            DTU_uart2_init();
+            HMI_uart4_init();
+        }
         if(device_status.selfcheck_ws==1){
             selfcheck_cnt++;
         }else{

+ 4 - 2
Application_cbd_apm32f1/applications/control.h

@@ -15,12 +15,12 @@
 #include <rtdevice.h>
 //定制相关//////////////////////////////////////////////////////
 
-#define SENSOR_EN        1             //温湿度:0关闭,1打开
+#define SENSOR_EN        0            //温湿度:0关闭,1打开
 
 
 ////////////////////////////////////////////////////////////////
 
-#define VERSION  "3.3.3"
+#define VERSION  "3.4.3"
 
 extern rt_event_t event_485_play;
 
@@ -104,6 +104,8 @@ typedef struct  _packed
     uint8_t gps_inuse;
     uint8_t gps_view;
     uint8_t sleep_mode;      //省电模式:0-关 1-开,> 1 超时时间
+    uint8_t rtu_status;
+    uint8_t hmi_status;
     uint16_t dattim;         //数据上传间隔
     uint16_t shake_sec;      //震动时间
     uint16_t thsp;           //息屏时间

+ 28 - 20
Application_cbd_apm32f1/applications/dtu_uart2.c

@@ -86,7 +86,7 @@ const char* insect_names[] = {
 };
 
 
-static int DTU_uart2_init(void)
+int DTU_uart2_init(void)
 {
     dtuserial  = rt_device_find(DTU_UART_NAME);
     /*修改串口配置参数*/
@@ -104,7 +104,8 @@ static int DTU_uart2_init(void)
     rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
     /* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
     #ifdef RT_USING_SERIAL_V2
-    rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
+    rt_device_close(dtuserial);
+    rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_NON_BLOCKING); // 串口设备使用模式为 (发送阻塞 接收非阻塞) 模式
     #else
     rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX);  //RT_DEVICE_FLAG_DMA_RX
     #endif
@@ -117,6 +118,10 @@ static int DTU_uart2_init(void)
     return RT_EOK;
 }
 
+void close_uart2(void)
+{
+    rt_device_close(dtuserial);
+}
 
 
 /*数据发送*/
@@ -1238,28 +1243,31 @@ int parseDtu(char *json_string)
                       //HMI_WRITE("menu.t0.txt=\"%s\"",link_ip->valuestring);
                   }
 
-                  if(strstr(device_status.imei,imei->valuestring)==RT_NULL)
+                  if(imei!=RT_NULL)
                   {
-                       rt_kprintf("Change IMEI:new:%s-old:%s",imei->valuestring,device_status.imei);
-                       rt_sprintf(device_status.imei, "%s",imei->valuestring);
-                       ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei));  //IMEI
+                      if(strstr(device_status.imei,imei->valuestring)==RT_NULL&&strcmp(imei->valuestring, "") != 0)
+                      {
+                          LOG_I("Change IMEI:new:%s-old:%s",imei->valuestring,device_status.imei);
+                           rt_sprintf(device_status.imei, "%s",imei->valuestring);
+                           ef_set_env_blob("imei", &device_status.imei, sizeof(device_status.imei));  //IMEI
+                      }
+                      else {
+                          LOG_I("IMEI not change:%s-%s",imei->valuestring,device_status.imei);
+                      }
                   }
-                  else {
-                      rt_sprintf(device_status.imei,"%s",imei->valuestring);
-                      rt_kprintf("IMEI not change:%s-%s",imei->valuestring,device_status.imei);
-                 }
-                  //如果DTU的ICCID和FLash中的不一样,保存到FLASH
-                  if(strstr(device_status.iccid,iccid->valuestring)==RT_NULL)
+                  if(iccid!=RT_NULL)
                   {
-                      rt_kprintf("Change ICCID:new:%s-old:%s",iccid->valuestring,device_status.iccid);
-                      rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
-                      ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid));  //Iccid
-                  }
-                  else {
-                      rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
-                      rt_kprintf("ICCID not change:%s-%s",iccid->valuestring,device_status.iccid);
+                      //如果DTU的ICCID和FLash中的不一样,保存到FLASH
+                      if(strstr(device_status.iccid,iccid->valuestring)==RT_NULL&&strcmp(iccid->valuestring, "") != 0)
+                      {
+                          LOG_I("Change ICCID:new:%s-old:%s",iccid->valuestring,device_status.iccid);
+                          rt_sprintf(device_status.iccid,"%s",iccid->valuestring);
+                          ef_set_env_blob("iccid", &device_status.iccid, sizeof(device_status.iccid));  //Iccid
+                      }
+                      else {
+                          LOG_I("ICCID not change:%s-%s",iccid->valuestring,device_status.iccid);
+                      }
                   }
-
                   if(datetime!=RT_NULL){
                       LOG_I("DTU时间:%s",datetime->valuestring);
                       struct tm *tm_time = parseTimestamp(datetime->valuestring);

+ 2 - 1
Application_cbd_apm32f1/applications/dtu_uart2.h

@@ -25,7 +25,8 @@ typedef struct{
 //};
 
 
-
+int DTU_uart2_init(void);
+void close_uart2(void);
 
 void dtu_send_data_start(void);
 //查询数据---{"cmd": "read"}

+ 76 - 51
Application_cbd_apm32f1/applications/hmi.c

@@ -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,

+ 4 - 0
Application_cbd_apm32f1/applications/hmi.h

@@ -22,6 +22,10 @@ extern uint8_t dbg_flag;
 extern uint8_t DBG_EN;  //是否使能调试打印
 
 extern rt_timer_t timer_uart2;
+
+int HMI_uart4_init(void);
+void close_uart4(void);
+
 void Check_ParamConfig(void);
 void HMI_WRITE(char *fmt,...);
 void Fresh_Hmi(void);

+ 2 - 0
Application_cbd_apm32f1/applications/sample_mb_master.c

@@ -60,6 +60,7 @@ static void send_thread_entry(void *parameter)
     BOOL IN_3A = 0;           //数据解析方式设置
     eMBMasterReqErrCode error_code = MB_MRE_NO_ERR;
     rt_uint16_t error_count = 0;
+    rt_uint16_t ok_count = 0;
     while (1)
     {
 
@@ -95,6 +96,7 @@ static void send_thread_entry(void *parameter)
             {
                 device_status.sensor_485_sta = 100;
                 error_count = 0;
+                ok_count++;
                 //温度=25并且湿度=30不更新,否则会导致屏幕上显示空
     //            if((int)usMRegHoldBuf[0][0]/10!=25&&(int)usMRegHoldBuf[0][1]/10!=30)
     //            {

+ 26 - 1
Application_cbd_apm32f1/drivers/drv_usart_v2.c

@@ -14,6 +14,7 @@
 #ifdef RT_USING_SERIAL_V2
 
 //#define DRV_DEBUG
+#define DBG_ENABLE
 #define DBG_TAG              "drv.usart"
 #ifdef DRV_DEBUG
 #define DBG_LVL               DBG_LOG
@@ -289,13 +290,37 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
     return RT_EOK;
 }
 
+
 static int stm32_putc(struct rt_serial_device *serial, char c)
 {
     struct stm32_uart *uart;
+    rt_tick_t start_tick;
+
     RT_ASSERT(serial != RT_NULL);
 
     uart = rt_container_of(serial, struct stm32_uart, serial);
-    while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET);
+
+    start_tick = rt_tick_get();
+
+    /* 等待发送完成 TC */
+    while (__HAL_UART_GET_FLAG(&(uart->handle), UART_FLAG_TC) == RESET)
+    {
+        /* 5ms超时 */
+        if (rt_tick_get() - start_tick > rt_tick_from_millisecond(1000))
+        {
+            LOG_E("UART TX timeout, reset uart");
+
+            /* 清标志 */
+            __HAL_UART_CLEAR_FLAG(&(uart->handle), UART_FLAG_TC);
+
+//            /* 复位 UART */
+//            HAL_UART_DeInit(&uart->handle);
+//            HAL_UART_Init(&uart->handle);
+
+            return 0;
+        }
+    }
+
     UART_INSTANCE_CLEAR_FUNCTION(&(uart->handle), UART_FLAG_TC);
     UART_SET_TDR(&uart->handle, c);
 

+ 2 - 5
Application_cbd_apm32f1/packages/syswatch-latest/src/syswatch.c

@@ -414,6 +414,7 @@ static void syswatch_thread_entry(void *parameter)
                     {
                         ctr_router(OFF);
                         ctr_RTU_12V(OFF);
+                        ctr_hmi_5v(OFF);         //屏幕供电
                     }
                 }
 
@@ -425,13 +426,9 @@ static void syswatch_thread_entry(void *parameter)
                 {
                     ctr_router(ON);
                     ctr_RTU_12V(ON);
-                    //ctr_hmi_5v(ON);         //屏幕供电
+                    ctr_hmi_5v(ON);         //屏幕供电
                 }
             }
-        }else if(device_status.sleep_mode==1){
-            if(device_status.work_status==0){
-
-            }
         }
         #endif
         rt_thread_delay(RT_TICK_PER_SECOND);

+ 6 - 0
Application_cbd_apm32f1/readme.md

@@ -1,3 +1,9 @@
+# 2026-03-12 更新说明
+	版本:3.4.3
+	1. 屏幕串口驱动使用信号量接收数据,取消使用消息队列
+	2. 解析IMEI时增加判空操作,解决传递过来的空IMEI替换已保存的IMEI,ICCID也一样;
+	3. 工作状态下休眠不关闭屏幕
+	4. 修改串口的打开模式为发送非阻塞,否则断电RTU时会导致串口异常,无法收发数据;
 # 2026-03-11 更新说明
 	版本:3.3.3
 	1. 关闭屏幕供电会导致rtu12V供电同时关闭,故增加休眠模式:只在上传/拍照时提前通电,结束后延迟掉电