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

Add binary capabilities to Serial node - along with timeout, fixed

buffer size and binary output options. Also updated icons and added strict
checking.
This commit is contained in:
Dave C-J 2014-06-08 14:57:57 +01:00
parent c10ed13322
commit 0cbc277a2c
2 changed files with 157 additions and 48 deletions

View File

@ -1,5 +1,5 @@
<!-- <!--
Copyright 2013 IBM Corp. Copyright 2013,2014 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -16,18 +16,19 @@
<script type="text/x-red" data-template-name="serial in"> <script type="text/x-red" data-template-name="serial in">
<div class="form-row node-input-serial"> <div class="form-row node-input-serial">
<label for="node-input-serial"><i class="icon-bullhorn"></i> Serial Port</label> <label for="node-input-serial"><i class="fa fa-random"></i> Serial Port</label>
<input type="text" id="node-input-serial"> <input type="text" id="node-input-serial">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
</script> </script>
<script type="text/x-red" data-help-name="serial in"> <script type="text/x-red" data-help-name="serial in">
<p>Reads data from a local serial port.</p> <p>Reads data from a local serial port.</p>
<p>Keeps reading from the serial port until it sees \n (default) or the character(s) requested. Only sets <b>msg.payload</b>.</p> <p>In ascii mode keeps reading from the serial port until it sees \n (default) or the character(s) requested. Outputs <b>msg.payload</b> as a <i>string</i>.</p>
<p>In binary mode can either output buffers of a fixed size, or wait for a timeout and then send whatever has accumlated. Outputs <b>msg.payload</b> as a <i>buffer</i>.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -53,11 +54,11 @@
<script type="text/x-red" data-template-name="serial out"> <script type="text/x-red" data-template-name="serial out">
<div class="form-row node-input-serial"> <div class="form-row node-input-serial">
<label for="node-input-serial"><i class="icon-bullhorn"></i> Serial Port</label> <label for="node-input-serial"><i class="fa fa-random"></i> Serial Port</label>
<input type="text" id="node-input-serial"> <input type="text" id="node-input-serial">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
</script> </script>
@ -93,13 +94,13 @@
<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="icon-bullhorn"></i> Serial Port</label> <label for="node-config-input-serialport"><i class="fa fa-random"></i> Serial Port</label>
<input type="text" id="node-config-input-serialport" style="width:60%;" placeholder="/dev/ttyUSB0"/> <input type="text" id="node-config-input-serialport" style="width:60%;" placeholder="/dev/ttyUSB0"/>
<a id="node-config-lookup-serial" class="btn"><i id="node-config-lookup-serial-icon" class="icon icon-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">
<table><tr> <table><tr>
<td width = "102px"><i class="icon-wrench"></i> Settings</td> <td width = "102px"><i class="fa fa-wrench"></i> Settings</td>
<td width = "100px">Baud Rate</td> <td width = "100px">Baud Rate</td>
<td width = "80px">Data Bits</td> <td width = "80px">Data Bits</td>
<td width = "80px">Parity</td> <td width = "80px">Parity</td>
@ -146,19 +147,66 @@
<option value="1">1</option> <option value="1">1</option>
</select> </select>
</td> </td>
</tr></table> </tr></table><br/>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-newline"><i class="icon-text-width"></i> New line</label> <label for="node-config-input-out"><i class="fa fa-cut"></i> Split input</label>
<input type="text" id="node-config-input-newline" style="width: 50px;"> <select type="text" id="node-config-input-out" style="width:52%;">
<option value="char">when character received is</option>
<option value="time">after a fixed timeout of</option>
<option value="count">a fixed number of characters</option>
</select>
<input type="text" id="node-config-input-newline" style="width:50px;">
<span id="node-units"></span>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-config-input-addchar">&nbsp;</label> <label for="node-config-input-bin"><i class="fa fa-sign-in"></i> and deliver</label>
<select type="text" id="node-config-input-addchar" style="width: 70%;"> <select type="text" id="node-config-input-bin" style="width: 77%;">
<option value="false">Don't add 'New Line' to any output messages</option> <option value="false">ascii strings</option>
<option value="true">Add 'New line' to output messages</option> <option value="bin">binary buffers</option>
</select> </select>
</div> </div>
<div class="form-tips">Tip: the new line character is used to split the input into separate messages. It can also be added to every message sent out to the serial port.</div> <br/>
<div class="form-row" id="node-config-addchar">
<label for="node-config-input-addchar"><i class="fa fa-sign-out"></i> On output</label>
<select type="text" id="node-config-input-addchar" style="width: 77%;">
<option value="false">don't add 'split' character to output messages</option>
<option value="true">add 'split' character to output messages</option>
</select>
</div>
<div class="form-tips" id="tip-split">Tip: the "Split on" character is used to split the input into separate messages. It can also be added to every message sent out to the serial port.</div>
<div class="form-tips" id="tip-bin" hidden>Tip: In timeout mode timeout starts from arrival of first character.</div>
<script>
var previous = null;
$("#node-config-input-out").on('focus', function () { previous = this.value; }).change(function() {
if (previous == null) { previous = $("#node-config-input-out").val(); }
if ($("#node-config-input-out").val() == "char") {
if (previous != "char") { $("#node-config-input-newline").val("\\n"); }
$("#node-units").text("");
$("#node-config-addchar").show();
$("#tip-split").show();
$("#tip-bin").hide();
}
else if ($("#node-config-input-out").val() == "time") {
if (previous != "time") { $("#node-config-input-newline").val("0"); }
$("#node-units").text("ms");
$("#node-config-addchar").hide();
$("#node-config-input-addchar").val("false");
$("#tip-split").hide();
$("#tip-bin").show();
}
else {
if (previous != "count") { $("#node-config-input-newline").val("12"); }
$("#node-units").text("chars");
$("#node-config-addchar").hide();
$("#node-config-input-addchar").val("false");
$("#tip-split").hide();
$("#tip-bin").hide();
}
});
</script>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -172,6 +220,8 @@
parity: {value:"none",required:true}, parity: {value:"none",required:true},
stopbits: {value:1,required:true}, stopbits: {value:1,required:true},
newline: {value:"\\n"}, newline: {value:"\\n"},
bin: {value:""},
out: {value:""},
addchar: {value:false} addchar: {value:false}
}, },
label: function() { label: function() {
@ -187,13 +237,12 @@
} catch(err) { } catch(err) {
} }
$("#node-config-lookup-serial").click(function() { $("#node-config-lookup-serial").click(function() {
$("#node-config-lookup-serial-icon").removeClass('icon-search'); //$("#node-config-lookup-serial-icon").removeClass('fa fa-search');
$("#node-config-lookup-serial-icon").addClass('spinner'); //$("#node-config-lookup-serial-icon").addClass('fa fa-spinner');
$("#node-config-lookup-serial").addClass('disabled'); $("#node-config-lookup-serial").addClass('disabled');
$.getJSON('serialports',function(data) { $.getJSON('serialports',function(data) {
$("#node-config-lookup-serial-icon").addClass('icon-search'); //$("#node-config-lookup-serial-icon").addClass('fa fa-search');
$("#node-config-lookup-serial-icon").removeClass('spinner'); //$("#node-config-lookup-serial-icon").removeClass('fa fa-spinner');
$("#node-config-lookup-serial").removeClass('disabled'); $("#node-config-lookup-serial").removeClass('disabled');
var ports = []; var ports = [];
$.each(data, function(i, port){ $.each(data, function(i, port){

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013,2014 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,10 +15,12 @@
**/ **/
module.exports = function(RED) { module.exports = function(RED) {
"use strict";
var settings = RED.settings; var settings = RED.settings;
var events = require("events"); var events = require("events");
var util = require("util"); var util = require("util");
var serialp = require("serialport"); var serialp = require("serialport");
var bufMaxSize = 32768; // Max serial buffer size, for inputs...
// TODO: 'serialPool' should be encapsulated in SerialPortNode // TODO: 'serialPool' should be encapsulated in SerialPortNode
@ -27,11 +29,12 @@ module.exports = function(RED) {
this.serialport = n.serialport; this.serialport = n.serialport;
this.newline = n.newline; this.newline = n.newline;
this.addchar = n.addchar || "false"; this.addchar = n.addchar || "false";
this.serialbaud = parseInt(n.serialbaud) || 57600; this.serialbaud = parseInt(n.serialbaud) || 57600;
this.databits = parseInt(n.databits) || 8; this.databits = parseInt(n.databits) || 8;
this.parity = n.parity || "none"; this.parity = n.parity || "none";
this.stopbits = parseInt(n.stopbits) || 1; this.stopbits = parseInt(n.stopbits) || 1;
this.bin = n.bin || "false";
this.out = n.out || "char";
} }
RED.nodes.registerType("serial-port",SerialPortNode); RED.nodes.registerType("serial-port",SerialPortNode);
@ -50,7 +53,7 @@ module.exports = function(RED) {
this.serialConfig.newline); this.serialConfig.newline);
node.addCh = ""; node.addCh = "";
if (node.serialConfig.addchar == "true") { if (node.serialConfig.addchar == "true") {
node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r"); node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0");
} }
node.on("input",function(msg) { node.on("input",function(msg) {
var payload = msg.payload; var payload = msg.payload;
@ -90,6 +93,7 @@ module.exports = function(RED) {
} }
RED.nodes.registerType("serial out",SerialOutNode); RED.nodes.registerType("serial out",SerialOutNode);
function SerialInNode(n) { function SerialInNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.serial = n.serial; this.serial = n.serial;
@ -97,15 +101,70 @@ module.exports = function(RED) {
if (this.serialConfig) { if (this.serialConfig) {
var node = this; var node = this;
node.tout = null;
var buf;
if (node.serialConfig.out != "count") { buf = new Buffer(bufMaxSize); }
else { buf = new Buffer(Number(node.serialConfig.newline)); }
var i = 0;
node.status({fill:"grey",shape:"dot",text:"unknown"}); node.status({fill:"grey",shape:"dot",text:"unknown"});
node.port = serialPool.get(this.serialConfig.serialport, node.port = serialPool.get(this.serialConfig.serialport,
this.serialConfig.serialbaud, this.serialConfig.serialbaud,
this.serialConfig.databits, this.serialConfig.databits,
this.serialConfig.parity, this.serialConfig.parity,
this.serialConfig.stopbits, this.serialConfig.stopbits,
this.serialConfig.newline); this.serialConfig.newline
);
var splitc = new Buffer(node.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0"));
this.port.on('data', function(msg) { this.port.on('data', function(msg) {
node.send({ "payload": msg }); // single char buffer
if (node.serialConfig.newline == 0) {
node.send({"payload": new Buffer([msg])});
}
else {
// do the timer thing
if (node.serialConfig.out === "time") {
if (node.tout) {
i += 1;
buf[i] = msg;
}
else {
node.tout = setTimeout(function () {
node.tout = null;
var m = new Buffer(i+1);
buf.copy(m,0,0,i+1);
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
node.send({"payload": m});
}, node.serialConfig.newline);
i = 0;
buf[0] = msg;
}
}
// count bytes into a buffer...
else if (node.serialConfig.out === "count") {
buf[i] = msg;
i += 1;
if ( i >= Number(node.serialConfig.newline)) {
var m = new Buffer(i);
buf.copy(m,0,0,i-1);
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
node.send({"payload":m});
i = 0;
}
}
// look to match char...
else if (node.serialConfig.out === "char") {
buf[i] = msg;
i += 1;
if ((msg === splitc[0]) || (i === bufMaxSize)) {
var m = new Buffer(i);
buf.copy(m,0,0,i);
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
node.send({"payload":m});
i = 0;
}
}
else { console.log("Should never get here"); }
}
}); });
this.port.on('ready', function() { this.port.on('ready', function() {
node.status({fill:"green",shape:"dot",text:"connected"}); node.status({fill:"green",shape:"dot",text:"connected"});
@ -144,9 +203,9 @@ module.exports = function(RED) {
close: function(cb) { this.serial.close(cb); }, close: function(cb) { this.serial.close(cb); },
write: function(m,cb) { this.serial.write(m,cb); }, write: function(m,cb) { this.serial.write(m,cb); },
} }
newline = newline.replace("\\n","\n").replace("\\r","\r"); //newline = newline.replace("\\n","\n").replace("\\r","\r");
var setupSerial = function() { var setupSerial = function() {
if (newline == "") { //if (newline == "") {
obj.serial = new serialp.SerialPort(port,{ obj.serial = new serialp.SerialPort(port,{
baudrate: baud, baudrate: baud,
databits: databits, databits: databits,
@ -154,16 +213,16 @@ module.exports = function(RED) {
stopbits: stopbits, stopbits: stopbits,
parser: serialp.parsers.raw parser: serialp.parsers.raw
},true, function(err, results) { if (err) obj.serial.emit('error',err); }); },true, function(err, results) { if (err) obj.serial.emit('error',err); });
} //}
else { //else {
obj.serial = new serialp.SerialPort(port,{ // obj.serial = new serialp.SerialPort(port,{
baudrate: baud, // baudrate: baud,
databits: databits, // databits: databits,
parity: parity, // parity: parity,
stopbits: stopbits, // stopbits: stopbits,
parser: serialp.parsers.readline(newline) // parser: serialp.parsers.readline(newline)
},true, function(err, results) { if (err) obj.serial.emit('error',err); }); // },true, function(err, results) { if (err) obj.serial.emit('error',err); });
} //}
obj.serial.on('error', function(err) { obj.serial.on('error', function(err) {
util.log("[serial] serial port "+port+" error "+err); util.log("[serial] serial port "+port+" error "+err);
obj._emitter.emit('closed'); obj._emitter.emit('closed');
@ -187,15 +246,16 @@ module.exports = function(RED) {
obj._emitter.emit('ready'); obj._emitter.emit('ready');
}); });
obj.serial.on('data',function(d) { obj.serial.on('data',function(d) {
if (typeof d !== "string") { //console.log(Buffer.isBuffer(d),d.length,d);
d = d.toString(); //if (typeof d !== "string") {
for (i=0; i<d.length; i++) { // //d = d.toString();
obj._emitter.emit('data',d.charAt(i)); for (var z=0; z<d.length; z++) {
} obj._emitter.emit('data',d[z]);
}
else {
obj._emitter.emit('data',d);
} }
//}
//else {
// obj._emitter.emit('data',d);
//}
}); });
} }
setupSerial(); setupSerial();
@ -226,7 +286,7 @@ module.exports = function(RED) {
RED.httpAdmin.get("/serialports",function(req,res) { RED.httpAdmin.get("/serialports",function(req,res) {
serialp.list(function (err, ports) { serialp.list(function (err, ports) {
console.log(JSON.stringify(ports)); //console.log(JSON.stringify(ports));
res.writeHead(200, {'Content-Type': 'text/plain'}); res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(ports)); res.write(JSON.stringify(ports));
res.end(); res.end();