feat: DI报警改为Modbus RTU报警上报,心跳包IO位替换为RTU报警状态,添加详细中文注释
This commit is contained in:
@ -92,7 +92,7 @@ static volatile uint32_t u1_last_rx_time = 0;
|
||||
/* === 协议处理全局变量 === */
|
||||
static uint16_t g_hb_seq = 0; /* 心跳序列号 */
|
||||
static uint32_t g_last_hb_tick = 0; /* 上次心跳时间 */
|
||||
static uint8_t g_last_io_state = 0xFF; /* 上次记录的 IO 状态,用于变化检测 */
|
||||
static uint8_t g_last_alarm_state = 0xFF; /* 上次 Modbus RTU 报警状态,用于变化检测 */
|
||||
|
||||
/* === W5500 外部变量声明 === */
|
||||
#if USE_W5500
|
||||
@ -169,7 +169,9 @@ uint8_t IO_Get_Current_State(void)
|
||||
/* W5500 variables */
|
||||
#if USE_W5500
|
||||
#define SOCKET_ID 0
|
||||
#ifndef ETHERNET_BUF_MAX_SIZE
|
||||
#define ETHERNET_BUF_MAX_SIZE 2048
|
||||
#endif
|
||||
static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0};
|
||||
static uint16_t local_port = 8000;
|
||||
#endif
|
||||
@ -356,16 +358,17 @@ int main(void)
|
||||
ModbusRTU_Master_Task();
|
||||
#endif
|
||||
|
||||
// (B) I/O 状态监控与变化上报 (Type 0x10)
|
||||
#if USE_IO_MONITOR
|
||||
IO_Monitor_Task(); // 执行去抖扫描
|
||||
uint8_t current_io = IO_Monitor_GetAllStates();
|
||||
if (current_io != g_last_io_state) {
|
||||
if (g_last_io_state == 0xFF) {
|
||||
g_last_io_state = current_io;
|
||||
} else {
|
||||
g_last_io_state = current_io;
|
||||
RF433_SendPacket(PROTO_TYPE_IO, ¤t_io, 1);
|
||||
// (B) Modbus RTU 报警状态变化上报 (Type 0x10,与原 DI 格式兼容)
|
||||
#if USE_RS485
|
||||
{
|
||||
uint8_t alarm_state = ModbusRTU_GetAlarmState();
|
||||
if (alarm_state != g_last_alarm_state) {
|
||||
if (g_last_alarm_state != 0xFF) {
|
||||
g_last_alarm_state = alarm_state;
|
||||
RF433_SendPacket(PROTO_TYPE_IO, &alarm_state, 1);
|
||||
} else {
|
||||
g_last_alarm_state = alarm_state;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -373,19 +376,9 @@ int main(void)
|
||||
|
||||
#if HEARTBEAT_PACKET
|
||||
// (C) 30秒系统心跳包 (Type 0xAA)
|
||||
#if USE_IO_MONITOR
|
||||
if (HAL_GetTick() - g_last_hb_tick >= 30000) {
|
||||
g_last_hb_tick = HAL_GetTick();
|
||||
uint8_t hb_payload[8];
|
||||
uint8_t io_state = IO_Monitor_GetAllStates();
|
||||
|
||||
#if USE_W5500
|
||||
uint8_t phy_link_status;
|
||||
ctlwizchip(CW_GET_PHYLINK, (void *)&phy_link_status);
|
||||
if (phy_link_status == PHY_LINK_ON) {
|
||||
io_state |= 0x80;
|
||||
}
|
||||
#endif
|
||||
uint8_t hb_payload[7];
|
||||
|
||||
hb_payload[0] = (uint8_t)(g_hb_seq >> 8);
|
||||
hb_payload[1] = (uint8_t)(g_hb_seq & 0xFF);
|
||||
@ -393,7 +386,7 @@ int main(void)
|
||||
hb_payload[2] = (uint8_t)(XTELL_FIRMWARE_CODE >> 8);
|
||||
hb_payload[3] = (uint8_t)(XTELL_FIRMWARE_CODE & 0xFF);
|
||||
|
||||
hb_payload[4] = io_state;
|
||||
hb_payload[4] = ModbusRTU_GetAlarmState();
|
||||
|
||||
uint16_t modbus_val = 0xFFFF;
|
||||
#if USE_W5500
|
||||
@ -401,7 +394,6 @@ 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++;
|
||||
|
||||
@ -409,8 +401,6 @@ int main(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if USE_W5500
|
||||
ModbusTCP_Client_Task();
|
||||
|
||||
Reference in New Issue
Block a user