SerialPort NodeJS - SerialPort is not a constructor - node.js

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…

Related

How can I import a NodeJS variable which comes out of my Arduino into my main web game JavaScript file?

I am making a web game with threejs, cannonjs, serialport library and arduino with 2 buttons. However when i am trying to import the button press value into my main javascript game file, it sends me into a loop of errors concerning the relative imports of the serial port library.
const { SerialPort } = require('serialport')
const { ReadlineParser } = require('#serialport/parser-readline')
const port = new SerialPort({ path: 'COM11', baudRate: 9600 })
const parser = port.pipe(new ReadlineParser({ delimiter: '\r\n' }))
var eparser = parser.on('data', console.log);
export default eparser;
This is the code to get the button press into the console log.
This is the kinds of errors i run into regarding the serial port library's relative imports
I have tried many other ways like socket.io, johnny five and other such, but have had no luck with finding a solution for my exact problem.

SerialPort.list is not a function

I tried to create an array of port, to which serial devices are connected using the serialport module of nodeJS.
I used the following code, which should in theory work I think:
var getPortsList = (callback) => {
var portsList = [];
SerialPort.list((err, ports) => {
ports.forEach((port) => {
portsList.push(port.comName);
});
callback(null, portsList);
});
};
Whenever I execute it tho, I get the following error: TypeError: SerialPort.list is not a function.
U tried to google the problem, but could not find anything useful.
Help in any way is greatly appreciated.
SerialPort.List is deprecated from SerialPort core module and moved to SerialPort/List as a command tool.
npm install #serialport/list

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

socket.io-redis trailing bytes

I am using socket.io-emitter to broadcast an event to a set of channels with a for loop:
In the file, I have:
var io = require('socket.io-emitter')({
host: 'localhost',
port: 6379
});
module.exports = {
exampleFunction: function(req, res, next) {
var channels = req.param('channels'),
data = req.param('data');
for (var i=0; i<channels.length; i++) {
io.to(channels[i]).emit('example event', data)
}
}
}
In app.js, I have socket.io-redis:
io.adapter(socketio_redis({
host: 'localhost',
port: 6379,
pubClient: redis.createClient(6379, '127.0.0.1'),
subClient: redis.createClient(6379, '127.0.0.1')
}))
When I try to run exampleFunction, I get the following uncaught error in my console:
Error: 348 trailing bytes
at Object.decode (C:\Users\Website\socket.io-redis\node_modules\msgpack-js\msgpack.js:200:47)
at Redis.onmessage (C:\Users\Website\socket.io-redis\index.js:93:24)
at RedisClient.EventEmitter.emit (events.js:106:17)
at RedisClient.return_reply (C:\Users\Website\node_modules\redis\index.js:672:22)
at ReplyParser.<anonymous> (C:\Users\Website\node_modules\redis\index.js:309:14)
at ReplyParser.EventEmitter.emit (events.js:95:17)
at ReplyParser.send_reply (C:\Users\Website\node_modules\redis\lib\parser\javascript.js:300:10)
at ReplyParser.execute (C:\Users\Website\node_modules\redis\lib\parser\javascript.js:211:22)
at RedisClient.on_data (C:\Users\Website\node_modules\redis\index.js:534:27)
at Socket.<anonymous> (C:\Website\node_modules\redis\index.js:91:14)
I have seen that this is from msgpack. Have any of you encountered this error before? How did you resolve it?
Thank you.
On the main page of nm.socket.io-redis it is written:
Msgpack with giving us an error called "trailing bytes". After reading
up we realized that we could just use JSON.stringfy/JSON.parse instead
of msgpack.
Which looks like the error you are getting. As it is suggested there, try JSON.strigfy
Okay,
We found the solution. Refer to:
https://github.com/Automattic/socket.io-redis/issues/17
As you can see, socket.io-emitter requires you set return_buffers to true on your redis client.
Let me know if this works. Otherwise I can dif in the code some more.
This should answer your question.
https://github.com/Automattic/socket.io-redis/issues/17
This helped me with Error: -560815898 trailing bytes error:
npm install msgpack-js-v5
change file node_modules/socket.io-redis/index.js at the very top:
var msgpack = require('msgpack-js');
to
var msgpack = require('msgpack-js-v5');
now use code similar to this:
var adapter = require('socket.io-redis');
var pub = redis.createClient(6379, 'localhost');
var sub = redis.createClient(6379, 'localhost', { return_buffers: true });
io.adapter(adapter({pubClient: pub, subClient: sub}));
UPDATE:
In some days I stumbled upon an error bops.readUInt64BE is not a function, so eventually I switched msgpack-js-v5 to msgpack5 and now it works ok.

NodeJS - How to handle "listen EADDRINUSE" when accessing external process

I'm using phantomJS for printing PDF, with phantomjs-node module. It works well but when I try to create several files at once, it throws an Unhandled error "Listen EADDRINUSE.
I assume this is because the module uses phantomJS which is an external process and it can't bind it to the same port several times ?
Anyway, I can't catch this error, and I'd like to resolve this problem at least by avoiding a server crash when this happens.
I thought of using a "global" variable, like a locker, in order to block concurrent calls until the current one is finished.
Any idea of how to implement that, or any other solution ?
The code from #AndyD is not correct imho. See lines 45 - 54 in
https://github.com/sgentle/phantomjs-node/blob/master/phantom.coffee
So the example should be
var portscanner = require('portscanner');
var phantom = require('phantom');
portscanner.findAPortNotInUse(40000, 60000, 'localhost', function(err, freeport) {
phantom.create({'port': freeport}, function(ph){
...
}
});
You should be able to pass in a port number every time you call create:
var phantom = require('phantom');
phantom.create(null, null, function(ph){
}, null, 11111);
You can then use a counter to ensure it's different every time you start phantomjs-node.
If you are starting a new process every time and you can't share a counter then you can use portscanner to find a free port:
var portscanner = require('portscanner');
var phantom = require('phantom');
portscanner.findAPortNotInUse(40000, 60000, 'localhost', function(err, freeport) {
phantom.create(null, null, function(ph){
...
}
}, null, freeport);

Resources