| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- /*
- * 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 <rtdbg.h>
- #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<CMD_Find_Cnt;i++)
- {
- //rt_kprintf("i%d:%c\t",i,CMD_Find_Buf[i]);
- if(CMD_Find_Buf[i+0] == 'c' && CMD_Find_Buf[i+1] == 'm' && \
- CMD_Find_Buf[i+2] == 'd' && CMD_Find_Buf[i+3] == '_' && \
- CMD_Find_Buf[i+4] == 's' && CMD_Find_Buf[i+6] == ':')
- {
- break;
- }
- }
- cmdcnt = str2hex((uint8_t *)(CMD_Find_Buf + i+8), (uint8_t *)cmdhex);
- uint16_t crc = CRC16_Modbus(cmdhex, cmdcnt);
- cmdhex[cmdcnt++] = (uint8_t)(crc >> 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;
- }
|