Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting HIDAPI python output

Tags:

python

hidapi

I would like to read a string from a USB HID scanner with Python on OS X. The example below is my starting point and I have been able to tailor the code to my scanner: I have been able to execute the command: h.open() successfully and printout the manufacturer and product strings. The scan codes were verified with EVDEV with the scanner.

The challenge is interpreting the data returned and mapping it back to an ascii string.

This post provides python HIDAPI example code:

 from __future__ import print_function

import hid
import time

print("Opening the device")

h = hid.device()
h.open(1118, 2048) # A Microsoft wireless combo keyboard & mouse

print("Manufacturer: %s" % h.get_manufacturer_string())
print("Product: %s" % h.get_product_string())
print("Serial No: %s" % h.get_serial_number_string())

try:
    while True:
        d = h.read(64)
        if d:
            print('read: "{}"'.format(d))
finally:
    print("Closing the device")
    h.close()

$ sudo python try.pyoutput:

Opening the device
Manufacturer: Microsoft
Product: Microsoft® Nano Transceiver v2.0
Serial No: None
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"

--8<-- snip lots of repeated lines --8<--

read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 21, 0, 0, 0, 0, 0]"
read: "[0, 0, 0, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 22, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 0, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 0, 0, 0, 0]"
read: "[0, 0, 4, 9, 7, 0, 0, 0]"
read: "[0, 0, 4, 9, 7, 0, 0, 0]"
read: "[0, 0, 7, 0, 0, 0, 0, 0]"
^Closing the device
Traceback (most recent call last):
  File "try.py", line 17, in <module>
    d = h.read(64)
KeyboardInterrupt

Questions

I was unable to find good examples (like those found with EVDEV). Any link to the equivalent would be incredibly helpful. It is a challenge to interpret the output without good documentation. h.read() returns a list

  1. Why is 64 selected for h.read()?

    d = h.read(64)

When 64 is replaced with a number from 1,2,3...8, the number of elements in the list is the same. 9 or more results a list of 8 elements.

  1. Why is the variable 'd' an output list of 8 elements? (8 is not specified anywhere)

    print('read: "{}"'.format(d))

  2. What does each printed output list represent? 1 typed character?

  3. What does each column in the output list represent \ encode?

  4. Is there a published tabled that maps the numbers to the keyboard?

I am looking forward to responses: if you have experience using the HIDAPI (especially with Python) please state this in your answer. Enter the double-bonus round for HID scanner experience

like image 374
gatorback Avatar asked Sep 18 '25 23:09

gatorback


1 Answers

HIDAPI (which is what Python is using) does not retrieve descriptors, which is what you need to parse the incoming data. What you need is a way to dump and decode these descriptors: https://github.com/todbot/mac-hid-dump https://eleccelerator.com/usbdescreqparser/

like image 76
Paul McClernan Avatar answered Sep 20 '25 13:09

Paul McClernan