From 794db633dc72323acb691804555ff05e38d4a18f Mon Sep 17 00:00:00 2001 From: wangfq Date: Thu, 30 Jul 2026 21:47:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Modbus=20=E4=B8=BB=E5=8A=A8=E4=B8=8A?= =?UTF-8?q?=E6=8A=A5=E7=BC=BA=E5=B0=91=E7=BA=BF=E5=9C=88=E6=96=AD=E5=BC=80?= =?UTF-8?q?/=E9=87=8D=E8=BF=9E=E4=BA=8B=E4=BB=B6=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: modbus_report_schedule() 只检测继电器翻转, 没有检测 loop1_LOOP_OK 变化。线圈断开时依赖 150ms/800ms 周期上报, 若断开-重连-再断开在一个周期内完成, 中间事件被吞掉。 修复: - +extern loop1_LOOP_OK - +_last_loop_ok 状态追踪器 - +线圈断开/重连边沿检测 (对齐 700BS rs485_poll 行为) - +modbus_init 初始化 _last_loop_ok --- .../APP/rs485_modbus.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c b/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c index 3a290ee..d777097 100644 --- a/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c +++ b/BLE/DLD154Pro_Peripheral_28K/APP/rs485_modbus.c @@ -78,6 +78,7 @@ static uint16_t crc16(const uint8_t *data, uint8_t len) extern uint8_t _rx_buf[16]; extern uint8_t _rx_len; extern uint32_t _rx_last_tick; +extern uint8_t loop1_LOOP_OK; /* 线圈连接状态 (1=正常, 0=断开) */ /*=========================================================================== * Modbus 协议状态 @@ -100,6 +101,9 @@ static uint32_t _misc_time_value = 0; /* 时间值 (10ms 单位) */ /* 计数器 (上电清零) */ static uint32_t _relay_engage_cnt = 0; /* 继电器吸合累计次数 */ +/* 线圈状态追踪 (断开/重连 事件触发) */ +static uint8_t _last_loop_ok = 0; /* 线圈连接状态快照 (边沿检测) */ + /*=========================================================================== * 主动上报 — 全数据块组装 & 发送 *===========================================================================*/ @@ -282,6 +286,20 @@ static void modbus_report_schedule(void) } } + /* 线圈断开/重连 → 立即上报 (对齐 700BS 行为) */ + { + uint8_t loop_ok = loop1_LOOP_OK; + if (loop_ok != _last_loop_ok) { + _last_loop_ok = loop_ok; + update_misc_time(); + send_full_data(); + _rpt_last_ms = mstick(); + _rpt_hyst_cnt = 0; + _rpt_active = is_active(); + return; + } + } + uint32_t now = mstick(); uint32_t elapsed = now - _rpt_last_ms; @@ -770,6 +788,7 @@ void modbus_init(void) _rpt_active = 0; _rpt_hyst_cnt = 0; _last_relay_state = g_loop_acs_info.relay1_state; + _last_loop_ok = loop1_LOOP_OK; _last_relay_off_10ms = 0; _last_relay_on_10ms = 0; _misc_time_type = 0xFFFF;