简体   繁体   中英

Node.js: Convert Buffer form USB stream to string

I use my Raspberry Pi to read RFID Tags. My RFID Reader is connected by USB, and working like a HID-Keyboard. I use Node.js Version 12.x, an following npm packages:

  • node-hid
  • usb-detection

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. I tried data.toString() , also with parameters ascii, utf8 and 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

Greets

Use node-hid-stream and KeyboardLines.

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

Less hassle.

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