1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

[serialport] Allow baudrate change at run time (#675)

* Added baudrate change in serialport

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

* Added doc
This commit is contained in:
Orfait 2020-08-27 10:20:09 +02:00 committed by GitHub
parent ea729546d4
commit 53f32ec521
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View File

@ -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>
@ -111,6 +112,7 @@
</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>

View File

@ -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) {
@ -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;
@ -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 = {

View File

@ -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"
}
}
}

View File

@ -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"
}
}
}