Device :: HID读取条形码扫描器将二进制转换为Ascii

时间:2018-06-11 12:03:21

标签: perl barcode-scanner hid

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

条形码类型是代码39。

A1234 => [Barcode SCANNRT] => [perl] => binary => ?? A1234 ???

document.querySelector('input[list]').addEventListener('input', function(e) {
  var input = e.target,
  list = input.getAttribute('list'),
  options = document.querySelectorAll('#' + list + ' option'),
  hiddenInput = document.getElementById(input.getAttribute('id') + '-hidden'),
  label = input.value;
  hiddenInput.value = label;

  for(var i = 0; i < options.length; i++) {
    var option = options[i];

    if(option.innerText.toUpperCase() === label.toUpperCase()) {
      // It does not find any match although same value is choosen
      hiddenInput.value = option.getAttribute('data-value');
      break;
    }
  }    
});

输入&#34; A1234&#34;二进制输出。如何转换为sting&#34; A1234&#34;。

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,
    );


}

我更改代码以获取每个包的十六进制。

  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 : ................

输出是十六进制的。我将代码更改为输出十六进制。我收到12包。 如何转换为字符串&#39; A1234&#39; ?

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

1 个答案:

答案 0 :(得分:1)

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

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

如果键是 0,则忽略它。

所以这个输入:

[HttpPost("select/manager/{projectId}")]
public async Task<ActionResult<string>> SetProjectManager([FromBody] string name, string projectId)
{
    var (isValid, username, password) = spManager.GetCredentials(HttpContext.Request.Headers["x-token"]);

    if (!isValid || string.IsNullOrEmpty(name))
    {
        return BadRequest("Bad Key!");
    }

    var result = await spManager.SetProjectManager(username, password, name, projectId);

    if (result.Success == true)
    {
        return Ok(new SubmissionResponse { Success = true, ErrorCode = null });
    }
    else
    {
        return NotFound(new SubmissionResponse { Success = false, ErrorCode = "Not Found!" });
    }
}

变成:

  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 : ................

...等等。