Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python :Read from a USB HID device

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 ?

like image 559
Safwen Daghsen Avatar asked Dec 18 '25 05:12

Safwen Daghsen


1 Answers

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)
like image 74
5n00py Avatar answered Dec 20 '25 22:12

5n00py