相关文章推荐
帅气的领带  ·  【Pyspark ...·  2 周前    · 
近视的橙子  ·  python ...·  2 周前    · 
腼腆的烈马  ·  [Anaconda]——Linux下cond ...·  1 周前    · 
眉毛粗的电梯  ·  python ...·  6 天前    · 
沉着的抽屉  ·  python for循环 ...·  2 天前    · 
淡定的米饭  ·  Python ...·  1 年前    · 
开心的炒饭  ·  Using a State ...·  1 年前    · 
欢快的开水瓶  ·  FileSaver.js 介绍 - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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())
    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

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

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

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

  • What does each printed output list represent? 1 typed character?

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

  • 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

    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/

    Hid Specs

    Press the search button and see the documents. This one is most important: "Device Class Definition for HID 1.11" Also see the "Usage page" documents.

    Every device sends a HID-descriptor that exactly describes every single bit in the report. So to interpret the report, your code can either parse the descriptor (api) or you can manually assign the bytes/bits to a struct (not recommended since it only works for well known devices).

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.