Fix serial port add char logic

to close #450
This commit is contained in:
Dave Conway-Jones 2018-06-02 13:24:57 +01:00
parent 4d0360d94a
commit 997458c78e
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
4 changed files with 28 additions and 37 deletions

View File

@ -86,7 +86,7 @@
<script type="text/x-red" data-template-name="serial-port"> <script type="text/x-red" data-template-name="serial-port">
<div class="form-row"> <div class="form-row">
<label for="node-config-input-serialport"><i class="fa fa-random"></i> <span data-i18n="serial.label.serialport"></span></label> <label for="node-config-input-serialport"><i class="fa fa-random"></i> <span data-i18n="serial.label.serialport"></span></label>
<input type="text" id="node-config-input-serialport" style="width:60%;" data-i18n="[placeholder]serial.placeholder.serialport"> <input type="text" id="node-config-input-serialport" style="width:66%;" data-i18n="[placeholder]serial.placeholder.serialport">
<a id="node-config-lookup-serial" class="btn"><i id="node-config-lookup-serial-icon" class="fa fa-search"></i></a> <a id="node-config-lookup-serial" class="btn"><i id="node-config-lookup-serial-icon" class="fa fa-search"></i></a>
</div> </div>
<div class="form-row"> <div class="form-row">
@ -98,16 +98,16 @@
<td width="70px" data-i18n="serial.label.stopbits"></td> <td width="70px" data-i18n="serial.label.stopbits"></td>
</tr><tr><td>&nbsp;</td> </tr><tr><td>&nbsp;</td>
<td> <td>
<input type="text" id="node-config-input-serialbaud" style="width:100px"> <input type="text" id="node-config-input-serialbaud" style="width:92%">
</td><td> </td><td>
<select type="text" id="node-config-input-databits" style="width:70px;"> <select type="text" id="node-config-input-databits" style="width:90%;">
<option value="8">8</option> <option value="8">8</option>
<option value="7">7</option> <option value="7">7</option>
<option value="6">6</option> <option value="6">6</option>
<option value="5">5</option> <option value="5">5</option>
</select> </select>
</td><td> </td><td>
<select type="text" id="node-config-input-parity" style="width:80px;"> <select type="text" id="node-config-input-parity" style="width:90%;">
<option value="none" data-i18n="serial.parity.none"></option> <option value="none" data-i18n="serial.parity.none"></option>
<option value="even" data-i18n="serial.parity.even"></option> <option value="even" data-i18n="serial.parity.even"></option>
<option value="mark" data-i18n="serial.parity.mark"></option> <option value="mark" data-i18n="serial.parity.mark"></option>
@ -115,29 +115,29 @@
<option value="space" data-i18n="serial.parity.space"></option> <option value="space" data-i18n="serial.parity.space"></option>
</select> </select>
</td><td> </td><td>
<select type="text" id="node-config-input-stopbits" style="width:70px;"> <select type="text" id="node-config-input-stopbits" style="width:90%;">
<option value="2">2</option> <option value="2">2</option>
<option value="1">1</option> <option value="1">1</option>
</select> </select>
</td> </td></tr></table>
</tr></table></div> </div>
<br/> <br/>
<div class="form-row"> <div class="form-row">
<label><i class="fa fa-sign-in"></i> <span data-i18n="serial.label.input"></span></label> <label><i class="fa fa-sign-in"></i> <span data-i18n="serial.label.input"></span></label>
</div> </div>
<div class="form-row" style="padding-left: 10px;"> <div class="form-row" style="padding-left:10px;">
<span data-i18n="serial.label.split"></span> <span data-i18n="serial.label.split"></span>
<select type="text" id="node-config-input-out" style="margin-left: 5px; width:200px;"> <select type="text" id="node-config-input-out" style="margin-left:5px; width:200px;">
<option value="char" data-i18n="serial.split.character"></option> <option value="char" data-i18n="serial.split.character"></option>
<option value="time" data-i18n="serial.split.timeout"></option> <option value="time" data-i18n="serial.split.timeout"></option>
<option value="count" data-i18n="serial.split.lengths"></option> <option value="count" data-i18n="serial.split.lengths"></option>
</select> </select>
<input type="text" id="node-config-input-newline" style="width:50px;"> <input type="text" id="node-config-input-newline" style="width:50px;">
<span id="node-units"></span> <span id="node-units"></span>
</div> </div>
<div class="form-row" style="padding-left: 10px;"> <div class="form-row" style="padding-left:10px;">
<span data-i18n="serial.label.deliver"></span> <span data-i18n="serial.label.deliver"></span>
<select type="text" id="node-config-input-bin" style="margin-left: 5px; width: 150px;"> <select type="text" id="node-config-input-bin" style="margin-left:5px; width:150px;">
<option value="false" data-i18n="serial.output.ascii"></option> <option value="false" data-i18n="serial.output.ascii"></option>
<option value="bin" data-i18n="serial.output.binary"></option> <option value="bin" data-i18n="serial.output.binary"></option>
</select> </select>
@ -148,7 +148,7 @@
<label><i class="fa fa-sign-out"></i> <span data-i18n="serial.label.output"></span></label> <label><i class="fa fa-sign-out"></i> <span data-i18n="serial.label.output"></span></label>
</div> </div>
<div class="form-row"> <div class="form-row">
<input style="width: 30px;margin-left: 10px; vertical-align: top;" type="checkbox" id="node-config-input-addchar"><label style="width: auto;" for="node-config-input-addchar"><span data-i18n="serial.addsplit"></span></label> <input style="width:30px; margin-left:10px; vertical-align:top;" type="checkbox" id="node-config-input-addchar"><label style="width:auto;" for="node-config-input-addchar"><span data-i18n="serial.addsplit"></span></label>
</div> </div>
</div> </div>
<div class="form-tips" id="tip-split"><span data-i18n="serial.tip.split"></span></div> <div class="form-tips" id="tip-split"><span data-i18n="serial.tip.split"></span></div>

