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?
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:
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...
EDIT 2:
My device is
/dev/ttyAMA0
and in/lib/udev/rules.d/60-persistent-serial.rules
i see this line:which i suspect is the reason its not listed under
/dev/serial/by-id
. Any ideas what i can do about this?