Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert to string the data response from device.on() method using node-hid library

I have to implementing an API in NodeJS that connect to HID interface scanner for read a barcode, I'm using a library for NodeJS calling node-hid, that library permits connect to Interface USB, the code is:

//import
var HID = require('node-hid');

//connect to my interface
var device = new HID.HID(int,int)

//print all devices
console.log('DEVICE: ', device )

//My device is: 

{
    vendorId: int,
    productId: int,
    path: 'IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/....',
    serialNumber: '',
    manufacturer: 'Microchip Technology Inc.',
    product: 'Keyboard',
    release: 1,
    interface: 0,
    usagePage: 1,
    usage: 6
  }

to here, all is good.

For read event and view data scan from my device I have to implements this:

device.on('data', function(data) {
 console.log(data.toString())
} )

The response is:

**'

'

,

$

$

'













%**

the documentation says that it is enough with the toString() but the result is not the expected one: 00 7702111228221

The data returned and print without .toString is:

<Buffer 00 00 27 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 27 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 2c 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 24 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 24 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 27 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1e 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1e 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1e 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 25 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1f 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 1e 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>

someone have experience with that. Thanks !

like image 871
EdwinCab Avatar asked May 26 '26 20:05

EdwinCab


2 Answers

The codes in the buffer are HID reports from a typical USB keyboard. They are described in the Universal Serial Bus HID Usage Tables document in Chapter 10 "Keyboard/Keypad Page (0x07)". The format of each report depends on the report descriptor but it is almost always: 1 byte modifiers (ctrl, alt, etc), 1 byte reserved, 6 bytes representing up to 6 simultaneous keys being pressed. A report containing all zeros means "no keys are currently being pressed".

In your example the buffers indicate the following sequence:

27 = '0' key pressed
00 = no keys pressed
27 = '0' key pressed
00 = no keys pressed
2c = Space key pressed
00 = no keys pressed
24 = '7' key pressed
00 = no keys pressed
.
.
.

...and so on.

like image 57
aja Avatar answered May 28 '26 11:05

aja


You can use mag-stripe npm package and use it's parse_card method which get you data in json format.

const parser = require('mag-stripe');
device.on('data', function(data) {
  let result = parser.parse_card(data.toString());
  console.log("data ===> ",result)
})
like image 40
Dhaval Parmar Avatar answered May 28 '26 10:05

Dhaval Parmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!