Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
after my first question (
NodeJS and Serialport – Read RFID Card
) I figured out a way to get the correct data.
But now my result is the following:
data received:
data received: 0
data received: >
data received: 0
data received: 0
data received: 6
data received: 3
data received: 4
data received: :
data received: ;
data received: 4
data received:
my desired output is the following
0>00634:;4
how can I achieve this using this code:
var SerialPort = require('serialport');
const Readline = require('@serialport/parser-readline');
var sp = new SerialPort('/dev/tty.usbserial-0001', {
parser: new Readline(' '),
baudRate: 4800,
parity: 'none',
newline: ' ',
sp.on('open', function () {
console.log('open');
sp.on('data', function (data) {
console.log('data received: ' + data.toString());
I'm not 100% sure what exactly you mean by archive
, however, incase you want to filter out the empty data you could just use the data.toString()
output in an if
statement.
Example:
sp.on('data', function (data) => {
const stringData = data.toString();
if (stringData) {
console.log(`data received: ${stringData}`);
And if you would want to write this data to a file you could simply add:
fs.appendFileSync('output.bin', stringData);
In the data callback, you can decide yourself if you want to append the blank data or not by either putting it in or out of the if
statement.
Based on some updated information the following would be the answer. To seperate the information, the data event should be the following:
// Putting the package in a higher scope to make sure that the contents stay after the data event
let package = "";
sp.on('data', function (data) => {
const stringData = data.toString();
if (stringData) {
package += stringData;
} else {
// All the string processing goes here, use package for the result
package = ""; // Resetting the package to collect the next package
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.