【问题标题】:node.js USB (hid) Barcode Scanner read Buffernode.js USB(隐藏)条形码扫描仪读取缓冲区
【发布时间】:2017-08-04 00:27:25
【问题描述】:

我正在使用 node.js 从条形码扫描仪读取数据。这就是我的代码:

var HID = require('node-hid');
var usb = require('usb');

// Honeywell Scanner
var vid = 0xc2e;
var pid = 0xbe1;

var d = new HID.HID(vid, pid);

d.on("data", function (data) {

console.log(data);

});
d.on("error", function (error) {
console.log(error);
d.close();
});

我的问题是,我得到一个看起来像 的缓冲区。扫描条码(例如 id 为 12 的条码)后,控制台会返回类似的内容

<Buffer 00 00 53 00 00 00 00 00>
<Buffer 00 00 00 00 00 00 00 00>
<Buffer 00 00 53 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 1f 00 00 00 00 00>

如何将此缓冲区输出转换为可读字符串?在这种情况下,它将是 12。

感谢您的帮助!

【问题讨论】:

    标签: node.js usb buffer barcode-scanner hid


    【解决方案1】:

    我认为你想要做的是解码你的 data 缓冲区。

    要解码缓冲区,只需使用内置的.toString() 方法,传入要解码的字符编码即可:

    data.toString('hex'); //<-- Decodes to hexadecimal
    data.toString('base64'); //<-- Decodes to base64
    

    如果您不向toString 传递任何内容,utf8 将是默认值。


    编辑

    如果您想知道 Node 当前支持哪些字符编码(hexbase64utf8 除外) ,请访问official docs.

    【讨论】:

    • @JoseLopezGarcia 很棒的提示!应该被标记为答案。快速的问题,当控制台记录 data.toString() 时,我得到随机方块以及条形码的值,例如 1234 知道这是什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多