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

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.

Related

How to run serialport once port is ready to receive the data?

Good day everyone,
I have the following code:
var SerialPort = require('serialport');
var port = new SerialPort("COM6", {
baudRate: 38400
});
port.on('open', function(data, callback) {
port.write(Buffer.from('open1', 'ascii'), function(err) {
if (err)
return sendData(500, err.message);
port.drain(callback);
console.log('message written');
});
});
And I was wondering how I could run this code once the port is ready to receive commands so that i could open/close my lock.
For example, I feel that because the command is being sent immediately after its being open, nothing works because it doesn't have time to react.
For instance, at this documentation it talks about using drain https://github.com/EmergingTechnologyAdvisors/node-serialport#serialportwritedata-encoding-callback--boolean but I am not sure what to do.
This app is being run through electron, and I can confirm that its able to establish relationship between the port and nodejs since when i run serialport-list I am able to display the port connected, just that sending command is a bit of an issue,

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

Disconnecting serial cable crashes nodejs

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!

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.

subscriber not throwing error if the publisher is down in zeromq

Hi im writting a publisher and subscriber in nodejs using the zeromq below is my code
publisher.js
var zmq = require('zmq')
var publisher = zmq.socket('pub')
publisher.bind('tcp://127.0.0.1:7000', function(err) {
if(err)
console.log(err)
else
console.log("Listening on 7000...")
})
setTimeout(function() {
console.log('sent');
publisher.send("hi")
}, 1000)
process.on('SIGINT', function() {
publisher.close()
console.log('\nClosed')
})
Subscriber.js
var zmq = require('zmq')
var subscriber = zmq.socket('sub')
subscriber.on("message", function(reply) {
console.log('Received message: ', reply.toString());
})
subscriber.connect("tcp://localhost:7000",function(err) {
if (err) {
console.log( 'Error binding socket' );
return;
}
subscriber.close(); // This is fine! The socket is ready!
})
subscriber.subscribe("")
process.on('SIGINT', function() {
subscriber.close()
console.log('\nClosed')
})
When the publisher is down or the subscriber is down im trying to capture the error by writting a call back.But in both the cases the srror doesnt seem to be captured.I am stuck here dont know where am going wrong.Any help will be much appreciated.
ZeroMq does not raise errors when peers disconnect. The physical aspects of connect and disconnect are handled transparently and hidden from the application.
One way to force ZeroMq to raise an error if the peer is unavailable is with ROUTER and router mandatory option set to true. If ROUTER attempts to send to a peer that doesn't physically exist, ROUTER will raise an exception.

Resources