We are trying to narrow down the issue of why a serial port cannot be opened in a worker. I has been suggested that maybe there is a bug in worker_thread (???) Maybe someone can verify if this is a bug OR give us some guidance to figure it out please?
This is the issue that can be referenced at node-serialport:
Serial port commnunication does not work inside a Worker Thread #1938
This is the repo with the example code:
and here is the main code:
const {
Worker, isMainThread, parentPort, workerData
} = require('worker_threads');
const SerialPort = require("serialport");
const Readline = SerialPort.parsers.Readline;
if (isMainThread) {
console.log("In main thread");
const worker = new Worker(__filename);
worker.on('message', msg => {
console.log(msg);
});
worker.on('error', err => {
console.error("Oops " + err)
});
worker.on('exit', (code) => {
if (code !== 0)
console.log(`Worker stopped with exit code ${code}`);
});
} else {
console.log("In worker thread");
const port = new SerialPort("/dev/ttyACM0", {
baudRate: 9600
});
const parser = new Readline
port.pipe(parser);
parser.on("data", (data) => console.log(data));
// port.on('readable', function () {
// console.log(port.read().toString());
// });
port.on('error', function (err) {
console.log('Error: ', err.message);
});
parentPort.postMessage("Done.");
}
Not a bug with Node.js, see https://github.com/serialport/node-serialport/issues/1938#issuecomment-534951904.
Most helpful comment
Not a bug with Node.js, see https://github.com/serialport/node-serialport/issues/1938#issuecomment-534951904.