Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial port connect/disconnect, configurable reconnect time #1012

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions io/serialport/25-serial.html
Expand Up @@ -213,7 +213,12 @@
</div>
<div class="form-row" style="padding-left:18px; margin-bottom:18px;">
<span data-i18n="serial.label.responsetimeout"></span>
<input type="text" id="node-config-input-responsetimeout" style="width:60px; height:28px;">
<input type="text" id="node-config-input-responsetimeout" style="width:120px; height:28px;">
<span data-i18n="serial.label.ms"></span>
</div>
<div class="form-row" style="padding-left:18px; margin-bottom:18px;">
<span data-i18n="serial.label.serialReconnectTime"></span>
<input type="number" id="node-config-input-serialReconnectTime" style="width:120px; height:28px;">
<span data-i18n="serial.label.ms"></span>
</div>
</div>
Expand Down Expand Up @@ -245,7 +250,8 @@
bin: {value:"false"},
out: {value:"char"},
addchar: {value:""},
responsetimeout: {value: 10000}
responsetimeout: {value: 10000},
serialReconnectTime: {value: 15000}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't set this here - as then settings.js would never be able to override... must default to blank

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it, now its an empty string.

},
label: function() {
this.serialbaud = this.serialbaud || 57600;
Expand Down
41 changes: 38 additions & 3 deletions io/serialport/25-serial.js
Expand Up @@ -5,7 +5,6 @@ module.exports = function(RED) {
var events = require("events");
const { SerialPort } = require('serialport');
var bufMaxSize = 32768; // Max serial buffer size, for inputs...
const serialReconnectTime = settings.serialReconnectTime || 15000;

// TODO: 'serialPool' should be encapsulated in SerialPortNode

Expand All @@ -27,6 +26,7 @@ module.exports = function(RED) {
this.out = n.out || "char";
this.waitfor = n.waitfor || "";
this.responsetimeout = n.responsetimeout || 10000;
this.serialReconnectTime = n.serialReconnectTime || settings.serialReconnectTime || 15000;
}
RED.nodes.registerType("serial-port",SerialPortNode);

Expand All @@ -42,7 +42,14 @@ module.exports = function(RED) {
node.port = serialPool.get(this.serialConfig);

node.on("input",function(msg) {
if (msg.hasOwnProperty("baudrate")) {
if (msg.hasOwnProperty("action") && this.serialConfig) {
if(msg.action == "disconnect"){
serialPool.disconnect(this.serialConfig.serialport);
}else if(msg.action == "connect"){
serialPool.connect(this.serialConfig.serialport);
}
}
if (msg.hasOwnProperty("baudrate")) {
var baud = parseInt(msg.baudrate);
if (isNaN(baud)) {
node.error(RED._("serial.errors.badbaudrate"),msg);
Expand Down Expand Up @@ -135,6 +142,13 @@ module.exports = function(RED) {
node.port = serialPool.get(this.serialConfig);
// Serial Out
node.on("input",function(msg) {
if (msg.hasOwnProperty("action") && this.serialConfig) {
if(msg.action == "disconnect"){
serialPool.disconnect(this.serialConfig.serialport);
}else if(msg.action == "connect"){
serialPool.connect(this.serialConfig.serialport);
}
}
if (msg.hasOwnProperty("baudrate")) {
var baud = parseInt(msg.baudrate);
if (isNaN(baud)) {
Expand Down Expand Up @@ -219,7 +233,8 @@ module.exports = function(RED) {
waitfor = serialConfig.waitfor,
binoutput = serialConfig.bin,
addchar = serialConfig.addchar,
responsetimeout = serialConfig.responsetimeout;
responsetimeout = serialConfig.responsetimeout,
serialReconnectTime = serialConfig.serialReconnectTime;
var id = port;
// just return the connection object if already have one
// key is the port (file path)
Expand Down Expand Up @@ -260,6 +275,8 @@ module.exports = function(RED) {
queue: [],
on: function(a,b) { this._emitter.on(a,b); },
close: function(cb) { this.serial.close(cb); },
connect: function() { setupSerial(); },
disconnect: function(cb) { this.serial.close(cb); },
encodePayload: function (payload) {
if (!Buffer.isBuffer(payload)) {
if (typeof payload === "object") {
Expand Down Expand Up @@ -483,6 +500,24 @@ module.exports = function(RED) {
else {
done();
}
},
connect: function(port) {
if (connections[port]) {
try {
connections[port].connect();
}
catch(err) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to do nothing ? should this raise an error - or set node.status at least ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try catch was not necessory as we are using existing functions to connect so it should be handled in there.

}
},
disconnect: function(port) {
if (connections[port]) {
try {
connections[port].disconnect(function() {
RED.log.info(RED._("serial.errors.closed",{port:port}), {});
});
}
catch(err) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to do nothing ? should this raise an error - or set node.status at least ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, try catch was not necessory as we are using existing function to disconnect so it should be handled in there.

}
}
}
}());
Expand Down
3 changes: 3 additions & 0 deletions io/serialport/locales/en-US/25-serial.html
Expand Up @@ -32,6 +32,8 @@ <h3>Inputs</h3>
<dd>data to be sent via the serial port</dd>
<dt class="optional">baudrate <span class="property-type">string</span></dt>
<dd>baudrate of the serial port (optional)</dd>
<dt class="optional">action <span class="property-type">string</span></dt>
<dd>disconnect/reconnect the serial port eg. {action:"connect"} or {action:"disconnect"}(optional)</dd>
</dl>
<p>Only the <code>msg.payload</code> is sent.</p>
<p>Optionally the baudrate can be changed using <code>msg.baudrate</code>.</p>
Expand Down Expand Up @@ -62,6 +64,7 @@ <h3>Inputs</h3>
<li><code>msg.waitfor</code> must be a single character, escape code, or hex code.
If set, the node will wait until it matches that character in the stream and then start the output.</li>
<li>Optionally the baudrate can be changed using <code>msg.baudrate</code></li>
<li><code>msg.action</code> disconnect/reconnect the serial port eg. {action:"connect"} or {action:"disconnect"} (optional)</li>
</ul>
<h3>Outputs</h3>
<ul>
Expand Down
1 change: 1 addition & 0 deletions io/serialport/locales/en-US/25-serial.json
Expand Up @@ -17,6 +17,7 @@
"output": "Output",
"request": "Request",
"responsetimeout": "Default response timeout",
"serialReconnectTime": "Default Reconnect time",
"ms": "ms",
"serial": "serial",
"none": "none",
Expand Down