管道hexdump输出到node js程序

pipe hexdump output to node js program

本文关键字:js 程序 node hexdump 输出 管道      更新时间:2023-09-26

我正在使用Yoctoo 3.10的英特尔爱迪生,我在/dev/hidraw0上有一个条形码扫描仪,我想使用当我运行hexdump /dev/hidraw0作为节点js编写的程序的输入时输出的确切行。

node js程序如下:

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
rl.on('line', function(line){
    console.log(line);
})

我试过管道正常:

hexdump /dev/hidraw0 | node program.js

但是我什么也没得到,我认为这与hexdump不写'n的事实有关,所以缓冲区不写它的内容。

我也试过打开/dev/hidraw0作为一个文件,像这样:

var fs = require('fs');
fs.open('/dev/hidraw0', 'r', function(status, fd) {
    if (status) {
        console.log(status.message);
        return;
    }
    var buffer = new Buffer(100);
    fs.read(fd, buffer, 0, 100, 0, function(err, num) {
        console.log(buffer.toString('hex'));
    });
}); 

使用一些十六进制转储,如hexy,但在这种情况下,我得到一些十六进制线,但与hexdump不同,这是我需要的。

仅使用hexdump /dev/hidraw0就可以得到以下结果(当我使用卡片时)

0000000 0000 0020 0000 0000 0000 0000 0000 0000
0000010 0000 0020 0000 0000 0000 001f 0000 0000
0000020 0000 0027 0000 0000 0000 0026 0000 0000
0000030 0000 0025 0000 0000 0000 0000 0000 0000
0000040 0000 0025 0000 0000 0000 0024 0000 0000
0000050 0000 0021 0000 0000 0000 0025 0000 0000
0000060 0000 0028 0000 0000 0000 0000 0000 0000

以下内容适合我:

process.stdin.setEncoding('utf8');
process.stdin.on('readable', function () {
  var chunk = process.stdin.read();
  if (chunk !== null) {
    console.log(chunk);
  }
});
运行:

sudo hexdump /dev/hidraw0 | node this-script.js

示例输出(当我移动无线鼠标时):

0000000 0120 0002 fd00 ffcf 0000 0000 0000 2000
0000010 0201 0000 cffc 00ff 0000 0000 0000 0120
...