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

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
//...
}
}
});

Related

Can't get hidraw0 device to open in nodeJS app

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.

TCP/IP Communication, Executing System Commands and Local File Access using Metoer

So, I have this intranet application which parses a bunch of excel files, a read-only MSAccess DB, reads some data from custom hardware connected to serial ports and the network and controls a Vector CANoe simulation using Windows COM. The entire app is written in Python with CherryPy being used for serving web content.
Now new features (e.g. support for users) need to be added to the webapp, I want to port as much of this app as I can to Meteor (using v1.3), mostly because how easy it is to get a prototype up and running.
I can leave the non-web related application logic in the Python app and have Meteor talk to it using IPC implemented over TCP/IP; but at the very least, I need Meteor to be able to execute system commands and connect to TCP/IP sockets.
I've read that the NodeJS net API can be used for the TCP/IP communication part but have yet to figure out how exactly to do it, any help would be much appreciated.
Meteor already includes 'net', we can just use it as follows:
if (Meteor.isServer) {
var net = Npm.require('net');
var server = net.createServer(function (client) {
console.log('A client connected!');
});
server.listen(17231, function() {
console.log('Server bound to 17231!');
});
};

socket.io io.sockets.adapter.rooms[room] in 3G not working

I have a really strange issue going on here in a socket.io development.
Everything is working just fine (emits to/from server) except that from a 3G connection the server does the following:
console.log( io.sockets.adapter.rooms['thisroom'] )
This line, almost every time, returns a typeof of 'undefined', as if there where not sockets connected in the given 'thisroom' room when there actually are sockets connected to the room.
If the same code is executed from a WIFI connection, under the same circumstances, with the same sockets connected, the above line returns something like:
Room { sockets: { '/#wEkHFuAhbCEf1Dw2AAAC': true }, length: 1 }
which is correct.
I've tried everything with this as to:
Tried socket.io version 1.3.7
Tried socket.io version 1.4.8
Tried to connect with
var socket = io.connect("http://<ip>:8081");
Tried to force to websockets transport only with:
var socket = io.connect("http://<ip>:8081", {transports: ['websocket']});
Tried to use two transports as:
var socket = io.connect("http://<ip>:8081", {transports: ['websocket', 'polling']});
Nothing worked.
It is really weird because sometimes, the 3G connection works as expected when just restarting the app. But most of the time, it does work.
Ideas?
I just managed to make it work by using a secured connection with SSL. Under SSL, io.sockets.adapter.rooms['thisroom'] works every time in any 3G connection.

Solving Socket.IO socket undefined on connection from Arduino?

I'm trying to connect to socket.io from arduino. The service works from the browser, but if I try to connect from the arduino, when I look in the node log I created via console.log when a connection happens, the socket is undefined, but picks up the connection. Below is the protocol switching request, which returns a 404 not found, but socket.io does register a connection but doesn't define the socket.
client.print(F("GET /socket.io/1/websocket/"));
client.print(sid);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.print(hostname);
client.fastrprint(F(":"));
char portBuffer[10];
itoa(port, portBuffer, 10);
client.fastrprint(portBuffer);
client.print(F("Sec-WebSocket-Key: ")); //dGhlIHNhbXBsZSBub25jZQ==
client.print(F("dGhlIHNhbXBsZSBub25jZQ=="));
client.println(F("Origin: ArduinoSocketIOClient"));
client.println(F("Sec-WebSocket-Protocol: chat, superchat"));
client.println(F("Sec-WebSocket-Version: 13"));
client.println(F("Upgrade: websocket"));
client.println(F("Connection: Upgrade\r\n"));
I was thinking maybe I formed the request incorrectly, but I can't find any information for the socket.io protocol switching request so I don't know what to change. What I have done is loosely based on what I read in web socket protocol stand.

Arduino Uno + GSM Shield SIM900 TCP Client Dont Receive Data

I need to communicate an Arduino Uno R3 with Tinysine GSM Shield (sim 900 module) to a NodeJS Server Socket using TCP/IP sockets. So, the embedded system is a TCP client. I need the Arduino sends some message and receive the answer, using the received data to blink a different collor led. The tcp socket is working, and I can send the message, and process it in my server socket, but cant receive the answer (socket.write) in embedded (actually, I receive some fuzzy and variables caracters).
My Server Socket works fine, using the Hercules like client TCP, I could complete whole process.
I am using the SIM900 and InetGSM libraries with AT Commands to TCP connection (initially I dont want to use AT HTTP connection because It would change my system).
How do I can receive a legible message from my server socket? Thanks
I solved it!
When I send the AT+CIPSEND command, what I receive is a sequence of caracters with the AT response for the command and the data sent from my server.
So, what I need to do is storing it in an array and select the data position in this array or pick the answer in the right array position, like example below.
For example:
//Im waiting for char '1'
//After sending message with AT commands ...
char answer;
for(i = 0; i < 15; i++){ //15 is an random limit value that worked for me, I dont know why
answer = (char)gsm.read();
if(answer == '1'){
Serial.println("I find the answer!");
}
}

Resources