node.js and serialport cannot list any ports

3.8k views Asked by At

I am trying to open a serial port on ubuntu using node.js.

I cannot seem to open any ports nor can I list any.

Here is my code for list:

var serialport = require("serialport"),

serialport.list(function (err, ports) {
   console.log("thisis the list callback");
   ports.forEach(function(port) {
       console.log(port.comName);
       console.log(port.pnpId);
       console.log(port.manufacturer);
   });
 });

I get no output and no errors. It just returns zero ports. I have two com ports recognized by the OS:

rd@mediaplayer:~/cotto$ dmesg | grep tty
[    0.000000] console [tty0] enabled
[    0.732717] serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[    0.804533] serial8250: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A
[    1.097341] 00:0a: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
[    1.168637] 00:0b: ttyS1 at I/O 0x2f8 (irq = 3) is a 16550A

If I try to explicitly open a com port I get a "not open" error when using it. I assume this is because node serialport does not "see" any of my com ports:

rd@mediaplayer:~/cotto$ sudo node sptest.js
opening serial port: /dev/ttyS0

events.js:72
       throw er; // Unhandled 'error' event
              ^
 Error: Serialport not open.
    at SerialPortFactory.SerialPort.write (/home/rd/node_modules/serialport/serialport.js:246:17)
    at Object.<anonymous> (/home/rd/cotto/sptest.js:33:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

The code to open the serial port is here for reference:

var serialport = require("serialport"),
SerialPort = serialport.SerialPort;

var portName = "/dev/ttyS0";

console.log("opening serial port: " + portName);

var myPort = new SerialPort(portName, { baudrate: 9600,
    });


myPort.write("Hello World\r\n");

Is there anything special I need to do to expose the linux com ports to node serialport?

2

There are 2 answers

0
Holger Will On

I have a similar problem. I can not list the ports on a raspberry pi. the same code runs just fine on windows. On the other hand i can open the port and read and write to it without any problem.

you could try:

var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/ttyS0", {
  baudrate: 57600
}, false); // this is the openImmediately flag [default is true]

serialPort.open(function (error) {
  if ( error ) {
    console.log('failed to open: '+error);
  } else {
    console.log('open');
    serialPort.on('data', function(data) {
      console.log('data received: ' + data);
    });
    serialPort.write("ls\n", function(err, results) {
      console.log('err ' + err);
      console.log('results ' + results);
    });
  }
});

to see if you get any errors when opening.

EDIT: for the time being i go with something like this to list the ports, which is a bit hacky but anyways...

var exec = require('child_process').exec;
exec('dmesg | grep tty', function (error, stdout, stderr) {
  stdout.split("\n").forEach(function(line){
    var words=line.split(" ");
    if(words.length>=6 && words[6].substring(0,3)=="tty"){
        console.log(words[6])
    }
  });
});

EDIT 2:

My device is /dev/ttyAMA0 and in /lib/udev/rules.d/60-persistent-serial.rules i see this line:

KERNEL!="ttyUSB[0-9]*|ttyACM[0-9]*", GOTO="persistent_serial_end"

which i suspect is the reason its not listed under /dev/serial/by-id. Any ideas what i can do about this?

0
reconbot On

With the newer versions of node serialport the write will queue until the port is open, however with version 2 and maybe 3 you need to wait for the port to be open before writing to it. The serialport docs for the older versions talk about waiting for the open event before writing.

For version 3 https://github.com/EmergingTechnologyAdvisors/node-serialport/blob/3.1.2/README.md#opening-a-port

I know this question is a bit old, but I wanted to leave an answer for anyone else hitting this problem. I highly recommend you use at least serialport version 3 as it fixes major bugs with v2 with small api changes, however 4 and 5 are faster, use less memory and support backpressure and have more bug fixes.