python :Read from a USB HID device

徘徊边缘 提交于 2021-01-27 18:15:09

问题


I have a USB RFID device that appears on /dev/hidraw for my serial devices they appear on /dev/ttyUSB* i used pyserial and it works like charm but for this one i couldn't read from it using cat /dev/hidraw0 need root privileges plus i need to read one line and not keep on listening

I used evdev library but my device doesn't appear at all :

import evdev
devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for device in devices:
    print(device.fn, device.name, device.phys)

So is there a proper way to read from the device programmatically ?


回答1:


By default evdev.list_devices() look only to /dev/input

And you need permissions to work with your device. You can add your user to group which own your device (see $ ls -l /dev/hidraw0 )

Then you need to listen your device in loop

#!/usr/bin/python3
import evdev

devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()]
for device in devices:
  print(device.fn, device.name, device.phys)

device = evdev.InputDevice("/dev/input/event4")
print(device)
for event in device.read_loop(): 
  print(event)


来源:https://stackoverflow.com/questions/38038129/python-read-from-a-usb-hid-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!