Skip to content

Commit 94d19b2

Browse files
committed
Update rawserial (roughly) in line with main serial node.
(Now has binary and timout modes)
1 parent c135754 commit 94d19b2

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

io/rawserial/26-rawserial.html

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,43 @@
1616

1717
<script type="text/x-red" data-template-name="rawserial in">
1818
<div class="form-row">
19-
<label for="node-input-port"><i class="icon-random"></i> Port</label>
19+
<label for="node-input-port"><i class="fa fa-random"></i> Port</label>
2020
<input type="text" id="node-input-port" placeholder="COM1">
2121
</div>
2222
<div class="form-row">
23-
<label for="node-input-split"><i class="icon-edit"></i> Split on</label>
24-
<input type="text" id="node-input-split" placeholder="\n">
23+
<label for="node-input-out"><i class="fa fa-cut"></i> split input</label>
24+
<select type="text" id="node-input-out" style="width:52%;">
25+
<option value="char">when character received is</option>
26+
<option value="time">after a fixed timeout of</option>
27+
</select>
28+
<input type="text" id="node-input-splitc" style="width:50px;">
29+
<span id="node-units"></span>
2530
</div>
2631
<div class="form-row">
27-
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
32+
<label for="node-input-bin"><i class="fa fa-sign-in"></i> and deliver</label>
33+
<select type="text" id="node-input-bin" style="width: 77%;">
34+
<option value="false">ascii strings</option>
35+
<option value="true">binary buffers</option>
36+
</select>
37+
</div>
38+
<div class="form-row">
39+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
2840
<input type="text" id="node-input-name" placeholder="Name">
2941
</div>
42+
<script>
43+
var previous = null;
44+
$("#node-input-out").on('focus', function () { previous = this.value; }).change(function() {
45+
if (previous == null) { previous = $("#node-input-out").val(); }
46+
if ($("#node-input-out").val() == "char") {
47+
if (previous != "char") { $("#node-input-splitc").val("\\n"); }
48+
$("#node-units").text("");
49+
}
50+
else {
51+
if (previous != "time") { $("#node-input-splitc").val("0"); }
52+
$("#node-units").text("ms");
53+
}
54+
});
55+
</script>
3056
</script>
3157

3258
<script type="text/x-red" data-help-name="rawserial in">
@@ -44,7 +70,9 @@
4470
color:"BurlyWood",
4571
defaults: {
4672
name: {value:""},
47-
split: {value:""},
73+
splitc: {value:"\n"},
74+
out: {value:"char"},
75+
bin: {value:"false"},
4876
port: {value:"", required:true}
4977
},
5078
inputs:0,
@@ -61,11 +89,11 @@
6189

6290
<script type="text/x-red" data-template-name="rawserial out">
6391
<div class="form-row">
64-
<label for="node-input-port"><i class="icon-random"></i> Port</label>
92+
<label for="node-input-port"><i class="fa fa-random"></i> Port</label>
6593
<input type="text" id="node-input-port" placeholder="COM1">
6694
</div>
6795
<div class="form-row">
68-
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
96+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
6997
<input type="text" id="node-input-name" placeholder="Name">
7098
</div>
7199
</script>

io/rawserial/26-rawserial.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,57 @@ if (!plat.match(/^win/)) {
2929
function RawSerialInNode(n) {
3030
RED.nodes.createNode(this,n);
3131
this.port = n.port;
32-
this.split = n.split||null;
33-
if (this.split == '\\n') this.split = "\n";
34-
if (this.split == '\\r') this.split = "\r";
32+
this.splitc = n.splitc||null;
33+
this.out = n.out||"char";
34+
this.bin = n.bin||false;
35+
if (this.splitc == '\\n') this.splitc = "\n";
36+
if (this.splitc == '\\r') this.splitc = "\r";
37+
if (!isNaN(parseInt(this.splitc))) { this.splitc = parseInt(this.splitc); }
38+
console.log("Split is",this.out,this.splitc);
3539
var node = this;
3640

3741
var setupSerial = function() {
3842
node.inp = fs.createReadStream(pre+node.port);
3943
node.log("opened "+pre+node.port);
40-
node.inp.setEncoding('utf8');
44+
node.tout = null;
4145
var line = "";
46+
var buf = new Buffer(32768);
47+
var i = 0;
4248
node.inp.on('data', function (data) {
43-
if (node.split != null) {
44-
if (data == node.split) {
45-
node.send({payload:line});
46-
line = "";
49+
for (var z = 0; z < data.length; z++) {
50+
if ((node.out === "time") && (node.splitc != 0)) {
51+
if (node.tout) {
52+
i += 1;
53+
buf[i] = data[z];
54+
}
55+
else {
56+
node.tout = setTimeout(function () {
57+
node.tout = null;
58+
var m = new Buffer(i+1);
59+
buf.copy(m,0,0,i+1);
60+
if (node.bin !== "true") { m = m.toString(); }
61+
node.send({"payload": m});
62+
}, node.splitc);
63+
i = 0;
64+
buf[0] = data[z];
65+
}
66+
}
67+
else if ((node.out == "char") && (node.splitc != null)) {
68+
buf[i] = data[z];
69+
i += 1;
70+
if ((data[z] === node.splitc.charCodeAt(0)) || (i === 32768)) {
71+
var m = new Buffer(i);
72+
buf.copy(m,0,0,i);
73+
if (node.bin !== "true") { m = m.toString(); }
74+
node.send({"payload":m});
75+
i = 0;
76+
}
77+
}
78+
else {
79+
if (node.bin !== "true") { node.send({"payload": String.fromCharCode(data[z])}); }
80+
else { node.send({"payload": new Buffer([data[z]])});}
4781
}
48-
else { line += data; }
4982
}
50-
else { node.send({payload:data}); }
5183
});
5284
//node.inp.on('end', function (error) {console.log("End", error);});
5385
node.inp.on('close', function (error) {

0 commit comments

Comments
 (0)