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,23 +106,22 @@ 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);
|
||||
@ -149,6 +139,7 @@ var serialPool = function() {
|
||||
});
|
||||
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