I'm having quite a weird problem with a node.js app I made.
The app is designed to accept a request from a web application. A page has a button which says "get RFID". Using this button, a signal gets sent to my node.js server (a Raspberry Pi). The raspberry pi opens a serial connection with the RFID reader in it, and reads the tag. After tag reading, the Raspberry Pi sents back an RFID tag.
This works perfectly well 3 times. (Press button, get tag, refresh site, press button, get tag, etcetera). However, after the 4th try, the node.js server instantly shuts down, passing the last two letters of the tag.
The output during the crash; shows a third scan of a tag that is succesfull, then the 4th read of a tag, that causes the app to crash.
user connected
open
debug - websocket writing 5:::{"name":"pong","args":["0F00DB3BF4\r"]}
info - booting client
info - transport end by forced client disconnection
debug - websocket writing 0::
info - transport end (booted)
debug - set close timeout for client 4G-HA1VgwTR54eNWaj3w
debug - cleared close timeout for client 4G-HA1VgwTR54eNWaj3w
debug - cleared heartbeat interval for client 4G-HA1VgwTR54eNWaj3w
debug - discarding transport
debug - client authorized
info - handshake authorized KSrkMxYYBNkXJO3Vaj3x
debug - setting request GET /socket.io/1/websocket/KSrkMxYYBNkXJO3Vaj3x
debug - set heartbeat interval for client KSrkMxYYBNkXJO3Vaj3x
debug - client authorized for
debug - websocket writing 1::
user connected
open
debug - websocket writing 5:::{"name":"pong","args":["F4\r"]}
info - booting client
info - transport end by forced client disconnection
debug - websocket writing 0::
info - transport end (booted)
debug - set close timeout for client KSrkMxYYBNkXJO3Vaj3x
debug - cleared close timeout for client KSrkMxYYBNkXJO3Vaj3x
debug - cleared heartbeat interval for client KSrkMxYYBNkXJO3Vaj3x
debug - discarding transport
Aborted
The code is the following;
GNU nano 2.2.6 File: nocoin.js
// Socket.io server details
var io = require('socket.io').listen(3000);
// Serialport plugin declared and made a serialport variable
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
// Variable containing technical USB port details
var serialPort = new SerialPort("/dev/ttyUSB0", {baudrate: 2400, parser: serialport.parsers.readline("\n")}, false); // this is the openImmediately flag [default is true]
io.sockets.on('connection', function (socket) {
console.log('user connected');
socket.on('ping', function (data) {
serialPort.open(function () {
// Open notification
console.log('open');
//Start listening
serialPort.on('data', function(data) {
// If content is empty, filter out
if (data.trim() !== '') {
socket.emit('pong', data);
socket.disconnect();
//Execute function again, get tag, handle tag and end process
serialPort.flush(function(err) {
setTimeout(function() {
serialPort.close(function(err) { });
}, 10);
});
}
});
});
});
});
What could I be possibly be doing wrong here? Does node.JS require flushing of buffers? Why does it always instantly crash after the 4th try?