简体   繁体   English

python:从USB HID设备读取

[英]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 我有一个USB RFID设备出现在/ dev / hidraw中,而我的串行设备出现在/ dev / ttyUSB *中。我使用了pyserial,它的工作方式像魅力一样,但是对于这个,我无法使用cat /dev/hidraw0需要root权限加上我需要读一行而不继续听

I used evdev library but my device doesn't appear at all : 我使用了evdev库,但是我的设备根本没有出现:

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 ? 那么,是否有适当的方法以编程方式从设备读取内容?

By default evdev.list_devices() look only to /dev/input 默认情况下, evdev.list_devices()仅查找/ 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 ) 您可以将用户添加到拥有设备的组中(请参阅$ 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM