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
Changes from 11 commits
37733df
728bbc3
05e3a1b
cbfbfc8
b1855ab
69cc1b5
c04f666
6a2c2d9
7e509d8
d82707a
542d837
0c0f45b
64f12e8
b33f4ed
8ba7b4e
03aa77a
7a92fc5
d73bb47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
@@ -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)) { | ||
|
@@ -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) | ||
|
@@ -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") { | ||
|
@@ -483,6 +500,24 @@ module.exports = function(RED) { | |
else { | ||
done(); | ||
} | ||
}, | ||
connect: function(port) { | ||
if (connections[port]) { | ||
try { | ||
connections[port].connect(); | ||
} | ||
catch(err) { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
} | ||
} | ||
}()); | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.