Disconnecting serial cable crashes nodejs - node-serialport

I am using node 0.10.36 with node-serialport 1.7.4 on windows 7 64bit
If I run node with serial cable plugged, the code is running with a serial port listed. If unplug the cable, the node is crashed.
Here is my code:
var serialport = require("serialport");
var sp = null;
serialport.list(function (err, ports) {
ports.some(function (port) {
sp = new serialport.SerialPort(port.comName, {
baudrate: 19200 });
sp.on('open', function () {
console.log('Opened ' + port.comName);
});
sp.on('error', function () {
console.log('Errored!!!');
});
sp.on('close', function () {
console.log('Closed!!!');
});
return true;
});
});
The console output is:
Opened COM3
undefined:0
TypeError: undefined is not a function
I appreciate any input why error/close events are not triggered, and how to make the nodejs running if serial port is not found. Thanks!

Related

WebSocket to Serial bridge, send control chars like `\r\n` does not work

Im developing a WebSocket to Serial bridge.
The WebSocket server send some commands that are writen 1:1 to the serial device.
const { WebSocket } = require("ws");
const { autoDetect } = require("#serialport/bindings-cpp");
const { SerialPort } = require("serialport");
const binding = autoDetect()
const ws = new WebSocket("ws://example.com");
ws.on("open", () => {
console.log("WebSocket connected to:", ws.url);
const port = new SerialPort({
binding,
path: "/dev/ttyUSB0",
baudRate: 19200,
});
ws.on("message", (chunk) => {
console.log("From server", chunk);
port.write("sw i03\r\n"); // this works!
port.write(chunk) // this does not!
});
port.on("data", (data) => {
console.log("From port", data);
ws.send(data);
});
});
The messages string that comes from the server is sw i02\r\n.
Message format is the following, "sw i0\r\n", where <n> is a number between 1 - 8.
The problem is, \r\n is not interpreted as newline/control char, but literally as "\r\n" and not the control character they should represent.
Why does the serial device not recognize the message from the WebSocket as correct command, but when i rewrite it to a static port.write("sw i03\r\n")?
I simply dont understand whats the difference between the message from the WebSocket and the static port.write(..) i used to test.
The serial device is a Aten 8 port HDMI Switcher (VS0801H): https://www.aten.com/us/en/products/professional-audiovideo/video-switches/vs0801h/
EDIT/Workaround:
I changed the code/command payload, so now control character are stored in the backend/server side anymore and add them later on the "client"/serial side:
ws.on("message", (chunk) => {
console.log("From server", String(chunk));
port.write(`${chunk}\r\n`); // chunk = "sw i0<n>"
});
If any one know how to handle my problem, please let me know.
The solution i cam up with, feels more like a hack than a real problem solving solution.

NodeJS Net module doesn't send message to server

I am using NodeJS Net module to send a TCP message to a server.
I studied some code on the internet and end up with this simple code:
var enviarMensagemTCP = function (mensagem, port, host) {
const Net = require("net");
const client = Net.createConnection({ port: port, host: host });
console.log(`enviar mensagem TCP para ${host}:${port}`);
client.on("connect", function () {
client.write(`${mensagem}`, () => {
client.destroy();
});
});
client.on("error", function (error) {
client.destroy();
});
};
My client is successfully connection to the server, however no message is read by the server I wrote in delphi.
IDPessoa := AContext.Connection.Socket.ReadLn;
The problem was simple, I was trying to read the line in the server, however, my client was not sending the data line by line.
Just added a line breaking after the message fixed the issue.
client.write(`${mensagem}\r\n`, () => {
client.destroy();
});

Serialport module in node.js works only if minicom is active on the port

I am using version 3.10 of the serialport module of Node.js.
I have a GSM modem attached to an Ubuntu machine's serialport and I am sending SMS through the modem. Here is the simplified code:
var serialPort = require("serialport");
const Readline = serialPort.parsers.Readline;
var portSerial = new serialPort("/dev/ttyUSB1", {
baudrate: 115200,
dataBits:8, stopBits:1, parity: 'none'
}, function (err) {
if (err)
//log error here
});
parser = new Readline();
portSerial.pipe(parser);
portSerial.on("open", function(err) {
if (err)
return console.log("Error in opening serial port");
console.log("Port opened");
});
portSerial.on('error', function(err) {
//log error
})
//Send SMS
setTimeout(function() {
portSerial.write('AT+CMGF=1\nAT+CMGS="'+SMSphone + '"\n' +
SMSmessage + '\032');
}, 1000);
Yes, I am setting SMSphone and SMSmessage variables. And the code is actually a bit more complex but the core code for sending SMS is as shown above.
PROBLEM: All works fine if I am running minicom when the SMS is sent. The moment I exit minicom, the SMS do not go out. portSerial.write stops working.
It was all working fine until I upgraded the serialport version.

Serial Port not working after disabling login shell

I'm trying to write a small program in node.js to communicate over serial port. I'm using node.js for that with a sample code like that:
var SerialPort = require("serialport");
var serialPort = new SerialPort("/dev/ttyAMA0", {
baudrate: 9600
});
serialPort.on("open", function () {
console.log('open');
serialPort.on('data', function (data) {
console.log('data received: ' + data);
});
setInterval(function () {
serialPort.write(new Buffer('4', 'ascii'), function (err, results) {
console.log('err ' + err);
console.log('results ' + results);
});
}, 1000);
});
This is however not working at all after i disable the login shell over serial port serial port in raspi-config. After that i'm unable to send/recieve any data.
I connected pin 8 with pin 10 to create a self-loop and tried using minicom to send something over it with no success as well.
I checked the /boot/config.txt and enable_uart is set to 1.
I also checked the /boot/cmdline.txt and there are no entries with AMA0 port.
Can anybody provide me with a solution to this problem?
For anyone looking for solution - this did it for me and opened a serial port.
https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=159984&p=1039065#p1039065

NodeJs Buffer CRLF

I want to use Nodejs bluetooth-serial-port plugin and Buffer to send "be\r\n" to my Bluetooth device. (It's works only with that)
With this Android tool it's working.
The problem is my Bluetooth device can't recognise the message.
Here is my code:
var btSerial = new (require('bluetooth-serial-port')).BluetoothSerialPort();
btSerial.connect(device, channel, function () {
var data = new Buffer('be\r\n', 'utf-8');
console.log('connected, sending: ' + JSON.stringify(data.toString('utf-8', 0, 4)));
btSerial.write(data, function (err, bytesWritten) {
if (bytesWritten) {
console.log("writed: " + bytesWritten);
btSerial.close();
console.log("disconnected.");
}
if (err) console.log(err);
});
btSerial.on('data', function (buffer) {
console.log(buffer.toString('utf-8'));
});
}, function () {
console.log('cannot connect');
});
And the output:
connected, sending: "be\r\n"
writed: 4
disconnected.
Finally I found the solution: The Bluetooth agent must run to work with bluetooth-serial-port, and also the encoding have to be ASCII.

Resources