node-red-contrib-symi-modbus 1.6.2 → 1.6.5
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/nodes/modbus-master.js
CHANGED
|
@@ -102,8 +102,8 @@ module.exports = function(RED) {
|
|
|
102
102
|
|
|
103
103
|
// 更新节点状态显示
|
|
104
104
|
node.updateNodeStatus = function() {
|
|
105
|
-
const modbusStatus = node.isConnected ? "Modbus
|
|
106
|
-
const mqttStatus = node.mqttConnected ? "MQTT
|
|
105
|
+
const modbusStatus = node.isConnected ? "Modbus-OK" : "Modbus-ERR";
|
|
106
|
+
const mqttStatus = node.mqttConnected ? "MQTT-OK" : "MQTT-ERR";
|
|
107
107
|
|
|
108
108
|
if (node.isConnected && node.mqttConnected) {
|
|
109
109
|
node.status({fill: "green", shape: "dot", text: `${modbusStatus} ${mqttStatus}`});
|
|
@@ -525,11 +525,15 @@ module.exports = function(RED) {
|
|
|
525
525
|
const minInterval = Math.min(...node.config.slaves.map(s => s.pollInterval));
|
|
526
526
|
|
|
527
527
|
node.pollTimer = setInterval(() => {
|
|
528
|
-
node.pollNextSlave()
|
|
528
|
+
node.pollNextSlave().catch(err => {
|
|
529
|
+
node.error(`轮询错误: ${err.message}`);
|
|
530
|
+
});
|
|
529
531
|
}, minInterval);
|
|
530
532
|
|
|
531
533
|
// 立即执行一次
|
|
532
|
-
node.pollNextSlave()
|
|
534
|
+
node.pollNextSlave().catch(err => {
|
|
535
|
+
node.error(`轮询错误: ${err.message}`);
|
|
536
|
+
});
|
|
533
537
|
};
|
|
534
538
|
|
|
535
539
|
// 停止轮询
|
|
@@ -629,15 +633,16 @@ module.exports = function(RED) {
|
|
|
629
633
|
// 更新状态显示
|
|
630
634
|
node.updateNodeStatus();
|
|
631
635
|
|
|
632
|
-
//
|
|
633
|
-
if (
|
|
636
|
+
// 检测是否是连接断开错误
|
|
637
|
+
if (err.message &&
|
|
634
638
|
(err.message.includes('ECONNRESET') ||
|
|
635
639
|
err.message.includes('ETIMEDOUT') ||
|
|
636
|
-
err.message.includes('ENOTCONN')
|
|
640
|
+
err.message.includes('ENOTCONN') ||
|
|
641
|
+
err.message.includes('Port Not Open'))) {
|
|
637
642
|
|
|
638
|
-
//
|
|
643
|
+
// 连接断开,尝试重连
|
|
639
644
|
if (shouldLog) {
|
|
640
|
-
node.warn('
|
|
645
|
+
node.warn('检测到连接断开,尝试重连...');
|
|
641
646
|
}
|
|
642
647
|
|
|
643
648
|
node.isConnected = false;
|
|
@@ -667,62 +672,70 @@ module.exports = function(RED) {
|
|
|
667
672
|
};
|
|
668
673
|
|
|
669
674
|
// 写单个线圈
|
|
670
|
-
node.writeSingleCoil =
|
|
675
|
+
node.writeSingleCoil = function(slaveId, coil, value) {
|
|
671
676
|
if (!node.isConnected) {
|
|
672
677
|
node.warn('Modbus未连接');
|
|
673
|
-
return;
|
|
678
|
+
return Promise.resolve();
|
|
674
679
|
}
|
|
675
680
|
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
681
|
+
return (async () => {
|
|
682
|
+
try {
|
|
683
|
+
node.client.setID(slaveId);
|
|
684
|
+
await node.client.writeCoil(coil, value);
|
|
685
|
+
|
|
686
|
+
// 更新本地状态
|
|
687
|
+
node.deviceStates[slaveId].coils[coil] = value;
|
|
688
|
+
|
|
689
|
+
node.log(`写入成功: 从站${slaveId} 线圈${coil} = ${value}`);
|
|
690
|
+
|
|
691
|
+
// 发布到MQTT和触发事件
|
|
692
|
+
node.publishMqttState(slaveId, coil, value);
|
|
693
|
+
node.emit('stateUpdate', {
|
|
694
|
+
slave: slaveId,
|
|
695
|
+
coil: coil,
|
|
696
|
+
value: value
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
} catch (err) {
|
|
700
|
+
node.error(`写入线圈失败: ${err.message}`);
|
|
701
|
+
}
|
|
702
|
+
})().catch(err => {
|
|
703
|
+
node.error(`写入线圈异常: ${err.message}`);
|
|
704
|
+
});
|
|
696
705
|
};
|
|
697
706
|
|
|
698
707
|
// 写多个线圈
|
|
699
|
-
node.writeMultipleCoils =
|
|
708
|
+
node.writeMultipleCoils = function(slaveId, startCoil, values) {
|
|
700
709
|
if (!node.isConnected) {
|
|
701
710
|
node.warn('Modbus未连接');
|
|
702
|
-
return;
|
|
711
|
+
return Promise.resolve();
|
|
703
712
|
}
|
|
704
713
|
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
714
|
+
return (async () => {
|
|
715
|
+
try {
|
|
716
|
+
node.client.setID(slaveId);
|
|
717
|
+
await node.client.writeCoils(startCoil, values);
|
|
718
|
+
|
|
719
|
+
// 更新本地状态
|
|
720
|
+
for (let i = 0; i < values.length; i++) {
|
|
721
|
+
node.deviceStates[slaveId].coils[startCoil + i] = values[i];
|
|
722
|
+
// 发布到MQTT和触发事件
|
|
723
|
+
node.publishMqttState(slaveId, startCoil + i, values[i]);
|
|
724
|
+
node.emit('stateUpdate', {
|
|
725
|
+
slave: slaveId,
|
|
726
|
+
coil: startCoil + i,
|
|
727
|
+
value: values[i]
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
node.log(`批量写入成功: 从站${slaveId} 起始线圈${startCoil}`);
|
|
732
|
+
|
|
733
|
+
} catch (err) {
|
|
734
|
+
node.error(`批量写入线圈失败: ${err.message}`);
|
|
719
735
|
}
|
|
720
|
-
|
|
721
|
-
node.
|
|
722
|
-
|
|
723
|
-
} catch (err) {
|
|
724
|
-
node.error(`批量写入线圈失败: ${err.message}`);
|
|
725
|
-
}
|
|
736
|
+
})().catch(err => {
|
|
737
|
+
node.error(`批量写入线圈异常: ${err.message}`);
|
|
738
|
+
});
|
|
726
739
|
};
|
|
727
740
|
|
|
728
741
|
// 处理输入消息
|
|
@@ -331,21 +331,21 @@ module.exports = function(RED) {
|
|
|
331
331
|
|
|
332
332
|
// 更新节点状态显示
|
|
333
333
|
node.updateStatus = function() {
|
|
334
|
-
const rs485Status = node.isRs485Connected ? '
|
|
335
|
-
const mqttStatus = node.mqttClient && node.mqttClient.connected ? '
|
|
334
|
+
const rs485Status = node.isRs485Connected ? 'OK' : 'ERR';
|
|
335
|
+
const mqttStatus = node.mqttClient && node.mqttClient.connected ? 'OK' : 'ERR';
|
|
336
336
|
const state = node.currentState ? 'ON' : 'OFF';
|
|
337
337
|
|
|
338
338
|
if (node.isRs485Connected && node.mqttClient && node.mqttClient.connected) {
|
|
339
339
|
node.status({
|
|
340
340
|
fill: node.currentState ? "green" : "grey",
|
|
341
341
|
shape: "dot",
|
|
342
|
-
text: `RS485
|
|
342
|
+
text: `RS485-${rs485Status} MQTT-${mqttStatus} ${state}`
|
|
343
343
|
});
|
|
344
344
|
} else {
|
|
345
345
|
node.status({
|
|
346
346
|
fill: "red",
|
|
347
347
|
shape: "ring",
|
|
348
|
-
text: `RS485
|
|
348
|
+
text: `RS485-${rs485Status} MQTT-${mqttStatus}`
|
|
349
349
|
});
|
|
350
350
|
}
|
|
351
351
|
};
|
package/package.json
CHANGED
|
Binary file
|