簡體   English   中英

node.js 串行端口讀取數據 - 解析問題

[英]node.js serialport read data - parsing issues

我正在嘗試使用 Node.js 的串行端口模塊從串行端口解析信息。 我正在嘗試解析這個示例數據包,使用 0a 作為分隔符:

133733100a6E0686173650aFE0aEF100a52A48BAA5126EAABA66B45E94994

這是我正在使用的代碼:

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);
});

當我運行它時,我得到輸出:

13373310
6E068617365
FE
EF10
52A48BAA5126EAABA66B45E94994

然后我想把這些行中的每一行都存儲在一個單獨的數組中以備后用。 但是,如果我嘗試將位作為數組訪問,則會得到第一列數據,而不是第一行,如下所示:

console.log(bits);

變成

console.log(bits[0]);

輸出:

1
6
F
E
5

如何以行而不是列的形式訪問數據?

定義一個額外的數組變量來存儲您的值

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
})    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM