node.js and serialport cannot list any ports - linux

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:
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?

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.

Related

Unable to read serial port with node-serialport on raspberry pi

I'm using a raspberry pi (the first model) running on Jessy (8), node v0.12.6 and serialport 2.0.6. I have connected the pin Rx on the pin Tx of the physical serial port.
It's working fine with cat /dev/ttyAMA0 and echo "Hello" > /dev/ttyAMA0
The writing on the serial port with node-serialport is fine. I am using the code bellow (and using cat to read this) (source: https://www.npmjs.com/package/serialport)
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyAMA0", {
baudrate:9600,
databits: 8,
parity: 'none',
stopBits: 1,
flowControl: false,
parser: serialport.parsers.readline("\n"),
});
sp.on('open', function() {
console.log("sending");
sp.write("Hello");
});
I am now trying to read my serial port with node-serialport, but it doesn't work. When I am trying to read the serialport with node-serialport (and using echo to write on it), the data from echo are not writen in the terminal. The terminal only says "open". I am using this code, same source:
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyAMA0", {
baudrate:9600,
databits: 8,
parity: 'none',
stopBits: 1,
flowControl: false,
parser: serialport.parsers.readline("\n"),
});
sp.on('open', function() {
console.log('open');
sp.on('data', function(data) {
console.log('data received: ' + data);
});
});
I don't understand what's happening here. Any help would be much appreciated!
Thanks a lot! :)
Nicolas
Problem solved, shell and kernel messages on the serial connection wasn't disabled with the raspi-config tool to prevent the kernel from using the serial port. (sudo raspi-config, Advanced-Options, Serial, No)
Thanks to fivdi : https://github.com/voodootikigod/node-serialport/issues/715
Nicolas

Linux Virtual Serial Port Not Showing Up In List

None of the solutions from this similar problem help: Socat virtual serial port not recognized
I'm creating a virtual serial port using socat and then linking it properly, but node-serialport is not picking it up.
$ sudo socat -d -d pty,raw,echo=0,link=/dev/ttyS10 pty,raw,echo=0,link=/dev/ttyS11
2015/09/01 01:36:44 socat[22927] N PTY is /dev/pts/9
2015/09/01 01:36:44 socat[22927] N PTY is /dev/pts/10
2015/09/01 01:36:44 socat[22927] N starting data transfer loop with FDs [3,3] and [5,5]
In node using node-serialport:
$ cat test.js
var serialPort = require("serialport");
serialPort.list(function (err, ports) {
if (err) {
console.log(err);
return;
}
ports.forEach(function(port) {
console.log(port.comName);
console.log(port.pnpId);
console.log(port.manufacturer);
});
});
$ node test.js
{ [Error: ENOENT, scandir '/dev/serial/by-id'] errno: -2, code: 'ENOENT', path: '/dev/serial/by-id' }
You'll notice that no serial port is being picked up. What can I do to be able to list this port?

In Node.js, I am getting an error saying that I have an unexpected identifier for setTimeout(). What's going on?

As a beginner in programming, I am doing a fun project with a raspberry pi. I am writing a simple program for a raspberry pi with a node module, and I would like to delay the GPIO pins of my pi to turn on and off (for now). I'm using set Timeout() to delay my turning off the GPIO pin.
The big problem is that when I run my file, I get an error saying that setTimeout() is an unexpected identifier. Some help?
More information on the node module that I used can be found in this link: https://www.npmjs.com/package/pi-gpio
My code:
var gpio = require("pi-gpio");
var timers = require("timers"); //thought this might help fix problem
function start(){
gpio.open(7, "output", function(err) { // Open pin 7 for output
gpio.write(7, 1, function() { // Set pin 7 high (1)
gpio.close(7); // Close pin 7
});
});
}
function stop(){
gpio.open(7, "output", function(err) { // Open pin 7 for output
gpio.write(7, 0, function() { // Set pin 7 low(0)
gpio.close(7); // Close pin 7
});
}
setTimeout(start,1000);
setTimeout(stop,3000);
This is the error I got on the SSH terminal:
pi#raspberrypi ~ $ node armrobot.js
/home/pi/armrobot.js:16
setTimeout(start,1000);
^^^^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
You didn't close the stop function correctly.
function stop(){
gpio.open(7, "output", function(err) { // Open pin 7 for output
gpio.write(7, 0, function() { // Set pin 7 low(0)
gpio.close(7); // Close pin 7
});
});
}

Node js 0.10.7: cluster support for udp dgram?

I'm trying to run following node js application as mentioned https://github.com/joyent/node/issues/2194
var util = require("util"),
dgram = require("dgram"),
cluster = require('cluster');
var udp = dgram.createSocket("udp4");
var port = 1190;
if (cluster.isMaster) {
for (i = 0; i < 2; i++) {
cluster.fork();
}
} else {
util.log("starting udp server on port " + port);
udp.on("error", function (error) {
util.log("failed to bind to UDP port - " + error)
});
udp.bind(port);
}
The app exits immediately with the following output:
23 May 23:22:13 - starting udp server on port 1190
23 May 23:22:13 - starting udp server on port 1190
events.js:72
throw er; // Unhandled 'error' event
^
Error: write ENOTSUP - cannot write to IPC channel.
at errnoException (child_process.js:980:11)
at ChildProcess.target.send (child_process.js:455:16)
at Worker.send (cluster.js:401:21)
at sendInternalMessage (cluster.js:394:10)
at handleResponse (cluster.js:177:5)
at respond (cluster.js:192:5)
at Object.messageHandler.queryServer (cluster.js:242:5)
at handleMessage (cluster.js:197:32)
at ChildProcess.EventEmitter.emit (events.js:117:20)
at handleMessage (child_process.js:318:10)
Does anyone know what is going on? When running this without cluster, everything is fine.
It seems that cluster does not support udp?
Some specs:
Window 7 x64
node js 0.10.7
It says in the link your provided that support for UDP clustering was added in v0.11.14. It is likely that you simply need to update your version of node.js

node.js to read usb port signals

I have installed serialport module for node.js using npm.(npm install serialport).
Now I would like to send some messages from my android phone to node application via usb port of my computer(assuming node can read serial port signals).
Has anyone done this before?
Thanks in advance.
Why, yes, it's certainly doable. There's a plenty of examples listed at the serialport package homepage itself.
Your node.js application will start with...
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("/dev/tty-usbserial1");
serialPort.on('data', function(data) { /* processing data */ });
For testing your ports on Windows (you may have to install sf "npm install sf"):
var serialport = require('serialport');
var sf = require('sf');
serialport.list(function (err, results) {
if (err) {
throw err;
}
for (var i = 0; i < results.length; i++) {
var item = results[i];
console.log(sf('{comName,-15} {pnpId,-20} {manufacturer}', item));
}
});
Output should be something like:
COM8 FTDIBUS\VID_0403+PID_6001+A100DKP7A\0000 FTDI
COM1 ACPI\PNP0501\4&2E24A907&0 (Standardanschlusstypen)
Now use the port your device is connected to, in my case COM8:
var SerialPort = require("serialport").SerialPort
var serialPort = new SerialPort("COM8");
regards

Resources