/* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2023-05-17 19813 the first version */ /*********************↓ define ↓************************/ //#define LOG_TAG "RC" //#define DBG_LVL DBG_LOG #define Sensor_PWR_ON HAL_GPIO_WritePin(CONTROL_12V_GPIO_Port, CONTROL_12V_Pin, 1) #define Sensor_PWR_OFF HAL_GPIO_WritePin(CONTROL_12V_GPIO_Port, CONTROL_12V_Pin, 0) /*********************↑ define ↑************************/ /** * * * * * */ /*********************↓ include ↓************************/ //#include #include "RC.h" #include "string.h" #include "modbus_host.h" /*********************↑ include ↑************************/ /** * * * * * */ /*********************↓ extern ↓************************/ extern uint16_t CRC16_Modbus(uint8_t *_pBuf, uint16_t _usLen); // 生成CRC16 extern uint8_t check_link(void); // 检查联网状态 extern void CMD_Find(void); // 执行命令 extern void MQTT_Send_Pub(uint8_t type); // 向pub发送数据 extern void RS485_SendBuf(uint8_t *_ucaBuf, uint16_t _usLen, UART_HandleTypeDef *uart); // 向485发送数据 extern uint8_t Valve_Mode; // 阀控模式 extern uint8_t Expand_Guard_T; // 用于扩容警戒线程的时间 extern uint8_t Wait_Cmd; // 检索命令循环圈数 extern uint8_t cache_buff[]; // 临时缓存 extern uint8_t CMD_Find_Flag; extern uint16_t CMD_Find_Cnt; extern uint8_t CMD_Find_Buf[]; extern MODH_T g_tModH; extern UART_HandleTypeDef huart2; /*********************↑ extern ↑************************/ /** * * * * * */ /********************↓ declaration ↓***********************/ uint8_t DTU_Service(void); uint8_t str2hex(uint8_t *str, uint8_t *bin); /********************↑ declaration ↑***********************/ /** * * * * * */ /*********************↓ global variables ↓************************/ volatile uint8_t RC_Mode = Zero; // 用于控制远程控制模式得进出 /*********************↑ global variables ↑************************/ /** * * * * * */ /**联网上报数据以后,(处理完遗嘱信息后),调用 * @brief 用于执行远程控制模式服务 * * @return uint8_t 返回值为0 则服务需继续执行 不为0则退出 */ extern void report_update(void); uint8_t RC_Service(void) { uint8_t res = 0; report_update(); /** * @brief 前置条件 使能远控模式 * */ if (RC_Mode != One) { return 1; } /** * @brief 前置条件 需要LTE模式 * */ // if (Node_Type != LTE) // { // return 3; // } rt_kprintf("RC ^"); Expand_Guard_T = One; // 扩容警戒时间 Sensor_PWR_ON; rt_kprintf(" \nRC ^Expand_Guard_T =%d\r\n",Expand_Guard_T); uint16_t CountdownMAX = 90; // 用于倒计时 uint32_t Countdown = 0; // 用于倒计时 uint16_t Base_Secson = 1000;// 计数基准 if (check_link() == 1) { rt_sprintf((char *)cache_buff, "RC in"); MQTT_Send_Pub(1); //check_link_flag = 1; } // 等待指令并响应 while ((Countdown < CountdownMAX * Base_Secson) && (RC_Mode == One)) { if (Countdown % (Base_Secson) == 0) // 隔段时间检查下在线情况 { rt_kprintf("RC %d/%d\n", Countdown / Base_Secson, CountdownMAX); if (check_link() != 1) // 确保在线 { //check_link_flag = 0; rt_thread_mdelay(10); if (check_link() != 1) { rt_thread_mdelay(10); Recode_e_code(28, 0); break; } //check_link_flag = 1; //check_link_oneflag = 1; } } Countdown++; rt_thread_mdelay(10); if (CMD_Find_Flag == 1 && CMD_Find_Cnt > 20) { Wait_Cmd = 1; DTU_Service(); //透传指令 CMD_Find(); //在线指令 } rt_thread_mdelay(10); } Sensor_PWR_OFF; if ((check_link() == 1) && (Countdown >= CountdownMAX * Base_Secson)) { rt_sprintf((char *)cache_buff, "RC timeout"); MQTT_Send_Pub(1); } return res; } /** * @brief 用于对485进行透传 * * @return uint8_t 1失败 0成功 */ uint8_t DTU_Service(void) { if (strstr((const char *)CMD_Find_Buf, "cmd_s") == NULL) { return 1; } uint8_t cmdhex[15] = {0}; // 存放hex的指令 uint8_t cmdcnt = 0; // hex个数 int i=0; for(i=70;i> 8); cmdhex[cmdcnt++] = (uint8_t)(crc & 0xFF); if (cmdcnt < 3) { rt_sprintf((char *)cache_buff, "format ×"); MQTT_Send_Pub(1); //format_uartbuf(5); return 1; } memset(g_tModH.RxBuf, 0, 64); g_tModH.RxCount = 0; RS485_SendBuf((uint8_t *)cmdhex, cmdcnt, &huart2); uint8_t a_time = 0; for (a_time = 0; a_time < 200; a_time++) { rt_thread_mdelay(10); if (g_tModH.RxCount > 3) { rt_thread_mdelay(30); break; } } memset(cache_buff, 0, 1024); if(g_tModH.RxCount == 0) { rt_sprintf((char *)cache_buff, "timeout"); } else { for (uint8_t a = 0; a < g_tModH.RxCount; a++) { rt_sprintf((char *)cmdhex, "%02X ", g_tModH.RxBuf[a]); strcat((char *)cache_buff, (const char *)cmdhex); if((a + 1) == g_tModH.RxCount) { if(a_time > 5) { a_time -= 5; //时间修正 } rt_sprintf((char *)cmdhex, "(%dms)", a_time * 10); strcat((char *)cache_buff, (const char *)cmdhex); } } } MQTT_Send_Pub(1); memset(g_tModH.RxBuf, 0, 64); g_tModH.RxCount = 0; return 0; } // 字符串转hex uint8_t str2hex(uint8_t *str, uint8_t *bin) { uint8_t count = 0; uint8_t one_byte = 0; for (uint8_t a = 0; a < strlen((const char *)str); a++) { one_byte = 0; if (str[a] != 0x20 && str[a + 1] != 0x20) { if (str[a] >= 0x30 && str[a] <= 0x39) { one_byte += (str[a] - 0x30) << 4; } else if (str[a] >= 0x41 && str[a] <= 0x46) { one_byte += (str[a] - 55) << 4; } else { return count; } if (str[a + 1] >= 0x30 && str[a + 1] <= 0x39) { one_byte += (str[a + 1] - 0x30); } else if (str[a + 1] >= 0x41 && str[a + 1] <= 0x46) { one_byte += (str[a + 1] - 55); } else { return count; } bin[count++] = one_byte; // rt_kprintf("%d byte : %d\n", count - 1, one_byte); a++; } } return count; }