【问题标题】:Device::HID read barcode scanner convert binary to AsciiDevice::HID 读取条码扫描器将二进制转换为 Ascii
【发布时间】:2018-11-20 16:16:06
【问题描述】:

我尝试读取条形码表单扫描仪。我从扫描仪得到数据,格式是二进制的。如何将二进制转换为 Ascii 字符串?

条码类型为code 39。

A1234 => [条形码扫描] => [perl] => 二进制 => ?? A1234 ???

use Device::HID;
use Data::Hexdumper qw(hexdump);

my $dev = Device::HID->new(vendor => 0x04b4, product => 0x0100) or die "No such device !\n";
$dev->timeout = 0.1; # seconds (=100 ms)
my $buf;
my $len=128;

while(defined(my $in = $dev->read_data($buf, $len))){

    if ($in == 0) {
        next;
    }

    print hexdump(
        data           => $buf, # what to dump
        suppress_warnings => false,
        space_as_space=> true,
    );


}

输入“A1234”二进制输出。如何转换为 sting "A1234"。

  0x0000 : 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 1E 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 1F 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 : .. .............
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 21 00 00 00 00 00 00 00 00 00 00 00 00 00 : ..!.............
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 28 00 00 00 00 00 00 00 00 00 00 00 00 00 : ..(.............
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................

我将代码更改为每个包的十六进制。

use Device::HID;
use Data::Hexdumper qw(hexdump);
my $dev = Device::HID->new(vendor => 0x04b4, product => 0x0100) or die "No such device !\n";
$dev->timeout = 0.1; # seconds (=100 ms)
my $buf;
my $len=128;
my $i=0;

while(defined(my $in = $dev->read_data($buf, $len))){
    if ($in == 0) {
        next;
    }
    $i++;
    my $hex = unpack(  'H*', $buf );
    print sprintf("%02d",$i)." => $hex\n";
}

out 是十六进制的。我将代码更改为输出十六进制。我收到了 12 个包裹。 如何转换为字符串'A1234'?

01 => 0200040000000000
02 => 0000000000000000
03 => 00001e0000000000
04 => 0000000000000000
05 => 00001f0000000000
06 => 0000000000000000
07 => 0000200000000000
08 => 0000000000000000
09 => 0000210000000000
10 => 0000000000000000
11 => 0000280000000000
12 => 0000000000000000

【问题讨论】:

  • 我认为如果不知道二进制数据的格式是什么,这是不可能回答的。
  • 感谢您的信息!我的条形码类型是“代码 93”,我将研究代码格式。
  • 我认为您可能需要先调查硬件
  • 我将代码更改为每个包的十六进制。我发现了一些行为。 'A1234' => 02000400 00001e00 00001f00 00002000 00002100 。下一步怎么走??
  • 这是一个相当典型的 USB 键盘数据流。简而言之,02 是键盘修饰符代码,表示按下 Shift 键,04 表示按下“a”键……给出大写字母 A。同样,1e 表示按下 1 键。全零数据包意味着当前没有按键被按下。

标签: perl barcode-scanner hid


【解决方案1】:

分成 8 个字节的块(对于您的设备,这取决于设备描述符)。第 0 列是控制键(第 2 位表示按下了 shift 键),第 2 列是 USB HID 使用表文档第 10 节(第 53 页)的使用代码:

https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf

如果键是 0,则忽略它。

所以这个输入:

  0x0000 : 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 1E 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................
  0x0000 : 00 00 1F 00 00 00 00 00 00 00 00 00 00 00 00 00 : ................

变成:

02 04 (shift + a) = "A"
00 00 (ignore)
00 1E (1)         = "1"
00 00 (ignore)
00 00 (ignore)
00 00 (ignore)
00 1F (2)         = "2"
00 00 (ignore)

...等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2014-08-16
    • 2012-07-07
    • 2015-03-13
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多