This seems to be a re-occurring problem with these nodes and reading through several of the topics (including my previous one).
This time I'm trying to connect to a serial device. It was working on a Tulip Edge IO device using their ancient Node Red 1.3.5. At some point node-red-contrib-modus
was updated to no longer be compatible with 1.X so I put Fedora Linux on a tablet and installed Node Red 3.0.2 (and the latest node-red-contrib-modbus
).
Using a catch node I can get the following very unhelpful error:
Error: Port Not Open
at /home/nodered/.node-red/node_modules/node-red-contrib-modbus/modbus/maps/core/core/modbus-client-core.js:79:156
Looking at line 79 of the file on github seems to be for closing the port. So am I to assume it's trying to close a port that's not open?
Furthermore, the documentation for node-red-contrib-modbus
is very confusing. At the top it says it's BASED on modbus-serial
, but in the details says to test using modbus-serial
if you have issues.
Which I did with what I think is success. I don't have the controller I'm trying to talk to while I'm troubleshooting in the lab but I can see the TX light blink on the USB->RS485 adapter when I run the following script and do not see a blink when I click the inject node in NR.
$ cat modbus-test.js
const ModbusRTU = require('modbus-serial');
const client = new ModbusRTU();
client.connectRTUBuffered('/dev/ttyUSB0', { baudRate: 19200 }, (err) => {
if (err) {
console.error(err);
return;
}
client.setID(1); // Modbus slave address
client.setTimeout(500);
const start = 2000; // starting register address
const quantity = 10; // number of registers to read
client.readHoldingRegisters(start, quantity)
.then((data) => {
console.log(data); // do something with the received data
})
.catch((err) => {
console.error(err); // handle error
})
.finally(() => {
client.close(); // close connection after the operation is complete
});
});
For posterity here's the flow: