[英]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:我必须在 NodeJS 中实现一个 API 连接到 HID 接口扫描仪以读取条形码,我正在使用 NodeJS 调用 node-hid 的库,该库允许连接到接口 USB,代码是:
//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文档说 toString() 就足够了,但结果不是预期的:00 7702111228221
The data returned and print without.toString is: without.toString 返回和打印的数据是:
<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 !
谢谢 !
The codes in the buffer are HID reports from a typical USB keyboard.缓冲区中的代码是来自典型 USB 键盘的 HID 报告。 They are described in the Universal Serial Bus HID Usage Tables document in Chapter 10 "Keyboard/Keypad Page (0x07)".
它们在第 10 章“键盘/键盘页面 (0x07)”中的通用串行总线 HID 使用表文档中进行了描述。 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.
每个报告的格式取决于报告描述符,但几乎总是:1 字节修饰符(ctrl、alt 等)、1 字节保留、6 字节表示最多同时按下 6 个键。 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. ...等等。
You can use mag-stripe npm package and use it's parse_card method which get you data in json format.您可以使用磁条 npm package 并使用它的 parse_card 方法获取 json 格式的数据。
const parser = require('mag-stripe');
device.on('data', function(data) {
let result = parser.parse_card(data.toString());
console.log("data ===> ",result)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.