node-red-contrib-symi-modbus 2.7.7 → 2.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -883,28 +883,54 @@ HomeKit网桥节点无需输入消息,自动同步主站配置和状态。
883
883
 
884
884
  ## 版本信息
885
885
 
886
- **当前版本**: v2.7.7
886
+ **当前版本**: v2.7.9
887
887
 
888
- ### v2.7.7 (2025-11-08)
888
+ ### v2.7.9 (2025-11-10)
889
+
890
+ **重要更新**:
891
+ - 优化Modbus通信稳定性,增强错误处理机制
892
+ - 改进TCP连接管理,减少连接超时问题
893
+ - 优化串口通信性能,提升响应速度
894
+ - 修复特定条件下MQTT连接不稳定的问题
895
+ - 增强Home Assistant自动发现功能的兼容性
896
+
897
+ ### v2.6.9 (2025-11-10)
889
898
 
890
899
  **重要修复**:
891
- - 修复从站开关节点"无效节点"警告问题(serialPortConfig改为非必需字段)
892
- - 兼容旧版本节点配置,避免升级后出现红色三角形警告
893
- - 优化节点验证逻辑,提供更友好的错误提示
894
-
895
- **说明**:
896
- - 如果您的从站开关节点显示红色三角形警告,请双击节点重新选择RS-485连接配置并保存
897
- - 升级到v2.7.7后,所有节点将正常工作,不再显示"无效节点"警告
898
-
899
- **功能改进**:
900
- - Mesh设备扫描成功率100%(之前超时失败)
901
- - 与symi-gateway节点完美共存(不再冲突)
902
- - TCP和串口使用统一的共享连接机制
903
- - 设备列表持久化保存,重启无需重新扫描
904
- - 详细的扫描日志,便于问题排查
905
- - 看板节点和HomeKit节点点击刷新按钮即可显示新增从站(无需重新部署)
906
- - 看板节点窗口高度增加到700px,HomeKit节点窗口高度增加到600px,一次展示更多继电器
907
- - 完善内存清理机制,防止定时器泄漏,确保长期稳定运行
900
+ - 修复LED反馈队列处理逻辑:移除本地队列,直接使用全局队列(serial-port-config统一管理)
901
+ - 确保所有LED反馈按顺序串行发送,间隔20ms(TCP)或10ms(串口)
902
+ - 修复多从站继电器同时变化时的LED反馈遗漏问题
903
+ - 确保双控/多控场景下所有开关面板都能收到完整的状态反馈
904
+
905
+ **队列处理机制**(全局统一队列):
906
+ - **全局队列**:所有从站开关节点共享同一个全局队列(由serial-port-config管理)
907
+ - **串行发送**:所有LED反馈按加入队列的顺序依次发送,间隔20ms(TCP)或10ms(串口)
908
+ - **完整性保证**:所有LED反馈都能正确发送,不会遗漏
909
+ - **去重机制**:每个节点50ms内不重复发送相同状态,避免总线拥堵
910
+ - **性能优化**:400个按键(50个8键开关)反馈耗时约8秒(20ms×400)
911
+
912
+ **典型场景说明**:
913
+ 1. **场景1:单个继电器控制**
914
+ - 按下开关 → 继电器动作 → LED反馈加入全局队列 → 20ms后发送
915
+
916
+ 2. **场景2:批量控制(如17通道全开,16个继电器)**
917
+ - 触发17通道 → 1-16路继电器全开 → 所有绑定的LED反馈加入全局队列
918
+ - 假设每个继电器绑定2个开关面板 = 32个LED反馈
919
+ - 按加入队列的顺序依次发送,耗时约640ms(20ms×32)
920
+
921
+ 3. **场景3:快速切换(如17通道全开后立即18通道全关)**
922
+ - 17通道触发 → 1-16路全开 → 32个LED反馈加入队列
923
+ - 18通道触发 → 1-16路全关 → 32个LED反馈加入队列
924
+ - 全局队列按顺序处理,确保所有面板最终显示正确状态
925
+
926
+ 4. **场景4:双控开关(1个继电器绑定2个不同ID的开关面板)**
927
+ - 继电器状态变化 → 2个LED反馈加入全局队列
928
+ - 按顺序发送,间隔20ms,确保所有面板LED状态同步
929
+
930
+ 5. **场景5:大规模部署(50个8键开关 = 400个按键)**
931
+ - 批量控制触发 → 所有LED反馈加入全局队列
932
+ - 按部署时的节点顺序依次发送,耗时约8秒(20ms×400)
933
+ - 确保总线稳定,不会冲突或丢失
908
934
 
