|
|
@@ -54,15 +54,23 @@ rt_thread_t dtu_send_data = RT_NULL;
|
|
|
|
|
|
|
|
|
rt_device_t dtuserial; /*串口设备句柄*/
|
|
|
-struct serial_configure dtuconfig = RT_SERIAL_CONFIG_DEFAULT; /*初始化配置参数*/
|
|
|
|
|
|
static uint8_t makejson_sta = 0;
|
|
|
|
|
|
extern eMBMasterReqErrCode _485_play(USHORT *Buffer,USHORT NREGS);
|
|
|
|
|
|
+
|
|
|
static int DTU_uart2_init(void)
|
|
|
{
|
|
|
+ struct serial_configure dtuconfig = RT_SERIAL_CONFIG_DEFAULT;
|
|
|
dtuserial = rt_device_find(DTU_UART_NAME);
|
|
|
+ if(dtuserial==RT_NULL)
|
|
|
+ {
|
|
|
+ LOG_E("Find Device:%s Failed!",DTU_UART_NAME);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ rt_device_close(dtuserial);
|
|
|
+#ifdef RT_USING_SERIAL_V2
|
|
|
/*修改串口配置参数*/
|
|
|
dtuconfig.baud_rate = BAUD_RATE_115200; //修改波特率9600
|
|
|
dtuconfig.data_bits = DATA_BITS_8; //数据位8c
|
|
|
@@ -73,21 +81,45 @@ static int DTU_uart2_init(void)
|
|
|
#else
|
|
|
dtuconfig.bufsz = 1024; //缓冲区1k
|
|
|
#endif
|
|
|
+
|
|
|
dtuconfig.parity = PARITY_NONE; //奇校验
|
|
|
+#endif
|
|
|
+ #ifdef RT_USING_SERIAL_X
|
|
|
+ rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
|
|
|
+ #else
|
|
|
/* step3:控制串口设备。通过控制接口传入命令控制字,与控制参数 */
|
|
|
rt_device_control(dtuserial, RT_DEVICE_CTRL_CONFIG, &dtuconfig);
|
|
|
+ #endif
|
|
|
+
|
|
|
/* step4:打开串口设备。以DMA接收及轮询发送模式打开串口设备 */
|
|
|
+ #ifdef RT_USING_SERIAL_X
|
|
|
+ if (rt_device_open(dtuserial, RT_DEVICE_OFLAG_RDWR
|
|
|
+ #ifdef RT_SERIAL_USING_DMA
|
|
|
+ | RT_DEVICE_FLAG_DMA_RX
|
|
|
+ | RT_DEVICE_FLAG_DMA_TX
|
|
|
+ #else
|
|
|
+ | RT_DEVICE_FLAG_INT_RX
|
|
|
+ | RT_DEVICE_FLAG_INT_TX
|
|
|
+ #endif
|
|
|
+ ) != RT_EOK)
|
|
|
+ {
|
|
|
+ LOG_E("Open device: %s failed\n", DTU_UART_NAME);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+#endif
|
|
|
#ifdef RT_USING_SERIAL_V2
|
|
|
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
|
|
|
if(uart_res!=RT_EOK)
|
|
|
{
|
|
|
LOG_E("%s打开失败!",DTU_UART_NAME);
|
|
|
}else{
|
|
|
LOG_I("%s Open Success!",DTU_UART_NAME);
|
|
|
}
|
|
|
+ #endif
|
|
|
+ #ifdef RT_USING_SERIAL_V1
|
|
|
+ rt_err_t uart_res = rt_device_open(dtuserial, RT_DEVICE_FLAG_DMA_RX); //RT_DEVICE_FLAG_DMA_RX
|
|
|
+ #endif
|
|
|
+
|
|
|
return RT_EOK;
|
|
|
}
|
|
|
|
|
|
@@ -128,151 +160,30 @@ static rt_err_t dtu_input(rt_device_t dev, rt_size_t size)
|
|
|
if ( result == -RT_EFULL)
|
|
|
{
|
|
|
/* 消息队列满 */
|
|
|
- rt_kprintf("message queue full!\n");
|
|
|
+ LOG_E("message queue full!\n");
|
|
|
rt_mq_control(&rx_mq_dtu, RT_IPC_CMD_RESET, RT_NULL); // 清空队列
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-// 最大JSON缓冲区长度(建议根据设备内存调整)
|
|
|
-#define MAX_JSON_BUFFER 1024
|
|
|
-// 最大超时时间(单位:RT_TICK_PER_SECOND)
|
|
|
-#define JSON_PARSE_TIMEOUT 2
|
|
|
-
|
|
|
-// 全局缓冲区结构体
|
|
|
-static struct {
|
|
|
- char buffer[MAX_JSON_BUFFER]; // 数据存储区
|
|
|
- int index; // 当前写入位置
|
|
|
- rt_tick_t last_received; // 最后接收时间戳
|
|
|
- int nested_level; // 当前嵌套层数
|
|
|
-} json_ctx = {.index = 0, .last_received = 0, .nested_level = 0};
|
|
|
-
|
|
|
-
|
|
|
-// 自定义异常类型
|
|
|
-typedef enum {
|
|
|
- JSON_OK,
|
|
|
- JSON_INCOMPLETE,
|
|
|
- JSON_INVALID_CHAR,
|
|
|
- JSON_TOO_DEEP,
|
|
|
- JSON_TOO_LONG
|
|
|
-} json_status_t;
|
|
|
-
|
|
|
-// 查找完整JSON边界(支持嵌套)
|
|
|
-static json_status_t find_json_boundary(int *start, int *end)
|
|
|
-{
|
|
|
- *start = -1;
|
|
|
- *end = -1;
|
|
|
- int level = 0;
|
|
|
-
|
|
|
- for (int i = 0; i < json_ctx.index; i++) {
|
|
|
- if (json_ctx.buffer[i] == '{') {
|
|
|
- if (level == 0) *start = i;
|
|
|
- level++;
|
|
|
- if (level > 32) return JSON_TOO_DEEP;
|
|
|
- } else if (json_ctx.buffer[i] == '}') {
|
|
|
- level--;
|
|
|
- if (level < 0) return JSON_INVALID_CHAR;
|
|
|
- if (level == 0 && *start != -1) {
|
|
|
- *end = i;
|
|
|
- return JSON_OK;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (*start != -1) return JSON_INCOMPLETE;
|
|
|
- return JSON_OK;
|
|
|
-}
|
|
|
-// 处理缓冲区超时
|
|
|
-static void check_timeout_reset(void)
|
|
|
-{
|
|
|
- if (rt_tick_get() - json_ctx.last_received > JSON_PARSE_TIMEOUT * RT_TICK_PER_SECOND) {
|
|
|
- rt_memset(json_ctx.buffer, 0, MAX_JSON_BUFFER);
|
|
|
- json_ctx.index = 0;
|
|
|
- json_ctx.nested_level = 0;
|
|
|
- rt_kprintf("[JSON] Timeout reset\n");
|
|
|
- }
|
|
|
-}
|
|
|
-#define RT_MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
-// 核心处理函数
|
|
|
-void process_uart_data(const char *data, int length)
|
|
|
-{
|
|
|
- // 有效性检查
|
|
|
- if (data == NULL || length <= 0) return;
|
|
|
- json_ctx.last_received = rt_tick_get();
|
|
|
-
|
|
|
- // 缓冲区溢出保护
|
|
|
- int remain_space = MAX_JSON_BUFFER - json_ctx.index - 1;
|
|
|
- if (remain_space < length) {
|
|
|
- rt_kprintf("[JSON] Buffer overflow (%d/%d)\n",
|
|
|
- json_ctx.index + length, MAX_JSON_BUFFER);
|
|
|
- json_ctx.index = 0; // 清空缓冲区重新开始
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 追加新数据
|
|
|
- rt_memcpy(json_ctx.buffer + json_ctx.index, data, length);
|
|
|
- json_ctx.index += length;
|
|
|
- json_ctx.buffer[json_ctx.index] = '\0';
|
|
|
-
|
|
|
- // 循环处理所有完整报文
|
|
|
- while (1) {
|
|
|
- int start_pos, end_pos;
|
|
|
- json_status_t status = find_json_boundary(&start_pos, &end_pos);
|
|
|
-
|
|
|
- switch (status) {
|
|
|
- case JSON_OK:
|
|
|
- if (start_pos != -1 && end_pos != -1) {
|
|
|
- // 提取有效JSON内容
|
|
|
- char json_str[512] = {0};
|
|
|
- int json_len = end_pos - start_pos + 1;
|
|
|
- rt_strncpy(json_str, json_ctx.buffer + start_pos,
|
|
|
- RT_MIN(sizeof(json_str)-1, json_len));
|
|
|
-
|
|
|
- // 调用解析函数
|
|
|
- if (parseDtu(json_str) == RT_EOK) {
|
|
|
- rt_kprintf("[JSON] Parsed: %.*s\n", json_len, json_str);
|
|
|
- }
|
|
|
-
|
|
|
- // 移动缓冲区剩余内容
|
|
|
- int remain_data = json_ctx.index - (end_pos + 1);
|
|
|
- if (remain_data > 0) {
|
|
|
- rt_memmove(json_ctx.buffer,
|
|
|
- json_ctx.buffer + end_pos + 1,
|
|
|
- remain_data);
|
|
|
- }
|
|
|
- json_ctx.index = remain_data;
|
|
|
- json_ctx.buffer[json_ctx.index] = '\0';
|
|
|
- } else {
|
|
|
- return; // 无更多完整报文
|
|
|
- }
|
|
|
- break;
|
|
|
-
|
|
|
- case JSON_INCOMPLETE:
|
|
|
- return; // 等待更多数据
|
|
|
-
|
|
|
- default:
|
|
|
- rt_kprintf("[JSON] Error %d at pos %d\n", status, start_pos);
|
|
|
- // 错误恢复:跳过错误数据
|
|
|
- json_ctx.index = 0;
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/* 入口函数-指令接收*/
|
|
|
static void dtu_thread_entry_recv(void *parameter)
|
|
|
{
|
|
|
struct rx_msg msg;
|
|
|
rt_memset(&msg, 0, sizeof(msg));
|
|
|
- static int rx_err = 0;
|
|
|
+
|
|
|
+ rt_err_t result;
|
|
|
+ rt_uint32_t rx_length;
|
|
|
+ rt_uint32_t rx_err = 0;
|
|
|
+ static char str_check[2]={'{','}'};
|
|
|
+ static char rx_buffer[512];
|
|
|
+ static char rx_buffer_tmp[512];
|
|
|
+ static char rx_buffer_tmp2[512];
|
|
|
while(1)
|
|
|
{
|
|
|
- //
|
|
|
+ result = rt_mq_recv(&rx_mq_dtu, &msg, sizeof(struct rx_msg), 50);
|
|
|
+ #if 0
|
|
|
// 从消息队列中获取被回调函数放入消息队列中的内容
|
|
|
- int result = rt_mq_recv(&rx_mq_dtu, &msg, sizeof(struct rx_msg), 50);
|
|
|
if (result != RT_EOK){
|
|
|
rx_err ++;
|
|
|
if(rx_err==15){
|
|
|
@@ -322,7 +233,8 @@ static void dtu_thread_entry_recv(void *parameter)
|
|
|
}
|
|
|
process_uart_data(rx_buffer, rx_length);
|
|
|
rt_free(rx_buffer);
|
|
|
- #if 0
|
|
|
+ #endif
|
|
|
+ #if 1
|
|
|
if(result == RT_EOK)
|
|
|
{
|
|
|
|
|
|
@@ -334,17 +246,11 @@ static void dtu_thread_entry_recv(void *parameter)
|
|
|
LOG_D("recv data:%s",rx_buffer);
|
|
|
if(strstr(rx_buffer,"{")==RT_NULL||strstr(rx_buffer,"}")==RT_NULL)
|
|
|
{
|
|
|
- if(device_status.dtype==7)
|
|
|
- {
|
|
|
- HMI_WRITE("sysTest.result.txt=\"%s\"",rx_buffer);
|
|
|
- }
|
|
|
-
|
|
|
if(device_status.dtype==3)
|
|
|
{
|
|
|
HMI_WRITE("sysSet.va0.txt=\"%s\"",rx_buffer);
|
|
|
rt_timer_stop(timer_uart2);
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
//过滤异常数据
|
|
|
@@ -372,16 +278,26 @@ static void dtu_thread_entry_recv(void *parameter)
|
|
|
rt_memset(rx_buffer_tmp,0,sizeof(rx_buffer_tmp));
|
|
|
rt_memset(rx_buffer_tmp2,0,sizeof(rx_buffer_tmp2));
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- #endif
|
|
|
+
|
|
|
}
|
|
|
-// else {
|
|
|
-//
|
|
|
-//
|
|
|
-// }
|
|
|
+ else {
|
|
|
+ rx_err ++;
|
|
|
+ if(rx_err==54000)//90分钟未接收到数据
|
|
|
+ {
|
|
|
+ ctr_RTU_DTU(OFF);
|
|
|
+ }
|
|
|
+ else if (rx_err>54100&&device_status.bat_status==0)
|
|
|
+ {
|
|
|
+ LOG_W("通信中断,RTU重新上电");
|
|
|
+ rx_err = 0;
|
|
|
+ ctr_RTU_DTU(ON);
|
|
|
+ ctr_photo(ON);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endif
|
|
|
}
|
|
|
-
|
|
|
// 解析时间戳并返回一个tm结构体
|
|
|
struct tm* parseTimestamp(const char *timestamp) {
|
|
|
if (strlen(timestamp) != 14) {
|
|
|
@@ -459,7 +375,7 @@ static void dtu_thread_entry_send(void *parameter)
|
|
|
static struct tm now_tm; //现在
|
|
|
//获取当前时间戳
|
|
|
now_tm =*localtime(&now_t);
|
|
|
- if(atoi(device_status.net)==0||atoi(device_status.gps)==1||(now_tm.tm_year+1900 >=2024)) //当前时间大于2024
|
|
|
+ if(atoi(device_status.net)==0||atoi(device_status.gps)==1||(now_tm.tm_year+1900 >=2025)) //当前时间大于2024
|
|
|
{
|
|
|
if(device_status.rain_status==1||device_status.temp_status==1)
|
|
|
{
|
|
|
@@ -1361,17 +1277,20 @@ int parseDtu(char *json_string)
|
|
|
cJSON* link_sta = NULL;
|
|
|
cJSON* link_ip = NULL;
|
|
|
cJSON* value_commn= NULL;
|
|
|
- rt_kprintf("%s\n",json_string);
|
|
|
+
|
|
|
cJSON *root = cJSON_Parse(json_string);
|
|
|
if(!root)
|
|
|
{
|
|
|
cJSON_Delete(root);
|
|
|
- rt_kprintf("parse json err:%s",json_string);
|
|
|
+ LOG_E("parse json err:%s",json_string);
|
|
|
return -1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
-
|
|
|
+ LOG_W("parseData:%s\n",json_string);
|
|
|
+ #if UART_TEST_EN == 1
|
|
|
+ DTU_Write(json_string);
|
|
|
+ #endif
|
|
|
item_cmd = cJSON_GetObjectItem(root,"cmd");
|
|
|
cJSON* item_aplay = cJSON_GetObjectItem(root,"aplay");
|
|
|
item_checkself = cJSON_GetObjectItem(root,"selfcheck");
|