Handle Buffer objects properly in Serial out node

Fixes #115
The serial out node does a JSON.stringify if the payload is an object. This was incorrectly being applied to Buffer objects, causing the output seen in issue #115.

The Buffer is now passed through as-is (with the newline appended if so configured).
This commit is contained in:
Nick O'Leary 2013-12-13 10:27:52 +00:00
parent 344660dfee
commit 751ac7b9ee
1 changed files with 13 additions and 4 deletions

View File

@ -41,11 +41,21 @@ function SerialOutNode(n) {
var node = this;
node.port = serialPool.get(this.serialConfig.serialport,this.serialConfig.serialbaud,this.serialConfig.newline);
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) {
var payload = msg.payload;
if (typeof payload === "object") { payload = JSON.stringify(payload); }
if (typeof payload !== "buffer") { payload = new String(payload) + node.addCh; }
if (!Buffer.isBuffer(payload)) {
if (typeof payload === "object") {
payload = JSON.stringify(payload);
} else {
payload = new String(payload);
}
payload += node.addCh;
} else if (node.addCh !== "") {
payload = Buffer.concat([payload,new Buffer(node.addCh)]);
}
node.port.write(payload,function(err,res) {
if (err) {
node.error(err);
@ -86,7 +96,6 @@ function SerialInNode(n) {
serialPool.close(this.serialConfig.serialport);
} catch(err) {
}
this.warn("Deploying with serial-port nodes is known to occasionally cause Node-RED to hang. This is due to an open issue with the underlying module.");
}
});
}