diff --git a/Dockerfile b/Dockerfile index a4f7c1d..59c28a3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ LABEL org.label-schema.build-date=$BUILD_DATE \ RUN [ "cross-build-start" ] #version -ENV HILSCHERNETPI_NODERED_NPIX_RS485_VERSION 1.0.1 +ENV HILSCHERNETPI_NODERED_NPIX_RS485_VERSION 1.0.2 #labeling LABEL maintainer="netpi@hilscher.com" \ diff --git a/node-red-contrib-npix-rs485/25-serial-rs485.js b/node-red-contrib-npix-rs485/25-serial-rs485.js index 951c0bc..132e3a1 100644 --- a/node-red-contrib-npix-rs485/25-serial-rs485.js +++ b/node-red-contrib-npix-rs485/25-serial-rs485.js @@ -160,20 +160,42 @@ module.exports = function(RED) { if (this.serialConfig) { var node = this; node.port = serialPool.get(this.serialConfig); - // 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; + // take control of GPIO 17 + gpio.setup(11, gpio.DIR_LOW, function (err) { + if (err) { + RED.log.warn(" Setting GPIO 17 to output failed"); + } else { + // 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"}); + // !!!!Modification !!!!! get Mutex, set RS485 TX enable to high first, then send + mutex.lock(function () { + gpio.write(11, true, function(err) { + node.port.enqueue(msg,node,function(err,res) { + if (err) { + var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path); + node.error(errmsg,msg); + mutex.unlock(); + } else { + // !!!!!Modification!!!! Wait till all data has been transmitted + node.port.drain(function(err) { + // set RS485 TX enable to low + gpio.write(11, false, function(err){ + // unlock mutex + mutex.unlock(); + }); + }); + } + }); + }); + }); + }); } - 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) { - var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path); - node.error(errmsg,msg); - } - }); }); // Serial In @@ -490,3 +512,4 @@ module.exports = function(RED) { }); } +