feat: RS485透传改为Modbus RTU Master轮询寄存器41161报警信号

This commit is contained in:
2026-05-27 17:15:57 +08:00
parent 0bc39b7944
commit 5318c35d37
17 changed files with 380 additions and 4373 deletions

View File

@ -40,6 +40,9 @@
#include "cmd_router.h"
#include "debug_log.h"
/* Modbus RTU Master 模块头文件 */
#include "modbus_rtu_master.h"
/* W5500 Ethernet模块头文件 */
#if USE_W5500
#include "user_main.h"
@ -86,12 +89,6 @@ static uint8_t u1_rx_buffer[256];
static volatile uint16_t u1_rx_len = 0;
static volatile uint32_t u1_last_rx_time = 0;
/* === 485 设备的接收缓存 (UART3) === */
static uint8_t u3_rx_buffer[512];
static volatile uint16_t u3_rx_len = 0;
static volatile uint32_t u3_last_rx_time = 0;
static volatile uint32_t u3_ignore_until = 0;
/* === 协议处理全局变量 === */
static uint16_t g_hb_seq = 0; /* 心跳序列号 */
static uint32_t g_last_hb_tick = 0; /* 上次心跳时间 */
@ -248,6 +245,7 @@ int main(void)
/* 启动UART3接收中断 - RS485接口 */
#if USE_RS485
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
ModbusRTU_Master_Init();
#endif
/* 初始化RF433模块 - 使用默认配置 */
@ -331,9 +329,8 @@ int main(void)
UART2_Print_Task();
MultiUART_Task();
/* === 2. 无线接收透传 (433 -> 485/Debug) === */
/* === 2. 无线接收透传 (433 -> Debug) === */
#if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH)
// 如果 433 收到无线数据,直接透传输出,不进行解码
if (u1_rx_len > 0 && (HAL_GetTick() - u1_last_rx_time > 20))
{
__disable_irq();
@ -341,7 +338,6 @@ int main(void)
u1_rx_len = 0;
__enable_irq();
MultiUART_Send(PORT_RS485, (uint8_t*)u1_rx_buffer, len);
MultiUART_Send(PORT_DEBUG, (uint8_t*)u1_rx_buffer, len);
}
#endif
@ -355,19 +351,9 @@ int main(void)
/* === 4. 实时数据采集与上报 (仅在非发射状态运行) === */
// (A) 485 来源数据处理 (Type 0x48)
// (A) Modbus RTU 轮询任务
#if USE_RS485
if (u3_rx_len > 0 && (HAL_GetTick() - u3_last_rx_time > 20))
{
static uint8_t temp_buf3[512];
__disable_irq();
uint16_t len = u3_rx_len;
memcpy(temp_buf3, (uint8_t*)u3_rx_buffer, len);
u3_rx_len = 0;
__enable_irq();
RF433_SendPacket(PROTO_TYPE_485, temp_buf3, len);
}
ModbusRTU_Master_Task();
#endif
// (B) I/O 状态监控与变化上报 (Type 0x10)
@ -384,11 +370,13 @@ int main(void)
}
#endif
#if HEARTBEAT_PACKET
// (C) 30秒系统心跳包 (Type 0xAA)
#if USE_IO_MONITOR
#if USE_IO_MONITOR
if (HAL_GetTick() - g_last_hb_tick >= 30000) {
g_last_hb_tick = HAL_GetTick();
uint8_t hb_payload[7];
uint8_t hb_payload[8];
uint8_t io_state = IO_Monitor_GetAllStates();
#if USE_W5500
@ -413,6 +401,7 @@ int main(void)
#endif
hb_payload[5] = (uint8_t)(modbus_val >> 8);
hb_payload[6] = (uint8_t)(modbus_val & 0xFF);
hb_payload[7] = ModbusRTU_GetAlarmState();
g_hb_seq++;
@ -420,6 +409,9 @@ int main(void)
}
#endif
#endif
#if USE_W5500
ModbusTCP_Client_Task();
#endif
@ -490,11 +482,7 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
}
else if (huart->Instance == USART3) {
/* 🚀 核心生效区:只有当单片机没有在发送数据时(度过屏蔽期),才允许接收 */
if (HAL_GetTick() >= u3_ignore_until) {
if (u3_rx_len < sizeof(u3_rx_buffer)) u3_rx_buffer[u3_rx_len++] = uart3_rx_byte;
u3_last_rx_time = HAL_GetTick();
}
ModbusRTU_FeedRxByte(uart3_rx_byte);
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
}
else if (huart->Instance == USART2) {
@ -533,16 +521,8 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
}
else if (huart->Instance == USART2)
{
/* 调用UART2打印模块的发送完成回调 */
UART2_Print_TxCpltCallback();
}
else if (huart->Instance == USART3)
{
/* 调用多UART路由器的UART3发送完成回调 */
MultiUART_TxCpltCallback(PORT_RS485);
}
}
/* USER CODE END 4 */