Node: Serial port communication does not work inside a Worker

Created on 25 Sep 2019  路  1Comment  路  Source: nodejs/node

  • Version: v12.10.0
  • Platform: Linux
  • Subsystem: Using module node-serialport

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:

hugo-a-garcia/serial-worker

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.");
}

addons question worker

Most helpful comment

>All comments

Was this page helpful?
0 / 5 - 0 ratings