mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
add serialport to node-red-nodes, fix lint etc etc
This commit is contained in:
parent
cfb8ab7e92
commit
73319a20b9
266
io/serialport/25-serial.html
Normal file
266
io/serialport/25-serial.html
Normal file
@ -0,0 +1,266 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2013,2015 IBM Corp.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="serial in">
|
||||||
|
<div class="form-row node-input-serial">
|
||||||
|
<label for="node-input-serial"><i class="fa fa-random"></i> Serial Port</label>
|
||||||
|
<input type="text" id="node-input-serial">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/x-red" data-help-name="serial in">
|
||||||
|
<p>Reads data from a local serial port.</p>
|
||||||
|
<p>Can either <ul><li>wait for a "split" character (default \n). Also accepts hex notation (0x0a).</li>
|
||||||
|
<li>wait for a timeout in milliseconds for the first character received</li>
|
||||||
|
<li>wait to fill a fixed sized buffer</li></ul></p>
|
||||||
|
<p>It then outputs <b>msg.payload</b> as either a UTF8 ascii string or a binary Buffer object.</p>
|
||||||
|
<p>If no split character is specified, or a timeout or buffer size of 0, then a stream of single characters is sent - again either as ascii chars or size 1 binary buffers.</p>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('serial in',{
|
||||||
|
category: 'input',
|
||||||
|
defaults: {
|
||||||
|
name: {name:""},
|
||||||
|
serial: {type:"serial-port",required:true}
|
||||||
|
},
|
||||||
|
color:"BurlyWood",
|
||||||
|
inputs:0,
|
||||||
|
outputs:1,
|
||||||
|
icon: "serial.png",
|
||||||
|
label: function() {
|
||||||
|
var serialNode = RED.nodes.node(this.serial);
|
||||||
|
return this.name||(serialNode?serialNode.label().split(":")[0]:"serial");
|
||||||
|
},
|
||||||
|
labelStyle: function() {
|
||||||
|
return this.name?"node_label_italic":"";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="serial out">
|
||||||
|
<div class="form-row node-input-serial">
|
||||||
|
<label for="node-input-serial"><i class="fa fa-random"></i> Serial Port</label>
|
||||||
|
<input type="text" id="node-input-serial">
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
||||||
|
<input type="text" id="node-input-name" placeholder="Name">
|
||||||
|
</div>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/x-red" data-help-name="serial out">
|
||||||
|
<p>Provides a connection to an outbound serial port.</p>
|
||||||
|
<p>Only the <b>msg.payload</b> is sent.</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>
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('serial out',{
|
||||||
|
category: 'output',
|
||||||
|
defaults: {
|
||||||
|
name: {name:""},
|
||||||
|
serial: {type:"serial-port",required:true}
|
||||||
|
},
|
||||||
|
color:"BurlyWood",
|
||||||
|
inputs:1,
|
||||||
|
outputs:0,
|
||||||
|
icon: "serial.png",
|
||||||
|
align: "right",
|
||||||
|
label: function() {
|
||||||
|
var serialNode = RED.nodes.node(this.serial);
|
||||||
|
return this.name||(serialNode?serialNode.label().split(":")[0]:"serial");
|
||||||
|
},
|
||||||
|
labelStyle: function() {
|
||||||
|
return this.name?"node_label_italic":"";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
<script type="text/x-red" data-template-name="serial-port">
|
||||||
|
<div class="form-row">
|
||||||
|
<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"/>
|
||||||
|
<a id="node-config-lookup-serial" class="btn"><i id="node-config-lookup-serial-icon" class="fa fa-search"></i></a>
|
||||||
|
</div>
|
||||||
|
<div class="form-row">
|
||||||
|
<table><tr>
|
||||||
|
<td width="102px"><i class="fa fa-wrench"></i> Settings</td>
|
||||||
|
<td width="100px">Baud Rate</td>
|
||||||
|
<td width="80px">Data Bits</td>
|
||||||
|
<td width="80px">Parity</td>
|
||||||
|
<td width="80px">Stop Bits</td>
|
||||||
|
</tr><tr><td> </td>
|
||||||
|
<td>
|
||||||
|
<select type="text" id="node-config-input-serialbaud" style="width: 100px;">
|
||||||
|
<option value="115200">115200</option>
|
||||||
|
<option value="57600">57600</option>
|
||||||
|
<option value="38400">38400</option>
|
||||||
|
<option value="19200">19200</option>
|
||||||
|
<option value="9600">9600</option>
|
||||||
|
<option value="4800">4800</option>
|
||||||
|
<option value="2400">2400</option>
|
||||||
|
<option value="1800">1800</option>
|
||||||
|
<option value="1200">1200</option>
|
||||||
|
<option value="600">600</option>
|
||||||
|
<option value="300">300</option>
|
||||||
|
<option value="200">200</option>
|
||||||
|
<option value="150">150</option>
|
||||||
|
<option value="134">134</option>
|
||||||
|
<option value="110">110</option>
|
||||||
|
<option value="75">75</option>
|
||||||
|
<option value="50">50</option>
|
||||||
|
</select>
|
||||||
|
</td><td>
|
||||||
|
<select type="text" id="node-config-input-databits" style="width: 80px;">
|
||||||
|
<option value="8">8</option>
|
||||||
|
<option value="7">7</option>
|
||||||
|
<option value="6">6</option>
|
||||||
|
<option value="5">5</option>
|
||||||
|
</select>
|
||||||
|
</td><td>
|
||||||
|
<select type="text" id="node-config-input-parity" style="width: 80px;">
|
||||||
|
<option value="none">None</option>
|
||||||
|
<option value="even">Even</option>
|
||||||
|
<option value="mark">Mark</option>
|
||||||
|
<option value="odd">Odd</option>
|
||||||
|
<option value="space">Space</option>
|
||||||
|
</select>
|
||||||
|
</td><td>
|
||||||
|
<select type="text" id="node-config-input-stopbits" style="width: 80px;">
|
||||||
|
<option value="2">2</option>
|
||||||
|
<option value="1">1</option>
|
||||||
|
</select>
|
||||||
|
</td>
|
||||||
|
</tr></table><br/>
|
||||||
|
|
||||||
|
<div class="form-row">
|
||||||
|
<label><i class="fa fa-sign-in"></i> Input</label>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" style="padding-left: 10px;">
|
||||||
|
Split input
|
||||||
|
<select type="text" id="node-config-input-out" style="margin-left: 5px; width:200px;">
|
||||||
|
<option value="char">on the character</option>
|
||||||
|
<option value="time">after a timeout of</option>
|
||||||
|
<option value="count">into fixed lengths of</option>
|
||||||
|
</select>
|
||||||
|
<input type="text" id="node-config-input-newline" style="width:50px;">
|
||||||
|
<span id="node-units"></span>
|
||||||
|
</div>
|
||||||
|
<div class="form-row" style="padding-left: 10px;">
|
||||||
|
and deliver
|
||||||
|
<select type="text" id="node-config-input-bin" style="margin-left: 5px; width: 150px;">
|
||||||
|
<option value="false">ascii strings</option>
|
||||||
|
<option value="bin">binary buffers</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<br/>
|
||||||
|
<div id="node-config-addchar">
|
||||||
|
<div class="form-row">
|
||||||
|
<label><i class="fa fa-sign-out"></i> Output</label>
|
||||||
|
</div>
|
||||||
|
<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">add split character to output messages</label>
|
||||||
|
</div>
|
||||||
|
</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>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
RED.nodes.registerType('serial-port',{
|
||||||
|
category: 'config',
|
||||||
|
defaults: {
|
||||||
|
//name: {value:""},
|
||||||
|
serialport: {value:"",required:true},
|
||||||
|
serialbaud: {value:57600,required:true},
|
||||||
|
databits: {value:8,required:true},
|
||||||
|
parity: {value:"none",required:true},
|
||||||
|
stopbits: {value:1,required:true},
|
||||||
|
newline: {value:"\\n"},
|
||||||
|
bin: {value:"false"},
|
||||||
|
out: {value:"char"},
|
||||||
|
addchar: {value:false}
|
||||||
|
},
|
||||||
|
label: function() {
|
||||||
|
this.serialbaud = this.serialbaud || 57600;
|
||||||
|
this.databits = this.databits || 8;
|
||||||
|
this.parity = this.parity || 'none';
|
||||||
|
this.stopbits = this.stopbits || 1;
|
||||||
|
return this.serialport+":"+this.serialbaud+"-"+this.databits+this.parity.charAt(0).toUpperCase()+this.stopbits;
|
||||||
|
},
|
||||||
|
oneditprepare: function() {
|
||||||
|
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(""); }
|
||||||
|
$("#node-units").text("chars");
|
||||||
|
$("#node-config-addchar").hide();
|
||||||
|
$("#node-config-input-addchar").val("false");
|
||||||
|
$("#tip-split").hide();
|
||||||
|
$("#tip-bin").hide();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
$("#node-config-input-serialport").autocomplete( "destroy" );
|
||||||
|
} catch(err) {
|
||||||
|
}
|
||||||
|
$("#node-config-lookup-serial").click(function() {
|
||||||
|
//$("#node-config-lookup-serial-icon").removeClass('fa fa-search');
|
||||||
|
//$("#node-config-lookup-serial-icon").addClass('fa fa-spinner');
|
||||||
|
$("#node-config-lookup-serial").addClass('disabled');
|
||||||
|
$.getJSON('serialports',function(data) {
|
||||||
|
//$("#node-config-lookup-serial-icon").addClass('fa fa-search');
|
||||||
|
//$("#node-config-lookup-serial-icon").removeClass('fa fa-spinner');
|
||||||
|
$("#node-config-lookup-serial").removeClass('disabled');
|
||||||
|
var ports = [];
|
||||||
|
$.each(data, function(i, port){
|
||||||
|
ports.push(port.comName);
|
||||||
|
});
|
||||||
|
$("#node-config-input-serialport").autocomplete({
|
||||||
|
source:ports,
|
||||||
|
minLength:0,
|
||||||
|
close: function( event, ui ) {
|
||||||
|
$("#node-config-input-serialport").autocomplete( "destroy" );
|
||||||
|
}
|
||||||
|
}).autocomplete("search","");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
291
io/serialport/25-serial.js
Normal file
291
io/serialport/25-serial.js
Normal file
@ -0,0 +1,291 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013,2015 IBM Corp.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
**/
|
||||||
|
|
||||||
|
module.exports = function(RED) {
|
||||||
|
"use strict";
|
||||||
|
var settings = RED.settings;
|
||||||
|
var events = require("events");
|
||||||
|
var serialp = require("serialport");
|
||||||
|
var bufMaxSize = 32768; // Max serial buffer size, for inputs...
|
||||||
|
|
||||||
|
// TODO: 'serialPool' should be encapsulated in SerialPortNode
|
||||||
|
|
||||||
|
function SerialPortNode(n) {
|
||||||
|
RED.nodes.createNode(this,n);
|
||||||
|
this.serialport = n.serialport;
|
||||||
|
this.newline = n.newline;
|
||||||
|
this.addchar = n.addchar || "false";
|
||||||
|
this.serialbaud = parseInt(n.serialbaud) || 57600;
|
||||||
|
this.databits = parseInt(n.databits) || 8;
|
||||||
|
this.parity = n.parity || "none";
|
||||||
|
this.stopbits = parseInt(n.stopbits) || 1;
|
||||||
|
this.bin = n.bin || "false";
|
||||||
|
this.out = n.out || "char";
|
||||||
|
}
|
||||||
|
RED.nodes.registerType("serial-port",SerialPortNode);
|
||||||
|
|
||||||
|
function SerialOutNode(n) {
|
||||||
|
RED.nodes.createNode(this,n);
|
||||||
|
this.serial = n.serial;
|
||||||
|
this.serialConfig = RED.nodes.getNode(this.serial);
|
||||||
|
|
||||||
|
if (this.serialConfig) {
|
||||||
|
var node = this;
|
||||||
|
node.port = serialPool.get(this.serialConfig.serialport,
|
||||||
|
this.serialConfig.serialbaud,
|
||||||
|
this.serialConfig.databits,
|
||||||
|
this.serialConfig.parity,
|
||||||
|
this.serialConfig.stopbits,
|
||||||
|
this.serialConfig.newline);
|
||||||
|
node.addCh = "";
|
||||||
|
if (node.serialConfig.addchar == "true" || node.serialConfig.addchar === true) {
|
||||||
|
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.on("input",function(msg) {
|
||||||
|
if (msg.hasOwnProperty("payload")) {
|
||||||
|
var payload = msg.payload;
|
||||||
|
if (!Buffer.isBuffer(payload)) {
|
||||||
|
if (typeof payload === "object") {
|
||||||
|
payload = JSON.stringify(payload);
|
||||||
|
} else {
|
||||||
|
payload = payload.toString();
|
||||||
|
}
|
||||||
|
payload += node.addCh;
|
||||||
|
} else if (node.addCh !== "") {
|
||||||
|
payload = Buffer.concat([payload,new Buffer(node.addCh)]);
|
||||||
|
}
|
||||||
|
node.port.write(payload,function(err,res) {
|
||||||
|
if (err) {
|
||||||
|
var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path);
|
||||||
|
node.error(errmsg,msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
node.port.on('ready', function() {
|
||||||
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
||||||
|
});
|
||||||
|
node.port.on('closed', function() {
|
||||||
|
node.status({fill:"red",shape:"ring",text:"not connected"});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.error("missing serial config");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.on("close", function(done) {
|
||||||
|
if (this.serialConfig) {
|
||||||
|
serialPool.close(this.serialConfig.serialport,done);
|
||||||
|
} else {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
RED.nodes.registerType("serial out",SerialOutNode);
|
||||||
|
|
||||||
|
|
||||||
|
function SerialInNode(n) {
|
||||||
|
RED.nodes.createNode(this,n);
|
||||||
|
this.serial = n.serial;
|
||||||
|
this.serialConfig = RED.nodes.getNode(this.serial);
|
||||||
|
|
||||||
|
if (this.serialConfig) {
|
||||||
|
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.port = serialPool.get(this.serialConfig.serialport,
|
||||||
|
this.serialConfig.serialbaud,
|
||||||
|
this.serialConfig.databits,
|
||||||
|
this.serialConfig.parity,
|
||||||
|
this.serialConfig.stopbits,
|
||||||
|
this.serialConfig.newline
|
||||||
|
);
|
||||||
|
|
||||||
|
var splitc;
|
||||||
|
if (node.serialConfig.newline.substr(0,2) == "0x") {
|
||||||
|
splitc = new Buffer([parseInt(node.serialConfig.newline)]);
|
||||||
|
} else {
|
||||||
|
splitc = 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
|
||||||
|
}
|
||||||
|
|
||||||
|
this.port.on('data', function(msg) {
|
||||||
|
// single char buffer
|
||||||
|
if ((node.serialConfig.newline === 0)||(node.serialConfig.newline === "")) {
|
||||||
|
if (node.serialConfig.bin !== "bin") { node.send({"payload": String.fromCharCode(msg)}); }
|
||||||
|
else { 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});
|
||||||
|
m = null;
|
||||||
|
}, 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 >= parseInt(node.serialConfig.newline)) {
|
||||||
|
var m = new Buffer(i);
|
||||||
|
buf.copy(m,0,0,i);
|
||||||
|
if (node.serialConfig.bin !== "bin") { m = m.toString(); }
|
||||||
|
node.send({"payload":m});
|
||||||
|
m = null;
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// look to match char...
|
||||||
|
else if (node.serialConfig.out === "char") {
|
||||||
|
buf[i] = msg;
|
||||||
|
i += 1;
|
||||||
|
if ((msg === splitc[0]) || (i === bufMaxSize)) {
|
||||||
|
var n = new Buffer(i);
|
||||||
|
buf.copy(n,0,0,i);
|
||||||
|
if (node.serialConfig.bin !== "bin") { n = n.toString(); }
|
||||||
|
node.send({"payload":n});
|
||||||
|
n = null;
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { node.log("should never get here"); }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.port.on('ready', function() {
|
||||||
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
||||||
|
});
|
||||||
|
this.port.on('closed', function() {
|
||||||
|
node.status({fill:"red",shape:"ring",text:"not connected"});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
this.error("missing serial config");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.on("close", function(done) {
|
||||||
|
if (this.serialConfig) {
|
||||||
|
serialPool.close(this.serialConfig.serialport,done);
|
||||||
|
} else {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
RED.nodes.registerType("serial in",SerialInNode);
|
||||||
|
|
||||||
|
|
||||||
|
var serialPool = (function() {
|
||||||
|
var connections = {};
|
||||||
|
return {
|
||||||
|
get:function(port,baud,databits,parity,stopbits,newline,callback) {
|
||||||
|
var id = port;
|
||||||
|
if (!connections[id]) {
|
||||||
|
connections[id] = (function() {
|
||||||
|
var obj = {
|
||||||
|
_emitter: new events.EventEmitter(),
|
||||||
|
serial: null,
|
||||||
|
_closing: false,
|
||||||
|
tout: null,
|
||||||
|
on: function(a,b) { this._emitter.on(a,b); },
|
||||||
|
close: function(cb) { this.serial.close(cb); },
|
||||||
|
write: function(m,cb) { this.serial.write(m,cb); },
|
||||||
|
}
|
||||||
|
//newline = newline.replace("\\n","\n").replace("\\r","\r");
|
||||||
|
var setupSerial = function() {
|
||||||
|
obj.serial = new serialp.SerialPort(port,{
|
||||||
|
baudrate: baud,
|
||||||
|
databits: databits,
|
||||||
|
parity: parity,
|
||||||
|
stopbits: stopbits,
|
||||||
|
parser: serialp.parsers.raw
|
||||||
|
},true, function(err, results) { if (err) { obj.serial.emit('error',err); } });
|
||||||
|
obj.serial.on('error', function(err) {
|
||||||
|
RED.log.error("serial port "+port+" error "+err);
|
||||||
|
obj._emitter.emit('closed');
|
||||||
|
obj.tout = setTimeout(function() {
|
||||||
|
setupSerial();
|
||||||
|
}, settings.serialReconnectTime);
|
||||||
|
});
|
||||||
|
obj.serial.on('close', function() {
|
||||||
|
if (!obj._closing) {
|
||||||
|
RED.log.error("serial port "+port+" closed unexpectedly");
|
||||||
|
obj._emitter.emit('closed');
|
||||||
|
obj.tout = setTimeout(function() {
|
||||||
|
setupSerial();
|
||||||
|
}, settings.serialReconnectTime);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
obj.serial.on('open',function() {
|
||||||
|
RED.log.info("serial port "+port+" opened at "+baud+" baud "+databits+""+parity.charAt(0).toUpperCase()+stopbits);
|
||||||
|
if (obj.tout) { clearTimeout(obj.tout); }
|
||||||
|
//obj.serial.flush();
|
||||||
|
obj._emitter.emit('ready');
|
||||||
|
});
|
||||||
|
obj.serial.on('data',function(d) {
|
||||||
|
for (var z=0; z<d.length; z++) {
|
||||||
|
obj._emitter.emit('data',d[z]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
obj.serial.on("disconnect",function() {
|
||||||
|
RED.log.error("serial port "+port+" gone away");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
setupSerial();
|
||||||
|
return obj;
|
||||||
|
}());
|
||||||
|
}
|
||||||
|
return connections[id];
|
||||||
|
},
|
||||||
|
close: function(port,done) {
|
||||||
|
if (connections[port]) {
|
||||||
|
if (connections[port].tout != null) {
|
||||||
|
clearTimeout(connections[port].tout);
|
||||||
|
}
|
||||||
|
connections[port]._closing = true;
|
||||||
|
try {
|
||||||
|
connections[port].close(function() {
|
||||||
|
RED.log.info("serial port closed");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch(err) { }
|
||||||
|
delete connections[port];
|
||||||
|
} else {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
|
||||||
|
RED.httpAdmin.get("/serialports", RED.auth.needsPermission('serial.read'), function(req,res) {
|
||||||
|
serialp.list(function (err, ports) {
|
||||||
|
res.json(ports);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
177
io/serialport/LICENSE
Normal file
177
io/serialport/LICENSE
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
53
io/serialport/README.md
Normal file
53
io/serialport/README.md
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
node-red-node-serialport
|
||||||
|
========================
|
||||||
|
|
||||||
|
<a href="http://nodered.org" target="_new">Node-RED</a> nodes to talk to an hardware Serial port.
|
||||||
|
|
||||||
|
**Note** : This is the same node as is/was in the core of Node-RED. If you already
|
||||||
|
have it installed you do NOT need this node.
|
||||||
|
|
||||||
|
Install
|
||||||
|
-------
|
||||||
|
|
||||||
|
Run the following command in the root directory of your Node-RED install, usually
|
||||||
|
this is ~/.node-red .
|
||||||
|
|
||||||
|
npm install node-red-node-serialport
|
||||||
|
|
||||||
|
During install there may be multiple messages about optional compilation.
|
||||||
|
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
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
Provides two nodes - one to receive messages, and one to send.
|
||||||
|
|
||||||
|
###Input
|
||||||
|
|
||||||
|
Reads data from a local serial port.
|
||||||
|
|
||||||
|
Clicking on the search icon will attempt to autodetect serial ports attached to
|
||||||
|
the device, however you many need to manually specify it. COM1, /dev/ttyUSB0, etc
|
||||||
|
|
||||||
|
It can either
|
||||||
|
|
||||||
|
- wait for a "split" character (default \n). Also accepts hex notation (0x0a).
|
||||||
|
- wait for a timeout in milliseconds for the first character received
|
||||||
|
- wait to fill a fixed sized buffer
|
||||||
|
|
||||||
|
It then outputs **msg.payload** as either a UTF8 ascii string or a binary Buffer object.
|
||||||
|
|
||||||
|
If no split character is specified, or a timeout or buffer size of 0, then a stream
|
||||||
|
of single characters is sent - again either as ascii chars or size 1 binary buffers.
|
||||||
|
|
||||||
|
###Output
|
||||||
|
|
||||||
|
Provides a connection to an outbound serial port.
|
||||||
|
|
||||||
|
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.
|
24
io/serialport/package.json
Normal file
24
io/serialport/package.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name" : "node-red-node-serialport",
|
||||||
|
"version" : "0.0.1",
|
||||||
|
"description" : "Node-RED nodes to talk to an serial port",
|
||||||
|
"dependencies" : {
|
||||||
|
"serialport" : "1.7.*"
|
||||||
|
},
|
||||||
|
"repository" : {
|
||||||
|
"type":"git",
|
||||||
|
"url":"https://github.com/node-red/node-red-nodes/tree/master/io/serialport"
|
||||||
|
},
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [ "node-red", "serial" ],
|
||||||
|
"node-red" : {
|
||||||
|
"nodes" : {
|
||||||
|
"serialport": "25-serial.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"author": {
|
||||||
|
"name": "Dave Conway-Jones",
|
||||||
|
"email": "ceejay@vnet.ibm.com",
|
||||||
|
"url": "http://nodered.org"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user