问题
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