Skip to content

Commit

Permalink
[serialport] Allow baudrate change at run time (#675)
Browse files Browse the repository at this point in the history
* Added baudrate change in serialport

* Added baudrate change to "serial request" (not only "serial out")

* Added doc
  • Loading branch information
Orfait committed Aug 27, 2020
1 parent ea72954 commit 53f32ec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions io/serialport/25-serial.html
Expand Up @@ -57,6 +57,7 @@
<script type="text/html" data-help-name="serial out">
<p>Provides a connection to an outbound serial port.</p>
<p>Only the <code>msg.payload</code> is sent.</p>
<p>Optionally the baudrate can be changed using <code>msg.baudrate</code></p>
<p>Optionally the new line character used to split the input can be appended to every message sent out to the serial port.</p>
<p>Binary payloads can be sent by using a Buffer object.</p>
</script>
Expand Down Expand Up @@ -111,6 +112,7 @@ <h3>Inputs</h3>
</li>
<li><code>msg.count</code> if set this will override the configured number of characters as long as it is less than the number configured.</li>
<li><code>msg.waitfor</code> 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>
</ul>
<h3>Outputs</h3>
<ul>
Expand Down
27 changes: 27 additions & 0 deletions io/serialport/25-serial.js
Expand Up @@ -41,6 +41,19 @@ module.exports = function(RED) {
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) {
Expand Down Expand Up @@ -121,6 +134,19 @@ module.exports = function(RED) {
node.port = serialPool.get(this.serialConfig);
// Serial Out
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
if (msg.hasOwnProperty("count") && (typeof msg.count === "number") && (node.serialConfig.out === "count")) {
node.serialConfig.newline = msg.count;
Expand Down Expand Up @@ -249,6 +275,7 @@ module.exports = function(RED) {
return payload;
},
write: function(m,cb) { this.serial.write(m,cb); },
update: function(m,cb) { this.serial.update(m,cb); },
enqueue: function(msg,sender,cb) {
var payload = this.encodePayload(msg.payload);
var qobj = {
Expand Down
3 changes: 2 additions & 1 deletion io/serialport/locales/en-US/25-serial.json
Expand Up @@ -66,7 +66,8 @@
"unexpected-close": "serial port __port__ closed unexpectedly",
"disconnected": "serial port __port__ disconnected",
"closed": "serial port __port__ closed",
"list": "Failed to list ports. Please enter manually."
"list": "Failed to list ports. Please enter manually.",
"badbaudrate": "Baudrate is not valid"
}
}
}
3 changes: 2 additions & 1 deletion io/serialport/locales/ja/25-serial.json
Expand Up @@ -51,7 +51,8 @@
"unexpected-close": "serial port __port__ closed unexpectedly",
"disconnected": "serial port __port__ disconnected",
"closed": "serial port __port__ closed",
"list": "ポートのリスト化に失敗しました。手動で入力してください。"
"list": "ポートのリスト化に失敗しました。手動で入力してください。",
"badbaudrate": "Baudrate is not valid"
}
}
}

0 comments on commit 53f32ec

Please sign in to comment.