1.安装HIDAPI的库

pip install hidapi

2.使用方法例子

# 查找USB HID设备

import hid

for device_dict in hid.enumerate():
    keys = list(device_dict.keys())
    keys.sort()
    for key in keys:
        print("%s : %s" % (key, device_dict[key]))
    print()

运行结果:

interface_number : -1
manufacturer_string : UMDF Virtual hidmini device Manufacturer string
path : b'\\\\?\\hid#lenovovhid#1&632d18&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}'
product_id : 65261
product_string : UMDF Virtual hidmini device Product string
release_number : 257
serial_number : UMDF Virtual hidmini device Serial Number string
usage : 12
usage_page : 1
vendor_id : 48879

interface_number : -1
manufacturer_string : PixArt
path : b'\\\\?\\hid#vid_093a&pid_2510#6&235e23b5&1&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}'
product_id : 9488
product_string : USB Optical Mouse
release_number : 256
serial_number : 
usage : 2
usage_page : 1
vendor_id : 2362

interface_number : -1
manufacturer_string : Vaunix
path : b'\\\\?\\hid#vid_041f&pid_1233#6&10c37be9&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}'
product_id : 4659
product_string : Lab Brick Signal Generator
release_number : 256
serial_number : SN:00000
usage : 10
usage_page : 65294
vendor_id : 1055

# 打开设备,读写,关闭设备

try:
    print("Opening the device")

    h = hid.device()
    h.open(0x41F, 0x1233)  # 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)
    #
    # # write some data to the device
    # print("Write the data")
    # h.write([0, 63, 35, 35] + [0] * 61)
    #
    # # wait
    # time.sleep(0.05)
    #
    # # 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("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")

执行结果:

Opening the device
Manufacturer: Vaunix
Product: Lab Brick Signal Generator
Serial No: SN:00000
Closing the device
Done

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