View File

@ -36,9 +36,13 @@ module.exports = function(RED) {
this.serialConfig.stopbits, this.serialConfig.stopbits,
this.serialConfig.newline); this.serialConfig.newline);
node.addCh = ""; node.addCh = "";
if (node.serialConfig.addchar == "true" || node.serialConfig.addchar === true) { if (node.serialConfig.newline.substr(0,2) == "0x") {
node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0"); // jshint ignore:line node.addCh = new Buffer([parseInt(node.serialConfig.newline)]);
} }
else {
node.addCh = new Buffer(node.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0")); // jshint ignore:line
}
node.on("input",function(msg) { node.on("input",function(msg) {
if (msg.hasOwnProperty("payload")) { if (msg.hasOwnProperty("payload")) {
var payload = msg.payload; var payload = msg.payload;
@ -49,10 +53,10 @@ module.exports = function(RED) {
else { else {
payload = payload.toString(); payload = payload.toString();
} }
if (node.out === "char") { payload += node.addCh; } if ((node.serialConfig.out === "char") && (node.serialConfig.addchar === true)) { payload += node.addCh; }
} }
else if ((node.addCh !== "") && (node.out === "char")) { else if ((node.serialConfig.out === "char") && (node.serialConfig.addchar === true) && (node.addCh !== "")) {
payload = Buffer.concat([payload,new Buffer(node.addCh)]); payload = Buffer.concat([payload,node.addCh]);
} }
node.port.write(payload,function(err,res) { node.port.write(payload,function(err,res) {
if (err) { if (err) {

View File

@ -1,37 +1,24 @@
node-red-node-serialport node-red-node-serialport
======================== ========================
<a href="http://nodered.org" target="_new">Node-RED</a> nodes to talk to <a href="http://nodered.org" target="noderedinfo">Node-RED</a> nodes to talk to
hardware Serial ports. hardware Serial ports.
Install ## Install
-------
This node is usually installed by default in Node-RED so should not need to be installed manually. This node is sometimes installed by default in Node-RED so may not need to be installed manually.
Run the following command in your Node-RED user directory (typically `~/.node-red`): Run the following command in your Node-RED user directory (typically `~/.node-red`):
npm i node-red-node-serialport npm i node-red-node-serialport
For versions on node.js prior to 4.x (ie v0.10.x and v0.12.x) please install using
sudo npm i -g npm@2.x
npm i node-red-node-serialport
You may also have to install or upgrade GCC to be version 4.8 or better.
Alternatively you can simply install the older version of this node.
npm install node-red-node-serialport@0.0.5
During install there may be multiple messages about optional compilation. During install there may be multiple messages about optional compilation.
These may look like failures... as they report as failure to compile errors - These may look like failures... as they report as failure to compile errors -
but often are warnings and the node will continue to install and, assuming nothing else but often are warnings and the node will continue to install and, assuming nothing else
failed, you should be able to use it. Occasionally some platforms *will* require failed, you should be able to use it. Occasionally some platforms *will* require
you to install the full set of tools in order to compile the underlying package. you to install the full set of tools in order to compile the underlying package.
## Usage
Usage
-----
Provides two nodes - one to receive messages, and one to send. Provides two nodes - one to receive messages, and one to send.
@ -59,4 +46,4 @@ Provides a connection to an outbound serial port.
Only the `msg.payload` is sent. Only the `msg.payload` is sent.
Optionally the new line character used to split the input can be appended to every message sent out to the serial port. Optionally the character used to split the input can be appended to every message sent out to the serial port.

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-serialport", "name" : "node-red-node-serialport",
"version" : "0.6.6", "version" : "0.6.7",
"description" : "Node-RED nodes to talk to serial ports", "description" : "Node-RED nodes to talk to serial ports",
"dependencies" : { "dependencies" : {
"serialport" : "^6.2.0" "serialport" : "^6.2.0"