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

Update rawserial (roughly) in line with main serial node.

(Now has binary and timout modes)
This commit is contained in:
Dave C-J 2014-06-09 08:12:34 +01:00
parent c1357545ca
commit 94d19b2a41
2 changed files with 77 additions and 17 deletions

View File

@ -16,17 +16,43 @@
<script type="text/x-red" data-template-name="rawserial in">
<div class="form-row">
<label for="node-input-port"><i class="icon-random"></i> Port</label>
<label for="node-input-port"><i class="fa fa-random"></i> Port</label>
<input type="text" id="node-input-port" placeholder="COM1">
</div>
<div class="form-row">
<label for="node-input-split"><i class="icon-edit"></i> Split on</label>
<input type="text" id="node-input-split" placeholder="\n">
<label for="node-input-out"><i class="fa fa-cut"></i> split input</label>
<select type="text" id="node-input-out" style="width:52%;">
<option value="char">when character received is</option>
<option value="time">after a fixed timeout of</option>
</select>
<input type="text" id="node-input-splitc" style="width:50px;">
<span id="node-units"></span>
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<label for="node-input-bin"><i class="fa fa-sign-in"></i> and deliver</label>
<select type="text" id="node-input-bin" style="width: 77%;">
<option value="false">ascii strings</option>
<option value="true">binary buffers</option>
</select>
</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>
var previous = null;
$("#node-input-out").on('focus', function () { previous = this.value; }).change(function() {
if (previous == null) { previous = $("#node-input-out").val(); }
if ($("#node-input-out").val() == "char") {
if (previous != "char") { $("#node-input-splitc").val("\\n"); }
$("#node-units").text("");
}
else {
if (previous != "time") { $("#node-input-splitc").val("0"); }
$("#node-units").text("ms");
}
});
</script>
</script>
<script type="text/x-red" data-help-name="rawserial in">
@ -44,7 +70,9 @@
color:"BurlyWood",
defaults: {
name: {value:""},
split: {value:""},
splitc: {value:"\n"},
out: {value:"char"},
bin: {value:"false"},
port: {value:"", required:true}
},
inputs:0,
@ -61,11 +89,11 @@
<script type="text/x-red" data-template-name="rawserial out">
<div class="form-row">
<label for="node-input-port"><i class="icon-random"></i> Port</label>
<label for="node-input-port"><i class="fa fa-random"></i> Port</label>
<input type="text" id="node-input-port" placeholder="COM1">
</div>
<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">
</div>
</script>

View File

@ -29,25 +29,57 @@ if (!plat.match(/^win/)) {
function RawSerialInNode(n) {
RED.nodes.createNode(this,n);
this.port = n.port;
this.split = n.split||null;
if (this.split == '\\n') this.split = "\n";
if (this.split == '\\r') this.split = "\r";
this.splitc = n.splitc||null;
this.out = n.out||"char";
this.bin = n.bin||false;
if (this.splitc == '\\n') this.splitc = "\n";
if (this.splitc == '\\r') this.splitc = "\r";
if (!isNaN(parseInt(this.splitc))) { this.splitc = parseInt(this.splitc); }
console.log("Split is",this.out,this.splitc);
var node = this;
var setupSerial = function() {
node.inp = fs.createReadStream(pre+node.port);
node.log("opened "+pre+node.port);
node.inp.setEncoding('utf8');
node.tout = null;
var line = "";
var buf = new Buffer(32768);
var i = 0;
node.inp.on('data', function (data) {
if (node.split != null) {
if (data == node.split) {
node.send({payload:line});
line = "";
for (var z = 0; z < data.length; z++) {
if ((node.out === "time") && (node.splitc != 0)) {
if (node.tout) {
i += 1;
buf[i] = data[z];
}
else {
node.tout = setTimeout(function () {
node.tout = null;
var m = new Buffer(i+1);
buf.copy(m,0,0,i+1);
if (node.bin !== "true") { m = m.toString(); }
node.send({"payload": m});
}, node.splitc);
i = 0;
buf[0] = data[z];
}
}
else if ((node.out == "char") && (node.splitc != null)) {
buf[i] = data[z];
i += 1;
if ((data[z] === node.splitc.charCodeAt(0)) || (i === 32768)) {
var m = new Buffer(i);
buf.copy(m,0,0,i);
if (node.bin !== "true") { m = m.toString(); }
node.send({"payload":m});
i = 0;
}
}
else {
if (node.bin !== "true") { node.send({"payload": String.fromCharCode(data[z])}); }
else { node.send({"payload": new Buffer([data[z]])});}
}
else { line += data; }
}
else { node.send({payload:data}); }
});
//node.inp.on('end', function (error) {console.log("End", error);});
node.inp.on('close', function (error) {