admin管理员组文章数量:1334153
Currently having a problem in a app.
My app is designed to open an RFID reader on a Raspberry PI. It proceeds to read the ining RFID tags.
The code is as follows;
// 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('opening RFID scanner');
//Start listening
serialPort.on('data', function(data) {
if (data.trim() !== '') {
io.sockets.emit('pong', data);
socket.disconnect();
}
});
});
});
});
I open the app, and it works just fine. It constantly can read tags of a card. However; when I use a different card, Node.JS seems to use some kind of buffer.
An example;
I scan card A, return tag AAA. I scan card A, return tag AAA. I scan card B, return tag AAA. I scan card B, return tag AAA. I scan card B, return tag BBB (The one that was supposed to be returned).
This always seems to happen when changing tags.
Is there some kind of buffer in Node.JS, that stores a buffer of data that wasnt transmitted yet?
I'm aware of the .drain (callback), but how could I implement this in a proper way?
Currently having a problem in a app.
My app is designed to open an RFID reader on a Raspberry PI. It proceeds to read the ining RFID tags.
The code is as follows;
// 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('opening RFID scanner');
//Start listening
serialPort.on('data', function(data) {
if (data.trim() !== '') {
io.sockets.emit('pong', data);
socket.disconnect();
}
});
});
});
});
I open the app, and it works just fine. It constantly can read tags of a card. However; when I use a different card, Node.JS seems to use some kind of buffer.
An example;
I scan card A, return tag AAA. I scan card A, return tag AAA. I scan card B, return tag AAA. I scan card B, return tag AAA. I scan card B, return tag BBB (The one that was supposed to be returned).
This always seems to happen when changing tags.
Is there some kind of buffer in Node.JS, that stores a buffer of data that wasnt transmitted yet?
I'm aware of the .drain (callback), but how could I implement this in a proper way?
Share Improve this question asked Jun 3, 2014 at 22:56 MichaelPMichaelP 1811 gold badge5 silver badges23 bronze badges1 Answer
Reset to default 6I don't pletely understand the node-serialport library, but I did have a similar problem when receiving data.
Each time my data value changed, I would always received the previous value once before then receiving the correct value on the next 'data' chunk.
I solved it by using the "flush" method in the node-serialport API.
A crude way of doing it would be:
sp.flush(function(err,results){});
Try testing this right before you disconnect the socket connection.
if (data.trim() !== '') {
io.sockets.emit('pong', data);
sp.flush(function(err,results){});
socket.disconnect();
}
本文标签: javascriptFlushing Buffer data in NodeJSStack Overflow
版权声明:本文标题:javascript - Flushing Buffer data in Node.JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742240711a2438814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论