Let serialport wiatfor a char before starting output

This commit is contained in:
Dave Conway-Jones
2019-04-04 22:20:21 +01:00
parent 60af3ffa28
commit 0b11d68c02
4 changed files with 92 additions and 50 deletions

View File

@@ -13,13 +13,14 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
this.serialport = n.serialport;
this.newline = n.newline; /* overloaded: split character, timeout, or character count */
this.addchar = n.addchar || "false";
this.addchar = n.addchar || "";
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";
this.waitfor = n.waitfor || "";
this.responsetimeout = n.responsetimeout || 10000;
}
RED.nodes.registerType("serial-port",SerialPortNode);
@@ -117,6 +118,10 @@ module.exports = function(RED) {
// Serial Out
node.on("input",function(msg) {
if (!msg.hasOwnProperty("payload")) { return; } // do nothing unless we have a payload
if (msg.hasOwnProperty("count") && (typeof msg.count === "number") && (node.serialConfig.out === "count")) {
node.serialConfig.newline = msg.count;
}
if (msg.hasOwnProperty("flush") && msg.flush === true) { node.port.serial.flush(); }
node.status({fill:"yellow",shape:"dot",text:"serial.status.waiting"});
node.port.enqueue(msg,node,function(err,res) {
if (err) {
@@ -176,6 +181,7 @@ module.exports = function(RED) {
stopbits = serialConfig.stopbits,
newline = serialConfig.newline,
spliton = serialConfig.out,
waitfor = serialConfig.waitfor,
binoutput = serialConfig.bin,
addchar = serialConfig.addchar,
responsetimeout = serialConfig.responsetimeout;
@@ -191,17 +197,25 @@ module.exports = function(RED) {
// "time" : a msg will be sent after .newline milliseconds
// "count" : a msg will be sent after .newline characters
// if we use "count", we already know how big the buffer will be
var bufSize = spliton == "count" ? Number(newline): bufMaxSize;
var buf = new Buffer(bufSize);
var bufSize = (spliton === "count") ? Number(newline): bufMaxSize;
waitfor = waitfor.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0"); // jshint ignore:line
if (waitfor.substr(0,2) == "0x") { waitfor = parseInt(waitfor,16); }
if (waitfor.length === 1) { waitfor = waitfor.charCodeAt(0); }
console.log("WF2",waitfor);
var active = (waitfor === "") ? true : false;
var buf = new Buffer.alloc(bufSize);
var splitc; // split character
// Parse the split character onto a 1-char buffer we can immediately compare against
if (newline.substr(0,2) == "0x") {
splitc = new Buffer([parseInt(newline)]);
splitc = new Buffer.alloc([parseInt(newline,16)]);
}
else {
splitc = new Buffer(newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0")); // jshint ignore:line
splitc = new Buffer.from(newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0")); // jshint ignore:line
}
if (addchar === true) { addchar = splitc; }
console.log("AC:",addchar,":");
connections[id] = (function() {
var obj = {
@@ -220,10 +234,10 @@ module.exports = function(RED) {
else {
payload = payload.toString();
}
if ((spliton === "char") && (addchar === true)) { payload += splitc; }
if ((spliton === "char") && (addchar !== "")) { payload += addchar; }
}
else if ((spliton === "char") && (addchar === true) && (splitc !== "")) {
payload = Buffer.concat([payload,splitc]);
else if ((spliton === "char") && (addchar !== "")) {
payload = Buffer.concat([payload,addchar]);
}
return payload;
},
@@ -287,6 +301,7 @@ module.exports = function(RED) {
//newline = newline.replace("\\n","\n").replace("\\r","\r");
var olderr = "";
var setupSerial = function() {
obj.serial = new serialp(port,{
baudRate: baud,
dataBits: databits,
@@ -331,23 +346,26 @@ module.exports = function(RED) {
obj.serial.on('data',function(d) {
function emitData(data) {
var m = Buffer.from(data);
var last_sender = null;
if (obj.queue.length) { last_sender = obj.queue[0].sender; }
if (binoutput !== "bin") { m = m.toString(); }
var msgout = obj.dequeue() || {};
msgout.payload = m;
msgout.port = port;
obj._emitter.emit('data',
msgout,
last_sender);
if (active === true) {
var m = Buffer.from(data);
var last_sender = null;
if (obj.queue.length) { last_sender = obj.queue[0].sender; }
if (binoutput !== "bin") { m = m.toString(); }
var msgout = obj.dequeue() || {};
msgout.payload = m;
msgout.port = port;
obj._emitter.emit('data', msgout, last_sender);
}
active = (waitfor === "") ? true : false;
}
for (var z=0; z<d.length; z++) {
var c = d[z];
if (c === waitfor) { active = true; }
// handle the trivial case first -- single char buffer
if (!active) { return; }
if ((newline === 0)||(newline === "")) {
emitData(new Buffer([c]));
emitData(new Buffer.from([c]));
continue;
}
@@ -376,6 +394,7 @@ module.exports = function(RED) {
}
// count bytes into a buffer...
else if (spliton === "count") {
newline = serialConfig.newline;
if ( i >= parseInt(newline)) {
emitData(buf.slice(0,i));
i=0;