Skip to content

Commit

Permalink
reuest node enablement
Browse files Browse the repository at this point in the history
  • Loading branch information
yhur committed Nov 14, 2023
1 parent b82e2cd commit a6cbc34
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions io/serialport/25-serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ module.exports = function(RED) {
setCallback(node, node.serialConfig);
});

this.on("close", function(done) {
serialPool.close(this.serialConfig.serialport,done);
});

function setCallback(node) {
node.status({fill:"grey",shape:"dot",text:"node-red:common.status.not-connected"});
node.port = serialPool.get(node.serialConfig);
Expand Down Expand Up @@ -174,28 +178,37 @@ module.exports = function(RED) {
});
});

// Serial In
this.port.on('data', function(msgout, sender) {
// serial request will only process incoming data pertaining to its own request (i.e. when it's at the head of the queue)
if (sender !== node) { return; }
node.status({fill:"green",shape:"dot",text:"node-red:common.status.ok"});
msgout.status = "OK";
node.send(msgout);
});
this.port.on('timeout', function(msgout, sender) {
if (sender !== node) { return; }
msgout.status = "ERR_TIMEOUT";
node.status({fill:"red",shape:"ring",text:"serial.status.timeout"});
node.send(msgout);
let serialConfig = this.serialConfig;
serialConfig.on('newport', function(newPort) {
node.port = serialPool.get(serialConfig);
setCallback(node);
});

// Common part
node.port.on('ready', function() {
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
});
node.port.on('closed', function() {
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
});
// Serial In
function setCallback(node) {
node.port.on('data', function (msgout, sender) {
// serial request will only process incoming data pertaining to its own request (i.e. when it's at the head of the queue)
if (sender !== node) { return; }
node.status({ fill: "green", shape: "dot", text: "node-red:common.status.ok" });
msgout.status = "OK";
node.send(msgout);
});
node.port.on('timeout', function (msgout, sender) {
if (sender !== node) { return; }
msgout.status = "ERR_TIMEOUT";
node.status({ fill: "red", shape: "ring", text: "serial.status.timeout" });
node.send(msgout);
});

// Common part
node.port.on('ready', function () {
node.status({ fill: "green", shape: "dot", text: "node-red:common.status.connected" });
});
node.port.on('closed', function () {
node.status({ fill: "red", shape: "ring", text: "node-red:common.status.not-connected" });
});
};
setCallback(node);
}
else {
this.error(RED._("serial.errors.missing-conf"), {});
Expand Down Expand Up @@ -372,6 +385,7 @@ module.exports = function(RED) {
},
}
//newline = newline.replace("\\n","\n").replace("\\r","\r");
obj._emitter.setMaxListeners(50);
var olderr = "";
var setupSerial = function() {
obj.serial = new SerialPort({
Expand Down

0 comments on commit a6cbc34

Please sign in to comment.