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

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

Related

SerialPort NodeJS - SerialPort is not a constructor

Good afternoon, I have a problem with Serial Port ,
I did everything as described in the documentation, but still I get an error SerialPort is not a constructor
const { SerialPort } = require('serialport')
const port = new SerialPort({
path: 'COM4',
baudRate: 9600,
autoOpen: false,
})
Spent several hours solving the problem, tried a lot of things and got to the official documentation, but there was an error here too :)
my nerves left the chat…

Serial port information like manufacturer/description is unavailable on Windows but available on Linux, using node.js with serialport package

I wrote a node.js script that connects to an external device via an USB serial port. The external device uses a chip from FTDI that converts to and from USB signals. I programmed basic device information like manufacturer, description, serial number etc. to that chip with FT_Prog, assuming this information is provided when I read the port information in my node.js script (using serialport package).
However it seems that I cannot retrieve that information on my Windows system. When I read the serial port information I am connected to, the manufacturer always is FTDI, description is undefined. But when using a Linux system, that information is available.
This seems to be the same topic. But as far as I understand I should be able to get the information as soon as I programmed the chip with FT_Prog. But it's still not working.
So why is it, that I cannot retrieve this information on Windows, but on Linux?
Thanks in advance!
EDIT
The code I am using:
'use strict';
const serialPort = require('serialport');
searchFscPort();
function searchFscPort() {
serialPort.list().then(ports => {
for(let i = 0; i < ports.length; i++) {
if(ports[i].manufacturer == 'FTDI' || ports[i].manufacturer == 'ME') {
console.log('Path: ', ports[i].path);
console.log('Manufacturer: ', ports[i].manufacturer);
console.log('SN: ', ports[i].serialNumber);
console.log('lID: ', ports[i].locationId);
console.log('pID: ', ports[i].productId);
console.log('vID: ', ports[i].vendorId);
console.log('pnpID: ', ports[i].pnpId);
return;
}
}
});
}
Result on Windows:
path: COM4
manufacturer: FTDI
SN: 110
lID: undefined
pID: 6015
vID: 0403
pnpID: FTDIBUS\VID_0403+PID_6015+110A\0000
Result on Linux:
path: /dev/ttyUSB0
manufacturer: ME
SN: 110
lID: undefined
pID: 6015
vID: 0403
pnpID: usb-ME_FSC1_7_110-if00-port0
This is what I have programmed into the chip:
By the way, there seems to be no node.js property to get the "Manufacturer Desc"?

Raspi-io: Error: Unknown pin "null"

I'm using the johnn-five and raspi-io packages for node and I'm trying to read the events created on a PIR motion sensor, but everytime I run the application, an error is thrown.
console
pi#raspberrypi ~/poc $ sudo node app.js
1439476578608 Device(s) RaspberryPi-IO
1439476578749 Connected RaspberryPi-IO
1439476578825 Repl Initialized
>> /home/pi/poc/node_modules/raspi-io/lib/index.js:316
throw new Error("Unknown pin \"" + pin + "\"");
^
Error: Unknown pin "null"
at Raspi._defineProperty.value (/home/pi/poc/node_modules/raspi-io/lib/index.js:316:17)
at Raspi.pinMode (/home/pi/poc/node_modules/raspi-io/lib/index.js:327:47)
at Motion.Controllers.PIR.initialize.value (/home/pi/poc/node_modules/johnny-five/lib/motion.js:27:17)
at new Motion (/home/pi/poc/node_modules/johnny-five/lib/motion.js:180:10)
at Board.<anonymous> (/home/pi/poc/app.js:9:16)
at Board.emit (events.js:104:17)
at process._tickDomainCallback (node.js:381:11)
package.js
{
"name": "poc",
"version": "0.0.1",
"main": "app.js",
"private": true,
"dependencies": {
"johnny-five": "0.8.86",
"raspi-io": "3.3.4"
}
}
app.js
var raspi = require('raspi-io');
var five = require('johnny-five');
var board = new five.Board({
io: new raspi()
});
board.on('ready', function() {
var motion = new five.Motion({pin: 'PI-17'});
motion.on('calibrated', function() {
console.log('calibrated');
});
motion.on('motionstart', function() {
console.log('motionstart');
});
motion.on('motionend', function() {
console.log('motionend');
});
});
However, the following code DOES seem to work:
snipped from another poc
var raspi = require('raspi');
var gpio = require('raspi-gpio');
raspi.init(function() {
var input = new gpio.DigitalInput({
pin: 'P1-17'
});
var logInput = function() {
console.log('Input 17: ' + input.read());
setTimeout(logInput, 1000);
};
logInput();
});
The Raspberry is a Rev. 2 model B.
The Motion detector I'm using is this one.
The cables are connected as follows (physical pin, taken from this schema)
Motion Sensor -> Pi
1 GND -> 6 GND
2 VDD -> 1 +3/V3 OUT
3 OUT -> 11 GPIO17
Any help would be greatly appreciated.
After more searching I came across this image.
It provided me with more insight on the WiringPi Pin numbers, BCM GPIO naming and P1 naming.
The pin GPIO0 on Pin 0 (BCM 17) seems to be throwing the error.
I switched to using pin GPIO4 on Pin 4 (BCM 23).
It doesn't really make sense why the 0 doesn't work, but for now, the poc I'm working on can progress again.
Your issue wasn't with any of your code, or a naming issue. You unfortunately chose poorly for which pin to use. P1-17 isn't able to accept an input, as it is dedicated 3.3v out. Choose another pin, and you should be fine.
Any pin reserved for I2C or Serial can't be used for regular GPIO function within Johnny Five, along with the dedicated power and ground pins which can't be used for IO, ever. As long as you avoid these ports, you'll be fine. A good resource on Raspberry Pi pinouts is here.

node.js and serialport cannot list any ports

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.

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