Can't get hidraw0 device to open in nodeJS app - node-serialport

Good evening!
I'm trying to setup a barcode scanner app using the Node SerialPort package, used on a Raspberry Pi 4, and I'm having a hard time getting the code to either recognise the device
I've identified that the path is /dev/hidraw0 as this is what I was able to ascertain from a dmesg output.
When I run cat /dev/hidraw0 it opens and when I scan a barcode it outputs %""'#''!'&" to the console (I know it's gibberish, but different problem for a different day). BUT, when I reference this path in my nodeJS code, I get the below error:
Serial port error: Error: Error: Invalid argument setting custom baud rate of 9600
I've confirmed on the manufacturer website that the default baud rate is 9600. I have tried removing the baudRate option in the below code, but it still says the same error.
This is the code I'm using right now:
// Use the serialport module to get scanner data
const SerialPort = require('serialport');
const Readline = require('#serialport/parser-readline')
// Open the serial port with some configuration
const port = new SerialPort('/dev/hidraw0', {baudRate: 9600});
const parser = new Readline()
port.pipe(parser)
// When the port is open just say so
port.on('open', function() {
console.log('Serial port open');
});
// Check for errors on the serial interface
port.on('error', function(err) {
console.log(`Serial port error: ${err}`);
});
// Pass scanner data onto web server
port.on('data', function(data) {
console.log('Scan prompted');
});
The USB Scanner I'm using is the Zebra LS2208 and I would love some help or guidance on what may be causing this.
Thanks in advance!

In case anyone else comes around this, I was not able to get the device working with the NPM package I referenced, but as this module used node-hid, I decided to use that one and was able to get my scanner recognised and working properly.

Related

trying to connect to TCP server using Node.js

I'm trying to connect to a server/specific port using Node.js, and I don't even get past var net = require('net');
I'm using Node.js v16.15.0.
Welcome to Node.js v16.15.0.
When I use the command above, I receive UNDEFINED. As far as I know, I've installed everything I need (including socket.io), and I'm working within the Node.js environment in iTerm.
My goal is to connect to a TCP server, receive a list of files, and then download each of them over a persistent socket. But I'm a little stuck as I can't even seem to get into the TCP server in the first place.
This is what I think I'm supposed to run to get in (obviously with my correct port and IP info which is omitted below).
var HOST = 'IP';
var PORT = 'PORT'
var FILEPATH = 'myfilepathhereIwilltweakitwhenIgettothispoint';
Can anyone point me in the right direction?
From what you said, I think you are trying to code NodeJS script within the NodeJS executable start in command line. You get an UNDEFINED because you imported the library into your variable and this assignment does have any value, so it is UNDEFINED. You can read more about this in this subject : link
But what we usually do in NodeJS development is creating a file, let's call it index.js. Inside that file we are writing our code, let's say :
const net = require('net');
const client = net.createConnection({ port: 8124 }, () => {
// 'connect' listener.
console.log('connected to server!');
client.write('world!\r\n');
});
client.on('data', (data) => {
console.log(data.toString());
client.end();
});
client.on('end', () => {
console.log('disconnected from server');
});
Code sample from NodeJS Documentation.
Then we want to run our code by using the command line like this : node path/to/index.js.
Hope it helps !

open serialport and log data automatically under various events

I am using a COM3 port and iam using the serialport module to read from the serialport. What I want is an automatic logging system which logs the data from serialport under the following events.
1.close program and reopening the program
2.restart my pc
3.unplug the usb cable and plug it again
4.restart the Arduino device
I want to open the serialport and log data received from it automatically whenever the above events occur. How to handle these cases.
Currently, this is the code iam using
var fs = require('fs');
const SerialPort = require('serialport')
const Readline = require('#serialport/parser-readline')
const port = new SerialPort('COM3')
const parser = new Readline()
port.pipe(parser)
parser.on('data', function (data) {
const index = data.indexOf('*#SENSOR_DATA')
if(index != -1){
fs.appendFileSync("sensor_data.txt", new Date(), 'utf8')
fs.appendFileSync("sensor_data.txt", data, 'utf8')
}
})
port.on('error', function(err) {
fs.appendFileSync("sensor_data.txt", new Date(), 'utf8')
fs.appendFileSync("sensor_data.txt", err, 'utf8')
console.log(err);
})
port.write('ROBOT PLEASE RESPOND\n')
How can I handle the above mentioned cases?
close program and reopening the program
Logging the data from the port on starting of the program, should be straight forward, i.e. the running of your script.
Closing of the program, depends on what you scenarios you want to handle, graceful or non graceful exits of the program. To implement this you can add event listeners on the nodeJS process object. You can register event handlers for when the process is going to exit to do some action.
process.on('SIGINT', () => {
// read my data from Serial
process.exit(-1);
})
restart my pc
Depending on what your OS is, you could configure your nodejs process as a service, or you could run it via cron. For example cron (depending on the OS) has the option for scheduling jobs on reboot. Regardless of your OS, you will be able to schedule jobs or create a service to ensure your process starts when your machine restarts.
unplug the usb cable and plug it again &
restart the Arduino device
For this I would look at the different parsers that are available for serialport. For example the ReadyParser.

