mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
connect/disconnect, configurable reconnect time
This commit is contained in:
parent
05e3a1b600
commit
cbfbfc821e
@ -5,7 +5,6 @@ module.exports = function(RED) {
|
|||||||
var events = require("events");
|
var events = require("events");
|
||||||
const { SerialPort } = require('serialport');
|
const { SerialPort } = require('serialport');
|
||||||
var bufMaxSize = 32768; // Max serial buffer size, for inputs...
|
var bufMaxSize = 32768; // Max serial buffer size, for inputs...
|
||||||
const serialReconnectTime = settings.serialReconnectTime || 15000;
|
|
||||||
|
|
||||||
// TODO: 'serialPool' should be encapsulated in SerialPortNode
|
// TODO: 'serialPool' should be encapsulated in SerialPortNode
|
||||||
|
|
||||||
@ -27,6 +26,7 @@ module.exports = function(RED) {
|
|||||||
this.out = n.out || "char";
|
this.out = n.out || "char";
|
||||||
this.waitfor = n.waitfor || "";
|
this.waitfor = n.waitfor || "";
|
||||||
this.responsetimeout = n.responsetimeout || 10000;
|
this.responsetimeout = n.responsetimeout || 10000;
|
||||||
|
this.serialReconnectTime = n.serialReconnectTime || 15000;
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("serial-port",SerialPortNode);
|
RED.nodes.registerType("serial-port",SerialPortNode);
|
||||||
|
|
||||||
@ -42,6 +42,14 @@ module.exports = function(RED) {
|
|||||||
node.port = serialPool.get(this.serialConfig);
|
node.port = serialPool.get(this.serialConfig);
|
||||||
|
|
||||||
node.on("input",function(msg) {
|
node.on("input",function(msg) {
|
||||||
|
if (msg.hasOwnProperty("disconnect") && this.serialConfig) {
|
||||||
|
serialPool.disconnect(this.serialConfig.serialport);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (msg.hasOwnProperty("connect") && this.serialConfig) {
|
||||||
|
serialPool.connect(this.serialConfig.serialport);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (msg.hasOwnProperty("baudrate")) {
|
if (msg.hasOwnProperty("baudrate")) {
|
||||||
var baud = parseInt(msg.baudrate);
|
var baud = parseInt(msg.baudrate);
|
||||||
if (isNaN(baud)) {
|
if (isNaN(baud)) {
|
||||||
@ -219,7 +227,8 @@ module.exports = function(RED) {
|
|||||||
waitfor = serialConfig.waitfor,
|
waitfor = serialConfig.waitfor,
|
||||||
binoutput = serialConfig.bin,
|
binoutput = serialConfig.bin,
|
||||||
addchar = serialConfig.addchar,
|
addchar = serialConfig.addchar,
|
||||||
responsetimeout = serialConfig.responsetimeout;
|
responsetimeout = serialConfig.responsetimeout,
|
||||||
|
serialReconnectTime = serialConfig.responsetimeout;
|
||||||
var id = port;
|
var id = port;
|
||||||
// just return the connection object if already have one
|
// just return the connection object if already have one
|
||||||
// key is the port (file path)
|
// key is the port (file path)
|
||||||
@ -260,6 +269,8 @@ module.exports = function(RED) {
|
|||||||
queue: [],
|
queue: [],
|
||||||
on: function(a,b) { this._emitter.on(a,b); },
|
on: function(a,b) { this._emitter.on(a,b); },
|
||||||
close: function(cb) { this.serial.close(cb); },
|
close: function(cb) { this.serial.close(cb); },
|
||||||
|
connect: function() { setupSerial(); },
|
||||||
|
disconnect: function(cb) { this.serial.close(cb); },
|
||||||
encodePayload: function (payload) {
|
encodePayload: function (payload) {
|
||||||
if (!Buffer.isBuffer(payload)) {
|
if (!Buffer.isBuffer(payload)) {
|
||||||
if (typeof payload === "object") {
|
if (typeof payload === "object") {
|
||||||
@ -483,6 +494,24 @@ module.exports = function(RED) {
|
|||||||
else {
|
else {
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
connect: function(port) {
|
||||||
|
if (connections[port]) {
|
||||||
|
try {
|
||||||
|
connections[port].connect();
|
||||||
|
}
|
||||||
|
catch(err) { }
|
||||||
|
}
|
||||||
|
},
|
||||||
|
disconnect: function(port) {
|
||||||
|
if (connections[port]) {
|
||||||
|
try {
|
||||||
|
connections[port].disconnect(function() {
|
||||||
|
RED.log.info(RED._("serial.errors.closed",{port:port}), {});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch(err) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user