From 94d19b2a41b77d4f1a3141de2d20a02720469587 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Mon, 9 Jun 2014 08:12:34 +0100 Subject: [PATCH] Update rawserial (roughly) in line with main serial node. (Now has binary and timout modes) --- io/rawserial/26-rawserial.html | 42 ++++++++++++++++++++++----- io/rawserial/26-rawserial.js | 52 +++++++++++++++++++++++++++------- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/io/rawserial/26-rawserial.html b/io/rawserial/26-rawserial.html index c7b7c362..5bf3a7e3 100644 --- a/io/rawserial/26-rawserial.html +++ b/io/rawserial/26-rawserial.html @@ -16,17 +16,43 @@ diff --git a/io/rawserial/26-rawserial.js b/io/rawserial/26-rawserial.js index da53dfe8..11762005 100644 --- a/io/rawserial/26-rawserial.js +++ b/io/rawserial/26-rawserial.js @@ -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) {