diff --git a/io/serialport/25-serial.html b/io/serialport/25-serial.html index 30469ddd7..596173fd0 100644 --- a/io/serialport/25-serial.html +++ b/io/serialport/25-serial.html @@ -100,6 +100,10 @@ + + + + \ No newline at end of file diff --git a/io/serialport/25-serial.js b/io/serialport/25-serial.js index 64093ceb9..66703c760 100644 --- a/io/serialport/25-serial.js +++ b/io/serialport/25-serial.js @@ -25,101 +25,115 @@ module.exports = function(RED) { this.dsr = n.dsr || "none"; this.bin = n.bin || "false"; this.out = n.out || "char"; + this.enable = n.enable || true; this.waitfor = n.waitfor || ""; this.responsetimeout = n.responsetimeout || 10000; - } - RED.nodes.registerType("serial-port",SerialPortNode); + + this.changePort = (serialPort) => { + serialPool.close(this.serialport,() => {}); + this.serialport = serialPort.serialport || this.serialport; + this.serialbaud = parseInt(serialPort.serialbaud) || this.serialbaud; + this.databits = parseInt(serialPort.databits) || this.databits; + this.parity = serialPort.parity || this.parity; + this.stopbits = parseInt(serialPort.stopbits) || this.stopbits; + this.dtr = serialPort.dtr || this.dtr; + this.rts = serialPort.rts || this.rts; + this.cts = serialPort.cts || this.cts; + this.dsr = serialPort.dsr || this.dsr; + this.bin = serialPort.bin || this.bin; + this.out = serialPort.out || this.out; + } + }; + RED.nodes.registerType("serial-port",SerialPortNode); // receives msgs and sends them to the serial port function SerialOutNode(n) { RED.nodes.createNode(this,n); - this.serial = n.serial; - this.serialConfig = RED.nodes.getNode(this.serial); + this.serialConfig = RED.nodes.getNode(n.serial); - if (this.serialConfig) { - var node = this; - node.port = serialPool.get(this.serialConfig); - - node.on("input",function(msg) { - if (msg.hasOwnProperty("baudrate")) { - var baud = parseInt(msg.baudrate); - if (isNaN(baud)) { - node.error(RED._("serial.errors.badbaudrate"),msg); - } else { - node.port.update({baudRate: baud},function(err,res) { - if (err) { - var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path); - node.error(errmsg,msg); - } - }); - } - } - if (!msg.hasOwnProperty("payload")) { return; } // do nothing unless we have a payload - var payload = node.port.encodePayload(msg.payload); - node.port.write(payload,function(err,res) { - if (err) { - var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path); - node.error(errmsg,msg); - } - }); - }); - node.port.on('ready', function() { - node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); - }); - node.port.on('closed', function() { - node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"}); - }); - } - else { + if (!this.serialConfig) { this.error(RED._("serial.errors.missing-conf"), {}); + return; } - this.on("close", function(done) { - if (this.serialConfig) { - serialPool.close(this.serialConfig.serialport,done); - } - else { - done(); + this.serial = n.serial; + var node = this; + node.port = serialPool.get(this.serialConfig); + var serialConfig = this.serialConfig; + + this.serialConfig.on('start', function() { + node.port = serialPool.get(serialConfig); + }); + + node.on("input",function(msg) { + if (msg.hasOwnProperty("baudrate")) { + var baud = parseInt(msg.baudrate); + if (isNaN(baud)) { + node.error(RED._("serial.errors.badbaudrate"),msg); + } else { + node.port.update({baudRate: baud},function(err,res) { + if (err) { + var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path); + node.error(errmsg,msg); + } + }); + } } + if (!msg.hasOwnProperty("payload")) { return; } // do nothing unless we have a payload + var payload = node.port.encodePayload(msg.payload); + node.port.write(payload,function(err,res) { + if (err) { + var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path); + node.error(errmsg,msg); + } + }); + }); + node.port.on('ready', function() { + node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); + }); + node.port.on('closed', function() { + node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"}); }); } RED.nodes.registerType("serial out",SerialOutNode); - // receives data from the serial port and emits msgs function SerialInNode(n) { RED.nodes.createNode(this,n); + this.serialConfig = RED.nodes.getNode(n.serial); + + if (!this.serialConfig) { + this.error(RED._("serial.errors.missing-conf"), {}); + return; + } + this.serial = n.serial; - this.serialConfig = RED.nodes.getNode(this.serial); + var node = this; - if (this.serialConfig) { - var node = this; + this.serialConfig.on('start', function() { + setCallback(node, node.serialConfig); + }); + + this.on("close", function(done) { + serialPool.close(this.serialConfig.serialport,done); + }); + + function setCallback(node) { node.status({fill:"grey",shape:"dot",text:"node-red:common.status.not-connected"}); - node.port = serialPool.get(this.serialConfig); + node.port = serialPool.get(node.serialConfig); - this.port.on('data', function(msgout) { + node.port.on('data', function(msgout) { node.send(msgout); }); - this.port.on('ready', function() { + node.port.on('ready', function() { node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); }); - this.port.on('closed', function() { + node.port.on('closed', function() { node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"}); }); - } - else { - this.error(RED._("serial.errors.missing-conf"), {}); - } - - this.on("close", function(done) { - if (this.serialConfig) { - serialPool.close(this.serialConfig.serialport,done); - } - else { - done(); - } - }); + }; + setCallback(node) } RED.nodes.registerType("serial in",SerialInNode); @@ -162,28 +176,36 @@ module.exports = function(RED) { }); }); - // Serial In - this.port.on('data', function(msgout, sender) { - // serial request will only process incoming data pertaining to its own request (i.e. when it's at the head of the queue) - if (sender !== node) { return; } - node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"}); - msgout.status = "OK"; - node.send(msgout); - }); - this.port.on('timeout', function(msgout, sender) { - if (sender !== node) { return; } - msgout.status = "ERR_TIMEOUT"; - node.status({fill:"red",shape:"ring",text:"serial.status.timeout"}); - node.send(msgout); + let serialConfig = this.serialConfig; + serialConfig.on('start', function() { + node.port = serialPool.get(serialConfig); + setCallback(node); }); - // Common part - node.port.on('ready', function() { - node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"}); - }); - node.port.on('closed', function() { - node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"}); - }); + // Serial In + function setCallback(node) { + node.port.on('data', function (msgout, sender) { + // serial request will only process incoming data pertaining to its own request (i.e. when it's at the head of the queue) + if (sender !== node) { return; } + node.status({ fill: "green", shape: "dot", text: "node-red:common.status.ok" }); + msgout.status = "OK"; + node.send(msgout); + }); + node.port.on('timeout', function (msgout, sender) { + if (sender !== node) { return; } + msgout.status = "ERR_TIMEOUT"; + node.status({ fill: "red", shape: "ring", text: "serial.status.timeout" }); + node.send(msgout); + }); + + node.port.on('ready', function () { + node.status({ fill: "green", shape: "dot", text: "node-red:common.status.connected" }); + }); + node.port.on('closed', function () { + node.status({ fill: "red", shape: "ring", text: "node-red:common.status.not-connected" }); + }); + }; + setCallback(node); } else { this.error(RED._("serial.errors.missing-conf"), {}); @@ -200,6 +222,50 @@ module.exports = function(RED) { } RED.nodes.registerType("serial request", SerialRequestNode); + + // Serial Control Node + function PortSelectNode(n) { + const configProps = [ + "serialport", "serialbaud", "databits", "parity", "stopbits", + "dtr", "rts", "cts", "dsr", "bin", "out" + ] + RED.nodes.createNode(this,n); + this.serialConfig = RED.nodes.getNode(n.serial); + + if (!this.serialConfig) { + this.error(RED._("serial.errors.missing-conf"), {}); + return; + } + + this.serial = n.serial; + var node = this; + node.port = serialPool.get(this.serialConfig); + node.on("input",function(msg) { + if (configProps.some((p) =>{return msg.payload.hasOwnProperty(p)})) { + msg.payload.enable = msg.payload.hasOwnProperty('enable') ? msg.payload.enable : true; + node.serialConfig.changePort(msg.payload); + } + if (msg.payload.hasOwnProperty("enable")) { + // if any of config parameters or enable property is passed, do this control + node.serialConfig.enable = msg.payload.enable; + if (msg.payload.enable === true) { + node.serialConfig.emit('start'); + } else { + serialPool.close(node.serialConfig.serialport,() => { + RED.log.info("[serialconfig:"+node.serialConfig.id+"] " + RED._("serial.stopped",{port:node.serialConfig.serialport})); + }); + } + } + let currentConfig = {}; + configProps.map((p) => { + currentConfig[p] = node.serialConfig[p]; + }); + currentConfig.enable = node.serialConfig.enable; + node.send({payload: currentConfig}); + }); + } + RED.nodes.registerType("serial control", PortSelectNode); + var serialPool = (function() { var connections = {}; return { @@ -334,6 +400,7 @@ module.exports = function(RED) { }, } //newline = newline.replace("\\n","\n").replace("\\r","\r"); + obj._emitter.setMaxListeners(50); var olderr = ""; var setupSerial = function() { obj.serial = new SerialPort({ diff --git a/io/serialport/README.md b/io/serialport/README.md index a5965222c..423eb7b16 100644 --- a/io/serialport/README.md +++ b/io/serialport/README.md @@ -55,3 +55,50 @@ This node behaves as a tightly coupled combination of serial in and serial out n Send the request message in `msg.payload` as you would do with a serial out node. The message will be forwarded to the serial port following a strict FIFO (First In, First Out) queue, waiting for a single response before transmitting the next request. Once a response is received (with the same logic of a serial in node), or after a timeout occurs, a message is produced on the output, with msg.payload containing the received response (or missing in case if timeout), msg.status containing relevant info, and all other fields preserved. For consistency with the serial in node, msg.port is also set to the name of the port selected. + +### Serial Control +When the node-red starts, the flow(program) picks up the pre-programmed serial port, open it, and start the communication. But there are some cases the port needs to switch to a different port, stop, and start again. For example, in order to upload a new binary for Arduino, the serial port needs to be stopped relased from the nodered, and start it again after uploading. Or when the FTDI device re-connects after disconnected for any reason, it may be possible the port number change, and the end user of the flow can't change the port. + + +This `Serial Control` node provides the serial port control capability to +1. change the serial port and its configuration on the run time programatically. +2. stop the communication and releasing the serial port so, for example the Arduino can upload the new binary without shutting down the nodered. +3. start the communication after stopped with this `Serial Control` node for above reason or the like. + +
In order to control the communication, just send these JSON messages to the control node.
++ { + "serialport": "/dev/tty.usbmodem1234561", + "serialbaud": 115200, + "databits": 8, + "parity": "none", + "stopbits": 1 + "enable": true + } ++changes the serial port and the configuration on the fly. +
The following optional parameters will change the configuration only if they are present.
+Any combination of them can be passed to change/control the serial communication
+Optionally the new line character used to split the input can be appended to every message sent out to the serial port.
+ + \ No newline at end of file diff --git a/io/serialport/locales/en-US/25-serial.json b/io/serialport/locales/en-US/25-serial.json index c15c6d447..0bd82f7e1 100644 --- a/io/serialport/locales/en-US/25-serial.json +++ b/io/serialport/locales/en-US/25-serial.json @@ -68,6 +68,7 @@ "closed": "serial port __port__ closed", "list": "Failed to list ports. Please enter manually.", "badbaudrate": "Baudrate is invalid" - } + }, + "stopped": "__port__ stopped" } }