简体   繁体   English

如何使用 node-hid 库将来自 device.on() 方法的数据响应转换为字符串

[英]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.

相关问题 有没有办法降级我的npm版本以匹配node-hid库? - Is there a way to downgrade my npm version to match the node-hid library? Node-hid模块未在Webworker上自注册 - Node-hid Module did not self-register on webworker 如何使用节点js从http(GET)响应中获取巨大的字符串数据而不进行缓冲? - How to get huge string data from http(GET) response using node js without buffering? 如何在html中使用ajax方法从节点服务器获取响应 - how to get response from node server using ajax method in html 如何使用 node.js 从蓝牙设备接收数据 - how to receive data from bluetooth device using node.js 如何使用 Express/Node 从 POST 请求中获取响应数据? - how to get the response data from a POST request using Express/Node? 如何将Ajax数据响应转换为字符串? - How to convert Ajax data response to string? 如何将从 ble 设备获取的字节数据转换为人类可读的字符串? - How to convert byte data which is getting from ble device in human readable string? 如何从 API 获取响应数据并从数据中编码函数以使用 node js JavaScript 获取结果 - How to get the response data from API and code the function from data to get the result using node js JavaScript 将二进制数据转换为节点中的字符串 - Convert binary data to string in node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM