node-zserial 1.0.20 → 1.0.21
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/package.json +1 -1
- package/zserial.js +78 -32
package/package.json
CHANGED
package/zserial.js
CHANGED
|
@@ -413,37 +413,71 @@ module.exports = function (RED) {
|
|
|
413
413
|
if (msg.hasOwnProperty("flush") && msg.flush === true) { curPort.serial.flush(); }
|
|
414
414
|
|
|
415
415
|
setCallback(msg, serialConfig, done);
|
|
416
|
-
// msg.payload = serialConfig.payload;
|
|
417
|
-
setTimeout(function () {
|
|
416
|
+
// // msg.payload = serialConfig.payload;
|
|
417
|
+
// setTimeout(function () {
|
|
418
|
+
// try {
|
|
419
|
+
// // node.warn(`当前:${curPort.serial.path}打开状态,${curPort.isopen}`);
|
|
420
|
+
// if (curPort.isopen) {
|
|
421
|
+
// curPort.enqueue(serialConfig, node, function (err, res) {
|
|
422
|
+
// // node.warn("加入队列::" + curPort.serial.path);
|
|
423
|
+
// // node.warn(curPort.queue);
|
|
424
|
+
// // node.warn(res);
|
|
425
|
+
// if (err) {
|
|
426
|
+
// node.error(err)
|
|
427
|
+
// }
|
|
428
|
+
// }, function (queue) {
|
|
429
|
+
// // node.warn("队列开始发送::");
|
|
430
|
+
// // node.warn(queue);
|
|
431
|
+
// });
|
|
432
|
+
// } else {
|
|
433
|
+
// curPort._retryNum = 0
|
|
434
|
+
// zsend(null, {
|
|
435
|
+
// ...serialConfig,
|
|
436
|
+
// status: "ERR_IN_QUEUE",
|
|
437
|
+
// text: '串口未打开,加入消息队列失败',
|
|
438
|
+
// port: curPort.serial.path
|
|
439
|
+
// }, null, curPort.serial.path, done);
|
|
440
|
+
// }
|
|
441
|
+
// } catch (error) {
|
|
442
|
+
// node.error(error);
|
|
443
|
+
// }
|
|
444
|
+
|
|
445
|
+
// }, 100);
|
|
446
|
+
// 等待端口就绪后再入队,避免固定 100ms 竞态导致失败
|
|
447
|
+
var openTimeout = msg.openTimeout || 5000; // 可通过 msg.openTimeout 覆盖,默认 5s
|
|
448
|
+
var enqueued = false;
|
|
449
|
+
|
|
450
|
+
function doEnqueue() {
|
|
451
|
+
if (enqueued) return;
|
|
452
|
+
enqueued = true;
|
|
418
453
|
try {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
// node.warn(res);
|
|
425
|
-
if (err) {
|
|
426
|
-
node.error(err)
|
|
427
|
-
}
|
|
428
|
-
}, function (queue) {
|
|
429
|
-
// node.warn("队列开始发送::");
|
|
430
|
-
// node.warn(queue);
|
|
431
|
-
});
|
|
432
|
-
} else {
|
|
433
|
-
curPort._retryNum = 0
|
|
434
|
-
zsend(null, {
|
|
435
|
-
...serialConfig,
|
|
436
|
-
status: "ERR_IN_QUEUE",
|
|
437
|
-
text: '串口未打开,加入消息队列失败',
|
|
438
|
-
port: curPort.serial.path
|
|
439
|
-
}, null, curPort.serial.path, done);
|
|
440
|
-
}
|
|
454
|
+
curPort.enqueue(serialConfig, node, function (err, res) {
|
|
455
|
+
if (err) { node.error(err); }
|
|
456
|
+
}, function (queue) {
|
|
457
|
+
// 队列开始发送(可选日志)
|
|
458
|
+
});
|
|
441
459
|
} catch (error) {
|
|
442
460
|
node.error(error);
|
|
443
461
|
}
|
|
462
|
+
}
|
|
444
463
|
|
|
445
|
-
|
|
446
|
-
|
|
464
|
+
if (curPort.isopen) {
|
|
465
|
+
doEnqueue();
|
|
466
|
+
} else {
|
|
467
|
+
curPort.once('ready', function () {
|
|
468
|
+
doEnqueue();
|
|
469
|
+
});
|
|
470
|
+
setTimeout(function () {
|
|
471
|
+
if (!enqueued) {
|
|
472
|
+
zsend(null, {
|
|
473
|
+
...serialConfig,
|
|
474
|
+
status: "ERR_IN_QUEUE",
|
|
475
|
+
text: '串口未在超时时间内就绪,加入消息队列失败',
|
|
476
|
+
port: (curPort.serial && curPort.serial.path) || serialConfig.serialport
|
|
477
|
+
}, null, (curPort.serial && curPort.serial.path) || serialConfig.serialport, done);
|
|
478
|
+
}
|
|
479
|
+
}, openTimeout);
|
|
480
|
+
}
|
|
447
481
|
|
|
448
482
|
}
|
|
449
483
|
|
|
@@ -541,7 +575,7 @@ module.exports = function (RED) {
|
|
|
541
575
|
binoutput = serialConfig.bin || 'false',
|
|
542
576
|
addchar = serialConfig.addchar || '',
|
|
543
577
|
responsetimeout = serialConfig.responsetimeout || 10000,
|
|
544
|
-
retryNum = serialConfig.retryNum ||
|
|
578
|
+
retryNum = serialConfig.retryNum || 5;
|
|
545
579
|
var id = port;
|
|
546
580
|
// just return the connection object if already have one
|
|
547
581
|
// key is the port (file path)
|
|
@@ -693,14 +727,14 @@ module.exports = function (RED) {
|
|
|
693
727
|
},
|
|
694
728
|
}
|
|
695
729
|
//newline = newline.replace("\\n","\n").replace("\\r","\r");
|
|
696
|
-
obj._emitter.setMaxListeners(
|
|
730
|
+
obj._emitter.setMaxListeners(500);
|
|
697
731
|
var olderr = "";
|
|
698
732
|
var setupSerial = function () {
|
|
699
733
|
obj._retryNum++;
|
|
700
734
|
// RED.log.info(obj._retryNum)
|
|
701
735
|
if (obj._retryNum > retryNum) {
|
|
702
736
|
// serialPool.zlog("已经重试" + retryNum + "次,请检查串口是否正常!", {});
|
|
703
|
-
obj._emitter.emit('retryerror', id, retryNum);
|
|
737
|
+
// obj._emitter.emit('retryerror', id, retryNum);
|
|
704
738
|
return null;
|
|
705
739
|
}
|
|
706
740
|
obj.serial = new SerialPort({
|
|
@@ -718,9 +752,13 @@ module.exports = function (RED) {
|
|
|
718
752
|
olderr = err.toString();
|
|
719
753
|
// RED.log.error("Err1:[serialconfig:" + id + "] " + RED._("serial.errors.error", { port: port, error: olderr }), {});
|
|
720
754
|
}
|
|
755
|
+
var delay = serialReconnectTime + Math.floor(Math.random() * 1000);
|
|
721
756
|
obj.tout = setTimeout(function () {
|
|
722
757
|
setupSerial();
|
|
723
|
-
},
|
|
758
|
+
}, delay);
|
|
759
|
+
// obj.tout = setTimeout(function () {
|
|
760
|
+
// setupSerial();
|
|
761
|
+
// }, serialReconnectTime);
|
|
724
762
|
} else {
|
|
725
763
|
obj.isopen = true;
|
|
726
764
|
}
|
|
@@ -731,9 +769,13 @@ module.exports = function (RED) {
|
|
|
731
769
|
serialPool.zlog("Err2:[serialconfig:" + id + "] " + RED._("serial.errors.error", { port: port, error: err.toString() }), {});
|
|
732
770
|
obj._emitter.emit('closed', id);
|
|
733
771
|
if (obj.tout) { clearTimeout(obj.tout); }
|
|
772
|
+
// obj.tout = setTimeout(function () {
|
|
773
|
+
// setupSerial();
|
|
774
|
+
// }, serialReconnectTime);
|
|
775
|
+
var delay = serialReconnectTime + Math.floor(Math.random() * 1000);
|
|
734
776
|
obj.tout = setTimeout(function () {
|
|
735
777
|
setupSerial();
|
|
736
|
-
},
|
|
778
|
+
}, delay);
|
|
737
779
|
});
|
|
738
780
|
obj.serial.on('close', function () {
|
|
739
781
|
obj.isopen = false;
|
|
@@ -744,9 +786,13 @@ module.exports = function (RED) {
|
|
|
744
786
|
}
|
|
745
787
|
obj._emitter.emit('closed', id);
|
|
746
788
|
if (obj.tout) { clearTimeout(obj.tout); }
|
|
789
|
+
// obj.tout = setTimeout(function () {
|
|
790
|
+
// setupSerial();
|
|
791
|
+
// }, serialReconnectTime);
|
|
792
|
+
var delay = serialReconnectTime + Math.floor(Math.random() * 1000);
|
|
747
793
|
obj.tout = setTimeout(function () {
|
|
748
794
|
setupSerial();
|
|
749
|
-
},
|
|
795
|
+
}, delay);
|
|
750
796
|
}
|
|
751
797
|
else {
|
|
752
798
|
obj._emitter.emit('stopped', id);
|