Skip to content

Instantly share code, notes, and snippets.

@tafaust
Created March 11, 2018 17:42
Show Gist options
  • Save tafaust/db0e29143c8164216a8a59c3ca6457b8 to your computer and use it in GitHub Desktop.
Save tafaust/db0e29143c8164216a8a59c3ca6457b8 to your computer and use it in GitHub Desktop.
Debug hidraw through hipdapi in linux python 3.6
#!/usr/bin/env python
"""Debug hidapi python library and hid devices.
You may use this utility script for debugging purposes to check if this library does work as
intended, thus detecting all plugged hid devices.
This files does also provide you with the vendor_id and product_id required by the udev rule.
"""
import hidapi as hid
import time
hid.hid_init()
vendor_id = '0x1234'
product_id = '0xed02'
device = None
try:
device = hid.hid_enumerate(vendor_id=int(vendor_id, 16), product_id=int(product_id, 16))[0]
except IndexError:
for dev in hid.hid_enumerate():
print(dev.description())
exit(1)
try:
assert device
except AssertionError as _err:
print('Device with vendorId={} and productId={} not found.'.format(vendor_id, product_id))
exit(2)
hid_handle = hid.hid_open_path(path=device.path)
hid.hid_set_nonblocking(hid_handle, nonblock=True)
print('Reading data....')
while True:
data = hid.hid_read_timeout(hid_handle, 16, 1000)
print(f'{time.time()} - read data: {data}')
time.sleep(0.001) # 1ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment