From 751ac7b9ee981d542cecc5324952ea67b72ba1a8 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 13 Dec 2013 10:27:52 +0000 Subject: [PATCH] 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). --- nodes/core/io/25-serial.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/nodes/core/io/25-serial.js b/nodes/core/io/25-serial.js index 7c4271869..1c5774594 100644 --- a/nodes/core/io/25-serial.js +++ b/nodes/core/io/25-serial.js @@ -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."); } }); }