deploy nodejs modbus-rtu using the modbus-serial library to HEROKU

Recently, i want to deploy nodejs to heroku using modbus-serial library and socket.io, the idea here is i want to retrieve temperature real time from pt100 using serial RS-485,
locally, the code run well having no problem, but went i deploy to heroku, it seems like heroku didnt recognize the usb port "/dev/ttyUSB0",here what i got,
UnhandledPromiseRejectionWarning: Error: Error: No such file or directory, cannot open /dev/ttyUSB0,
its because heroku doesnt know what it is, or do i have to set the environment variable for the usb port,
here my code,
const ModbusRTU = require("modbus-serial")
const client = new ModbusRTU()
const app = require('http').createServer(server)
app.listen(process.env.PORT || 5000)
const io = require("socket.io")(app)
io.on("connection",function(socket){
client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 115200 }).then(() => {
setInterval(() => {
client.writeFC3(1,0,2,(err,data) => {
if(err)console.log("error ",err)
console.log("datas ",data)
io.emit("datas_holding_register_rtu",{ datas : data["data"] })
})
},100)
})
socket.on('disconnect', () => console.log('Client disconnected'))
})
any idea what happening here ? thank you
sincerely,
toni
So... Heroku is a cloud system, and serial communication occurs LOCALLY WITH a cable attached to your computer to the USB port. So, Heroku cannot communicate with your device, because it doesn't have an USB port. Solution: Run your code on a computer connected to the usb cable.

node-serialport only communicates with Arduino if another app has already connected to the port

Running on node in OS X, I am trying to use node-serialport to talk to an Arduino. All communication to the Arduino works as expected when using Arduino IDE's Serial Monitor, or the OS X utility SerialTools. However, when just running my node app, node-serialport tells me the connection is successful, but I get no communication. If I first make a connection to the arduino with Arduino IDE's Serial Monitor or SerialPorts, then run my node app, the node app sends and receives data just fine using node-serialport.
I'm not familiar with serial communication, but it seems like the other serial utilities are able to properly start a connection (which is then available to node-serialport), but node-serialport is not able to connect on its own.
Is there a way to get absolutely all connection information, so I can compar the utilities' successful connections to node-serialports non-working connection?
Any other ideas as to why this would be happening?
I have a working solution, but unfortunately not a complete explanation. Reviewing some related issues such as What's going on after DTR/RTS is sent to an FTDI-based Arduino board?, I determined that even just restarting the node app (rather than requiring another serial connection app) gave node the ability to communicate through the serial port. I'm beyond my depth, but I suspect that initially establishing the RTS connection restarts the arduino, and only after that happens can node-serialport communicate through the connection.
My workaround is to simply give the Arduino some time to reset before attempting a second serialport connection, which works.
var firstConnect = true;
serialPort.open(function (error) {
if (firstConnect){
firstConnect = false;
//First connection, letting Arduino reset
setTimeout(function(){serialPort.open()},4000)
} else {
//Second connection, which will work
serialPort.on('data', function(data) {
//data parsing function
//...
}
}
});

How to access a Child Process' sockets in NodeJS?

I am currently running into a very strange problem.
I am trying to start VLC using a NodeJS child process and then accessing it's Remote Control (RC) interface using socket. The problem occurs when connecting to this socket. I get an error, connection refused. The port is open and the application is allowed from the firewall.
The tricky part is, when I open VLC manually using this interface, and only try to connect on the socket, it works. I am assuming something in the spawned process makes things different causing the error somehow.
Here is the code I am trying to run:
var spawn = require('child_process').spawn;
var file_dir = "V:\\TEST\\";
var files = ["Ika.mkv", "Nami.mkv", "Azu.mkv"];
var player = spawn("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe", ['--intf="rc"', '--rc-host="localhost:3000"', '--fullscreen', file_dir + files[0]]);
var net = require('net');
var client = net.createConnection(3000, "localhost");
client.on('connect', function() {
console.log('connected to VLC on port 3000');
client.write("add " + file_dir + files[1] + "\n");
client.write("enqueue " + file_dir + files[2] + "\n");
client.write("help" + "\n");
});
client.on('data', function(data) {
console.log(data.toString());
});
client.on('end', function() {
console.log('disconnected from server');
});
I have tried this code on two machines, and I am running into the same problem.
Some questions you may ask:
What operating system? Windows 8.1
Why do I need to use a socket?
VLC doesn't have any interfaces that read and write from standard in or standard out. I have tried many different options and they simply do nothing.
What am I trying to build?
A Media Center with an web interface to it. I am using VLC as a media player.
Can't you use the built in HTTP interface?
It doesn't suit what I want to build. I want more control over managing my media.
Any and all help would be welcome. My thanks.
Turns out for some strange reason not all command line arguments get passed to the VLC instance.
I solved it by grouping together the instancing of the RC interface and the setting the RC mode to localhost:3000
This is the new line to spawn a process
var player = spawn("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe", ['-I rc --rc-host=\"localhost:3000\"','--fullscreen', file_dir + files[0]]);
This works, only downfall is it also creates a RC console window, but I can live with it.
Thanks to #jfriend00 for helping solve the strange mystery.

Resources