mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Let serialport retry reconnects etc
Partial fix to Issue #111 This doesn't fix the screaming loop issue if you try to write to an unplugged serial port - but does attempt to fix the not retrying to reconnect part of the issue. Both were introduced by changes to the underlying serialport npm.
This commit is contained in:
parent
67e16adfd0
commit
fae34f8244
@ -39,12 +39,7 @@ function SerialOutNode(n) {
|
||||
|
||||
if (this.serialConfig) {
|
||||
var node = this;
|
||||
try {
|
||||
node.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
|
||||
} catch(err) {
|
||||
this.error(err);
|
||||
return;
|
||||
}
|
||||
node.addCh = "";
|
||||
if (node.serialConfig.addchar == "true") { node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r"); }
|
||||
node.on("input",function(msg) {
|
||||
@ -52,7 +47,9 @@ function SerialOutNode(n) {
|
||||
if (typeof payload === "object") { payload = JSON.stringify(payload); }
|
||||
if (typeof payload !== "buffer") { payload = new String(payload) + node.addCh; }
|
||||
node.port.write(payload,function(err,res) {
|
||||
if (err) { node.error(err); }
|
||||
if (err) {
|
||||
node.error(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
@ -75,13 +72,7 @@ function SerialInNode(n) {
|
||||
|
||||
if (this.serialConfig) {
|
||||
var node = this;
|
||||
try {
|
||||
this.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
|
||||
} catch(err) {
|
||||
this.error(err);
|
||||
return;
|
||||
}
|
||||
|
||||
this.port.on('data', function(msg) {
|
||||
node.send({ "payload": msg });
|
||||
});
|
||||
@ -115,40 +106,40 @@ var serialPool = function() {
|
||||
_closing: false,
|
||||
tout: null,
|
||||
on: function(a,b) { this._emitter.on(a,b); },
|
||||
close: function(cb) { this.serial.close(cb)},
|
||||
write: function(m,cb) { this.serial.write(m,cb)},
|
||||
close: function(cb) { this.serial.close(cb); },
|
||||
write: function(m,cb) { this.serial.write(m,cb); },
|
||||
}
|
||||
newline = newline.replace("\\n","\n").replace("\\r","\r");
|
||||
var setupSerial = function() {
|
||||
try {
|
||||
if (newline == "") {
|
||||
obj.serial = new serialp.SerialPort(port,{
|
||||
baudrate: baud,
|
||||
parser: serialp.parsers.raw
|
||||
});
|
||||
},true, function(err, results) { if (err) obj.serial.emit('error',err); });
|
||||
}
|
||||
else {
|
||||
obj.serial = new serialp.SerialPort(port,{
|
||||
baudrate: baud,
|
||||
parser: serialp.parsers.readline(newline)
|
||||
});
|
||||
},true, function(err, results) { if (err) obj.serial.emit('error',err); });
|
||||
}
|
||||
obj.serial.on('error', function(err) {
|
||||
util.log("[serial] serial port "+port+" error "+err);
|
||||
obj.tout = setTimeout(function() {
|
||||
setupSerial();
|
||||
},settings.serialReconnectTime);
|
||||
}, settings.serialReconnectTime);
|
||||
});
|
||||
obj.serial.on('close', function() {
|
||||
if (!obj._closing) {
|
||||
util.log("[serial] serial port "+port+" closed unexpectedly");
|
||||
obj.tout = setTimeout(function() {
|
||||
setupSerial();
|
||||
},settings.serialReconnectTime);
|
||||
}, settings.serialReconnectTime);
|
||||
}
|
||||
});
|
||||
obj.serial.on('open',function() {
|
||||
util.log("[serial] serial port "+port+" opened at "+baud+" baud");
|
||||
if (obj.tout) { clearTimeout(obj.tout); }
|
||||
obj.serial.flush();
|
||||
obj._emitter.emit('ready');
|
||||
});
|
||||
@ -163,7 +154,6 @@ var serialPool = function() {
|
||||
obj._emitter.emit('data',d);
|
||||
}
|
||||
});
|
||||
} catch(err) { console.log("Booo!",err,"Booo!"); }
|
||||
}
|
||||
setupSerial();
|
||||
return obj;
|
||||
|
Loading…
Reference in New Issue
Block a user