简体   繁体   中英

node.js serialport read data - parsing issues

I am trying to parse information from a serial port using the serialport module for Node.js. I am trying to parse this example packet, using 0a as the delimiter:

133733100a6E0686173650aFE0aEF100a52A48BAA5126EAABA66B45E94994

This is the code I am using:

const SerialPort = require('serialport');

var portName = 'COM8';
const Readline = SerialPort.parsers.Readline;

const port = new SerialPort(portName, {
    baudRate: 9600,
    dataBits: 8,
    parity: 'none',
    stopBits: 1,
    flowControl: false
});

const parser = port.pipe(new Readline({ delimiter: '0a' }));

parser.on('data', function(data) {
    var bits = data;
    console.log(bits);
});

When I run this, I get the output:

13373310
6E068617365
FE
EF10
52A48BAA5126EAABA66B45E94994

I want to then take each of these lines and store them in a separate array for later use. However, if I try to access bits as an array, I get the first column of data, rather than the first row, like so:

console.log(bits);

becomes

console.log(bits[0]);

output:

1
6
F
E
5

how can I access the data as lines instead of columns?

Define an extra array variable to store your value

let bitsArray = []
parser.on('data', function(data) {
  var bits = data;
  // add code here
  bitsArray.push(bits)
  console.log(bits);
});
// now your code has all the bits in the bitsArray

bitsArray.forEach((bit) => {
    console.log(bit) // here is your full bit
})    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM