# hid-python **Repository Path**: weiqifa/hid-python ## Basic Information - **Project Name**: hid-python - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-12-13 - **Last Updated**: 2024-12-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 这两天在测试hid到功能,这个时候就感觉python的好处的,如果用C++写一个hid的上位机软件,一定是需要花费很多心思的,但是如果用python来搞一个hid的测试代码,那是非常简单的。 这里安装还是有点奇葩,如果是直接用pip安装的时候,我看了网上的一些网友,也是遇到安装后不能直接使用的,所以还是比较推荐源码安装,而且源码也比较简单,下载安装也比较快。首先要安装 ``` pip uninstall hid pip install hidapi pip install Cython git clone https://github.com/trezor/cython-hidapi.git cd cython-hidapi git submodule update --init python setup.py build python setup.py install pip install -e . ``` 然后,我们要的代码是在一个try.py 的文件里面 ```python from __future__ import print_function import hid import time # enumerate USB devices for d in hid.enumerate():     keys = list(d.keys())     keys.sort()     for key in keys:         print("%s : %s" % (key, d[key]))     print() # try opening a device, then perform write and read h = hid.device() try:     print("Opening the device")     h.open(0x2a5f, 0x2005)  # TREZOR VendorID/ProductID     print("Manufacturer: %s" % h.get_manufacturer_string())     print("Product: %s" % h.get_product_string())     print("Serial No: %s" % h.get_serial_number_string())     # enable non-blocking mode     h.set_nonblocking(1)     # mute     print("Write the data")     h.write([2, 3, 0, 0] + [0] * 61)     # wait     time.sleep(1)     # unmute     print("Write the data")     h.write([2, 1, 0, 0] + [0] * 61)     # wait     time.sleep(1)     # read back the answer     print("Read the data")     while True:         d = h.read(64)         if d:             print(d)         else:             break     print("Closing the device")     h.close() except IOError as ex:     print(ex)     print("hid error:")     print(h.error())     print("")     print("You probably don't have the hard-coded device.")     print("Update the h.open() line in this script with the one")     print("from the enumeration list output above and try again.") print("Done") ``` 我这个代码比较简单,通过VID,PID打开 HID后,然后写一个mute指令,之后在写一个unmute指令。 实际效果也是没问题的 # help https://www.digi.com/resources/documentation/digidocs/90002386/os/python-hid-module-c.htm?TocPath=Applications%7C_____5 Use the Human Interface Device (HID) module The Python hid module provides a programmatic access to a USB Human Interface Device (HID) from within a Python script. For example, to determine information about a USB-connected keyboard: Select a device in Remote Manager that is configured to allow shell access to the admin user, and click Actions > Open Console. Alternatively, log into the LR54 local command line as a user with shell access. Depending on your device configuration, you may be presented with an Access selection menu. Type shell to access the device shell. At the shell prompt, use the python command with no parameters to enter an interactive Python session: # python Python 3.10.1 (main, Mar 30 2023, 23:47:13) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> Import the hid module: >>> import hid >>> Use the enumerate() function to return information about the keyboard: >>> hid.enumerate() This returns the following: [{'path': b'/dev/hidraw0', 'vendor_id': 1008, 'product_id': 36, 'serial_number': '', 'release_number': 768, 'manufacturer_string': 'CHICONY', 'product_string': 'Basic USB Keyboard', 'usage_page': 18432, 'usage': 17481, 'interface_number': 0}] Use the vender_id and product_id to return specific information about the keyboard, or to read input from the keyboard: >>> hid.Device(1008,36).product This returns information about the keyboard: 'Basic USB Keyboard' To read input from the keyboard: >>> hid.Device(1008,36).read(64) Which returns: b'\x00\x00,\x00\x00\x00\x00\x00' Use Ctrl-D to exit the Python session. You can also exit the session using exit() or quit(). Help for the hid module Get help for the hid module: Select a device in Remote Manager that is configured to allow shell access to the admin user, and click Actions > Open Console. Alternatively, log into the LR54 local command line as a user with shell access. Depending on your device configuration, you may be presented with an Access selection menu. Type shell to access the device shell. At the shell prompt, use the python command with no parameters to enter an interactive Python session: # python Python 3.10.1 (main, Mar 30 2023, 23:47:13) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> Import the hid module: >>> import hid >>> Use the help command with hid: >>> help(hid) Help on package hid: NAME hid PACKAGE CONTENTS CLASSES _ctypes.Structure(_ctypes._CData) DeviceInfo builtins.Exception(builtins.BaseException) HIDException builtins.object Device ... >>> Use Ctrl-D to exit the Python session. You can also exit the session using exit() or quit().