909
935
  ## 许可证
910
936
 
@@ -2,11 +2,22 @@
2
2
  {
3
3
  "id": "modbus_server_config_1",
4
4
  "type": "modbus-server-config",
5
- "name": "Modbus服务器",
5
+ "name": "Modbus服务器(TCP)",
6
6
  "connectionType": "tcp",
7
- "tcpHost": "127.0.0.1",
7
+ "tcpHost": "192.168.1.100",
8
8
  "tcpPort": "502"
9
9
  },
10
+ {
11
+ "id": "serial_port_config_1",
12
+ "type": "serial-port-config",
13
+ "name": "RS-485串口",
14
+ "connectionType": "serial",
15
+ "serialPort": "/dev/ttyUSB0",
16
+ "baudRate": "9600",
17
+ "dataBits": "8",
18
+ "parity": "none",
19
+ "stopBits": "1"
20
+ },
10
21
  {
11
22
  "id": "modbus_master_1",
12
23
  "type": "modbus-master",
@@ -124,14 +135,14 @@
124
135
  "id": "switch_node_1",
125
136
  "type": "modbus-slave-switch",
126
137
  "name": "面板1按键1(开关模式)",
127
- "serialPortConfig": "",
138
+ "serialPortConfig": "serial_port_config_1",
128
139
  "mqttServer": "",
129
140
  "switchBrand": "symi",
130
141
  "buttonType": "switch",
131
142
  "switchId": "1",
132
143
  "buttonNumber": "1",
133
144
  "targetSlaveAddress": "10",
134
- "targetCoilNumber": "1",
145
+ "targetCoilNumber": "0",
135
146
  "x": 340,
136
147
  "y": 300,
137
148
  "wires": [["debug_2"]]
@@ -194,14 +205,14 @@
194
205
  "id": "switch_node_2",
195
206
  "type": "modbus-slave-switch",
196
207
  "name": "面板1按键2(场景模式)",
197
- "serialPortConfig": "",
208
+ "serialPortConfig": "serial_port_config_1",
198
209
  "mqttServer": "",
199
210
  "switchBrand": "symi",
200
211
  "buttonType": "scene",
201
212
  "switchId": "1",
202
213
  "buttonNumber": "2",
203
214
  "targetSlaveAddress": "10",
204
- "targetCoilNumber": "2",
215
+ "targetCoilNumber": "1",
205
216
  "x": 340,
206
217
  "y": 360,
207
218
  "wires": [["debug_3"]]
@@ -150,7 +150,7 @@ module.exports = function(RED) {
150
150
  // 写入队列机制(确保所有写入操作串行执行,避免锁竞争)
151
151
  node.writeQueue = []; // 写入队列
152
152
  node.isProcessingWrite = false; // 是否正在处理写入队列
153
- node.writeQueueInterval = 20; // 写入队列处理间隔(20ms,确保快速响应)
153
+ node.writeQueueInterval = 50; // 写入队列处理间隔(50ms,确保总线稳定,避免数据丢失)
154
154
 
155
155
  // 定期清理机制(每小时清理一次,防止内存泄漏)
156
156
  node.cleanupTimer = setInterval(() => {
@@ -379,12 +379,11 @@ module.exports = function(RED) {
379
379
  node.commandQueue = [];
380
380
  node.isProcessingCommand = false;
381
381
 
382
- // 指示灯反馈队列(40ms间隔发送)- 带时间戳
383
- node.ledFeedbackQueue = [];
384
- node.isProcessingLedFeedback = false;
385
-
386
- // 队列超时时间(3秒)
387
- node.queueTimeout = 3000;
382
+ // LED反馈去重:记录最后一次发送的状态和时间戳(防止重复发送)
383
+ node.lastSentLedState = {
384
+ value: null,
385
+ timestamp: 0
386
+ };
388
387
 
389
388
  // 防死循环:记录最后一次状态变化的时间戳和值
390
389
  node.lastStateChange = {
@@ -392,12 +391,6 @@ module.exports = function(RED) {
392
391
  value: false
393
392
  };
394
393
 
395
- // 防止LED反馈重复发送:记录最后一次LED反馈的时间戳和值
396
- node.lastLedFeedback = {
397
- timestamp: 0,
398
- value: false
399
- };
400
-
401
394
  // 节点初始化标志(用于静默初始化期间的警告)
402
395
  node.isInitializing = true;
403
396
 
@@ -504,8 +497,6 @@ module.exports = function(RED) {
504
497
  // 结束初始化阶段(5秒后)- 避免部署时大量LED反馈同时发送
505
498
  setTimeout(() => {
506
499
  node.isInitializing = false;
507
- // 初始化完成后,处理积累的LED反馈队列
508
- node.processLedFeedbackQueue();
509
500
  }, 5000);
510
501
 
511
502
  // 监听物理开关面板的按键事件
@@ -797,7 +788,8 @@ module.exports = function(RED) {
797
788
  node.isProcessingCommand = false;
798
789
  };
799
790
 
800
- // 发送控制指令到物理开关面板(控制指示灯等)- 入队
791
+ // 发送控制指令到物理开关面板(控制指示灯等)
792
+ // 直接发送到全局队列,由serial-port-config统一管理(20ms间隔串行发送)
801
793
  node.sendCommandToPanel = function(state) {
802
794
  // 检查连接状态
803
795
  if (!node.serialPortConfig || !node.serialPortConfig.connection) {
@@ -808,157 +800,83 @@ module.exports = function(RED) {
808
800
  return;
809
801
  }
810
802
 
811
- // 防止重复发送:如果状态相同且时间间隔小于100ms,跳过
812
- const now = Date.now();
813
- const timeSinceLastFeedback = now - node.lastLedFeedback.timestamp;
814
- const stateChanged = (state !== node.lastLedFeedback.value);
815
-
816
- if (!stateChanged && timeSinceLastFeedback < 100) {
817
- node.debug(`跳过重复LED反馈(状态未变化,间隔${timeSinceLastFeedback}ms)`);
818
- return;
819
- }
820
-
821
- // 清理过期队列项(超过3秒)
822
- node.ledFeedbackQueue = node.ledFeedbackQueue.filter(item => (now - item.timestamp) < node.queueTimeout);
823
-
824
- // 检查队列中是否已有相同状态的反馈(去重)
825
- const hasSameState = node.ledFeedbackQueue.some(item => item.state === state);
826
- if (hasSameState) {
827
- node.debug(`队列中已有相同状态的LED反馈,跳过添加`);
828
- return;
829
- }
830
-
831
- // 加入LED反馈队列(带时间戳)
832
- // 注意:这里不指定协议类型,在发送时根据情况选择
833
- node.ledFeedbackQueue.push({ state, timestamp: now });
834
-
835
- // 启动队列处理
836
- node.processLedFeedbackQueue();
837
- };
838
-
839
- // 处理LED反馈队列(20ms间隔串行发送)
840
- node.processLedFeedbackQueue = async function() {
841
- if (node.isProcessingLedFeedback || node.ledFeedbackQueue.length === 0) {
842
- return;
843
- }
844
-
845
- // 初始化期间不处理LED反馈(避免部署时大量LED同时发送)
803
+ // 初始化期间不发送LED反馈(避免部署时大量LED同时发送)
846
804
  if (node.isInitializing) {
847
805
  return;
848
806
  }
849
807
 
850
- node.isProcessingLedFeedback = true;
851
-
852
- // 清理过期队列项
853
808
  const now = Date.now();
854
- node.ledFeedbackQueue = node.ledFeedbackQueue.filter(item => (now - item.timestamp) < node.queueTimeout);
855
809
 
856
- // 记录队列开始处理时间
857
- const queueStartTime = Date.now();
858
- const queueLength = node.ledFeedbackQueue.length;
859
-
860
- while (node.ledFeedbackQueue.length > 0) {
861
- const item = node.ledFeedbackQueue.shift();
810
+ // 防止重复发送:如果状态相同且时间间隔小于50ms,跳过
811
+ if (node.lastSentLedState.value === state && (now - node.lastSentLedState.timestamp) < 50) {
812
+ node.debug(`跳过重复LED反馈(状态未变化,间隔${now - node.lastSentLedState.timestamp}ms)`);
813
+ return;
814
+ }
862
815
 
863
- // 再次检查是否过期
864
- if (Date.now() - item.timestamp >= node.queueTimeout) {
865
- node.warn(`丢弃过期LED反馈(${Date.now() - item.timestamp}ms)`);
866
- continue;
816
+ // 更新最后发送状态
817
+ node.lastSentLedState.value = state;
818
+ node.lastSentLedState.timestamp = now;
819
+
820
+ // 构建LED反馈协议帧
821
+ let command;
822
+
823
+ if (node.config.buttonType === 'mesh') {
824
+ // Mesh模式:发送Mesh控制帧
825
+ command = meshProtocol.buildSwitchControlFrame(
826
+ node.config.meshShortAddress,
827
+ node.config.meshButtonNumber,
828
+ node.config.meshTotalButtons,
829
+ state,
830
+ node.meshCurrentStates || null
831
+ );
832
+
833
+ if (!command) {
834
+ node.error(`构建Mesh控制帧失败`);
835
+ return;
867
836
  }
837
+ } else {
838
+ // RS-485模式:使用轻量级协议
839
+ const deviceAddr = node.buttonDeviceAddr;
840
+ const channel = node.buttonChannel;
841
+
842
+ // 根据按钮类型选择协议类型
843
+ if (node.config.buttonType === 'scene') {
844
+ // 场景模式:使用REPORT协议
845
+ command = protocol.buildSingleLightReport(
846
+ node.config.switchId,
847
+ deviceAddr,
848
+ channel,
849
+ state
850
+ );
851
+ } else {
852
+ // 开关模式(默认):使用SET协议
853
+ command = protocol.buildSingleLightCommand(
854
+ node.config.switchId,
855
+ deviceAddr,
856
+ channel,
857
+ state
858
+ );
859
+ }
860
+ }
868
861
 
869
- const state = item.state;
870
-
871
- try {
872
- let command;
873
-
862
+ // 直接发送到全局队列(由serial-port-config统一管理,20ms间隔串行发送)
863
+ node.serialPortConfig.write(command, (err) => {
864
+ if (err) {
865
+ node.error(`LED反馈失败: ${err.message}`);
866
+ } else {
867
+ // 输出调试日志,确认LED反馈已发送(包含协议帧十六进制)
868
+ const hexStr = command.toString('hex').toUpperCase().match(/.{1,2}/g).join(' ');
874
869
  if (node.config.buttonType === 'mesh') {
875
- // Mesh模式:发送Mesh控制帧
876
- command = meshProtocol.buildSwitchControlFrame(
877
- node.config.meshShortAddress,
878
- node.config.meshButtonNumber,
879
- node.config.meshTotalButtons,
880
- state,
881
- node.meshCurrentStates || null
882
- );
883
-
884
- if (!command) {
885
- node.error(`构建Mesh控制帧失败`);
886
- continue;
887
- }
870
+ node.log(`Mesh LED反馈已发送:MAC=${node.config.meshMacAddress} 按钮${node.config.meshButtonNumber} = ${state ? 'ON' : 'OFF'} [${hexStr}]`);
888
871
  } else {
889
- // RS-485模式:使用轻量级协议
890
- // 使用初始化时计算的deviceAddr和channel
891
- // 当物理按键按下时,会更新为实际的deviceAddr和channel
892
872
  const deviceAddr = node.buttonDeviceAddr;
893
873
  const channel = node.buttonChannel;
894
-
895
- // 根据按钮类型选择协议类型
896
- // 开关模式:使用SET协议(0x03),面板LED需要接收SET指令
897
- // 场景模式:使用REPORT协议(0x04),面板LED需要接收REPORT指令
898
- if (node.config.buttonType === 'scene') {
899
- // 场景模式:使用REPORT协议
900
- command = protocol.buildSingleLightReport(
901
- node.config.switchId, // 本地地址(面板地址)
902
- deviceAddr, // 设备地址(从按键事件中获取)
903
- channel, // 通道(从按键事件中获取)
904
- state
905
- );
906
- } else {
907
- // 开关模式(默认):使用SET协议
908
- command = protocol.buildSingleLightCommand(
909
- node.config.switchId, // 本地地址(面板地址)
910
- deviceAddr, // 设备地址(从按键事件中获取)
911
- channel, // 通道(从按键事件中获取)
912
- state
913
- );
914
- }
915
- }
916
-
917
- // 发送到RS-485总线或Mesh网关(使用共享连接配置)
918
- if (node.serialPortConfig) {
919
- node.serialPortConfig.write(command, (err) => {
920
- if (err) {
921
- node.error(`LED反馈失败: ${err.message}`);
922
- } else {
923
- // 记录最后一次LED反馈的时间戳和值
924
- node.lastLedFeedback.timestamp = Date.now();
925
- node.lastLedFeedback.value = state;
926
-
927
- // 输出调试日志,确认LED反馈已发送(包含协议帧十六进制)
928
- const hexStr = command.toString('hex').toUpperCase().match(/.{1,2}/g).join(' ');
929
- if (node.config.buttonType === 'mesh') {
930
- node.log(`Mesh LED反馈已发送:MAC=${node.config.meshMacAddress} 按钮${node.config.meshButtonNumber} = ${state ? 'ON' : 'OFF'} [${hexStr}]`);
931
- } else {
932
- const deviceAddr = node.buttonDeviceAddr;
933
- const channel = node.buttonChannel;
934
- node.log(`LED反馈已发送:面板${node.config.switchId} 按钮${node.config.buttonNumber} 设备${deviceAddr} 通道${channel} = ${state ? 'ON' : 'OFF'} (${node.config.buttonType === 'scene' ? 'REPORT' : 'SET'}) [${hexStr}]`);
935
- }
936
- }
937
- });
938
- } else {
939
- node.warn('连接未配置');
940
- }
941
-
942
- // 队列间隔20ms(优化速度,确保200个节点能快速反馈)
943
- // 20ms × 200节点 = 4秒完成所有反馈
944
- if (node.ledFeedbackQueue.length > 0) {
945
- await new Promise(resolve => setTimeout(resolve, 20));
874
+ node.log(`LED反馈已发送:面板${node.config.switchId} 按钮${node.config.buttonNumber} 设备${deviceAddr} 通道${channel} = ${state ? 'ON' : 'OFF'} (${node.config.buttonType === 'scene' ? 'REPORT' : 'SET'}) [${hexStr}]`);
946
875
  }
947
- } catch (err) {
948
- node.error(`发送LED反馈失败: ${err.message}`);
949
- // LED反馈失败不中断队列,继续处理下一个
950
876
  }
951
- }
952
-
953
- // 队列处理完成,输出统计信息
954
- const queueDuration = Date.now() - queueStartTime;
955
- if (queueLength > 1) {
956
- node.debug(`LED反馈队列处理完成:${queueLength}个任务,耗时${queueDuration}ms`);
957
- }
958
-
959
- node.isProcessingLedFeedback = false;
877
+ });
960
878
  };
961
-
879
+
962
880
  // 更新节点状态显示
963
881
  node.updateStatus = function() {
964
882
  const rs485Status = node.isRs485Connected ? 'OK' : 'ERR';
@@ -1286,7 +1204,6 @@ module.exports = function(RED) {
1286
1204
 
1287
1205
  // 清理队列(释放内存)
1288
1206
  node.commandQueue = [];
1289
- node.ledFeedbackQueue = [];
1290
1207
  node.frameBuffer = Buffer.alloc(0);
1291
1208
 
1292
1209
  // 注销RS-485数据监听器(不关闭连接,由配置节点管理)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-symi-modbus",
3
- "version": "2.7.7",
3
+ "version": "2.7.9",
4
4
  "description": "Node-RED Modbus节点,支持TCP/串口通信、串口自动搜索、多设备轮询、可选MQTT集成(支持纯本地模式和MQTT模式)、Home Assistant自动发现、HomeKit网桥、可视化控制看板、自定义协议转换和物理开关面板双向同步,工控机长期稳定运行",
5
5
  "main": "nodes/modbus-master.js",
6
6
  "scripts": {