简体   繁体   English

Node.js:将 Buffer 形式的 USB 流转换为字符串

[英]Node.js: Convert Buffer form USB stream to string

I use my Raspberry Pi to read RFID Tags.我使用我的 Raspberry Pi 读取 RFID 标签。 My RFID Reader is connected by USB, and working like a HID-Keyboard.我的 RFID 阅读器通过 USB 连接,像 HID 键盘一样工作。 I use Node.js Version 12.x, an following npm packages:我使用 Node.js 版本 12.x,以下 npm 包:

  • node-hid节点隐藏
  • usb-detection USB检测

My Code is:我的代码是:

/* app.js */
const HID = require('node-hid');
const usbDetect = require('usb-detection');
 
usbDetect.startMonitoring();

console.log("=== START ===")
 
// Detect add/insert
usbDetect.on('add', function(device) { 
    console.log('add0', device); 
    listenToInput(device)
});
 
// Detect remove
usbDetect.on('remove', function(device) { console.log('remove0', device); });


// Observe input Data
 function listenToInput(device){
    console.log("Listen start", device);
    
    // get Connection to RFID Scanner
    var RFIDscanner = new HID.HID(device.vendorId, device.productId);
    RFIDscanner.on("data", function(data) {
        console.log(data, new Date())
        
        //var dataArr = Array.prototype.slice.call(new Uint8Array(data, 0, 8))
        //console.log(dataArr);
        //console.log("string",data.toString());
        
    });
}

the code works pretty good;代码运行良好; I get following output.我得到以下输出。

<Buffer 00 00 27 00 00 00 00 00> 2019-12-25T17:56:35.692Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.700Z
<Buffer 02 00 09 00 00 00 00 00> 2019-12-25T17:56:35.716Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.724Z
<Buffer 00 00 27 00 00 00 00 00> 2019-12-25T17:56:35.732Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.740Z
<Buffer 00 00 27 00 00 00 00 00> 2019-12-25T17:56:35.756Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.764Z
<Buffer 00 00 1e 00 00 00 00 00> 2019-12-25T17:56:35.772Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.780Z
<Buffer 02 00 06 00 00 00 00 00> 2019-12-25T17:56:35.796Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.804Z
<Buffer 00 00 1e 00 00 00 00 00> 2019-12-25T17:56:35.812Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.820Z
<Buffer 00 00 22 00 00 00 00 00> 2019-12-25T17:56:35.836Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.844Z
<Buffer 00 00 25 00 00 00 00 00> 2019-12-25T17:56:35.852Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.860Z
<Buffer 02 00 04 00 00 00 00 00> 2019-12-25T17:56:35.876Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.885Z
<Buffer 00 00 28 00 00 00 00 00> 2019-12-25T17:56:35.892Z
<Buffer 00 00 00 00 00 00 00 00> 2019-12-25T17:56:35.900Z

My Problem is,我的问题是,

how convert the Buffer (-stream?), to String.如何将 Buffer (-stream?) 转换为 String。 I tried data.toString() , also with parameters ascii, utf8 and hex;我试过data.toString() ,还有参数 ascii、utf8 和 hex; but I don't get the string I want.但我没有得到我想要的字符串。 If I print the RFID Tag in a text file (without my node.js script) I get following output: 0F001C158A如果我在文本文件中打印 RFID 标签(没有我的 node.js 脚本),我会得到以下输出: 0F001C158A

Greets问候

Use node-hid-stream and KeyboardLines.使用 node-hid-stream 和 KeyboardLines。

https://github.com/agirorn/node-hid-stream https://github.com/agirorn/node-hid-stream

Less hassle.少麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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