1
0
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:
Dave C-J 2013-12-08 16:59:36 +00:00
parent 67e16adfd0
commit fae34f8244

View File

@ -39,12 +39,7 @@ function SerialOutNode(n) {
if (this.serialConfig) { if (this.serialConfig) {
var node = this; var node = this;
try { node.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
node.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
} catch(err) {
this.error(err);
return;
}
node.addCh = ""; node.addCh = "";
if (node.serialConfig.addchar == "true") { node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r"); } if (node.serialConfig.addchar == "true") { node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r"); }
node.on("input",function(msg) { node.on("input",function(msg) {
@ -52,7 +47,9 @@ function SerialOutNode(n) {
if (typeof payload === "object") { payload = JSON.stringify(payload); } if (typeof payload === "object") { payload = JSON.stringify(payload); }
if (typeof payload !== "buffer") { payload = new String(payload) + node.addCh; } if (typeof payload !== "buffer") { payload = new String(payload) + node.addCh; }
node.port.write(payload,function(err,res) { node.port.write(payload,function(err,res) {
if (err) { node.error(err); } if (err) {
node.error(err);
}
}); });
}); });
} else { } else {
@ -75,13 +72,7 @@ function SerialInNode(n) {
if (this.serialConfig) { if (this.serialConfig) {
var node = this; var node = this;
try { this.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
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) { this.port.on('data', function(msg) {
node.send({ "payload": msg }); node.send({ "payload": msg });
}); });
@ -115,55 +106,54 @@ var serialPool = function() {
_closing: false, _closing: false,
tout: null, tout: null,
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); },
write: function(m,cb) { this.serial.write(m,cb)}, write: function(m,cb) { this.serial.write(m,cb); },
} }
newline = newline.replace("\\n","\n").replace("\\r","\r"); newline = newline.replace("\\n","\n").replace("\\r","\r");
var setupSerial = function() { var setupSerial = function() {
try { if (newline == "") {
if (newline == "") { obj.serial = new serialp.SerialPort(port,{
obj.serial = new serialp.SerialPort(port,{ baudrate: baud,
baudrate: baud, parser: serialp.parsers.raw
parser: serialp.parsers.raw },true, function(err, results) { if (err) obj.serial.emit('error',err); });
}); }
} else {
else { obj.serial = new serialp.SerialPort(port,{
obj.serial = new serialp.SerialPort(port,{ baudrate: baud,
baudrate: baud, parser: serialp.parsers.readline(newline)
parser: serialp.parsers.readline(newline) },true, function(err, results) { if (err) obj.serial.emit('error',err); });
}); }
} obj.serial.on('error', function(err) {
obj.serial.on('error', function(err) { util.log("[serial] serial port "+port+" error "+err);
util.log("[serial] serial port "+port+" error "+err); obj.tout = setTimeout(function() {
setupSerial();
}, settings.serialReconnectTime);
});
obj.serial.on('close', function() {
if (!obj._closing) {
util.log("[serial] serial port "+port+" closed unexpectedly");
obj.tout = setTimeout(function() { obj.tout = setTimeout(function() {
setupSerial(); setupSerial();
},settings.serialReconnectTime); }, settings.serialReconnectTime);
}); }
obj.serial.on('close', function() { });
if (!obj._closing) { obj.serial.on('open',function() {
util.log("[serial] serial port "+port+" closed unexpectedly"); util.log("[serial] serial port "+port+" opened at "+baud+" baud");
obj.tout = setTimeout(function() { if (obj.tout) { clearTimeout(obj.tout); }
setupSerial(); obj.serial.flush();
},settings.serialReconnectTime); obj._emitter.emit('ready');
});
obj.serial.on('data',function(d) {
if (typeof d !== "string") {
d = d.toString();
for (i=0; i<d.length; i++) {
obj._emitter.emit('data',d.charAt(i));
} }
}); }
obj.serial.on('open',function() { else {
util.log("[serial] serial port "+port+" opened at "+baud+" baud"); obj._emitter.emit('data',d);
obj.serial.flush(); }
obj._emitter.emit('ready'); });
});
obj.serial.on('data',function(d) {
if (typeof d !== "string") {
d = d.toString();
for (i=0; i<d.length; i++) {
obj._emitter.emit('data',d.charAt(i));
}
}
else {
obj._emitter.emit('data',d);
}
});
} catch(err) { console.log("Booo!",err,"Booo!"); }
} }
setupSerial(); setupSerial();
return obj; return obj;