Device::HID read barcode scanner convert binary to

2019-07-23 15:06发布

问题:

I try to read barcode form scanner. I had got data from scanner and the format is binary. How do I conert binary to Ascii string?

The barcode type is code 39.

A1234 => [Barcode SCANNRT] => [perl] => binary => ?? 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,
    );


}

The input "A1234" binary output. How to convert to 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 : ................

I change code to get hexadecimal for each package.

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";
}

The out is hexadecimal. I change code to output hexadecimal. I received 12 package. How to convert to string '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