【发布时间】:2016-09-05 04:55:06
【问题描述】:
我在 Python 中使用 PyUSB,因为我必须监听 USB 端口才能从电子卡中检索数据。目前,我必须通过从连接到 Raspberry-Pi 的小键盘(USB 连接)读取直接输入来训练自己。当然,我不想阅读类型化的字符串,我希望得到例如 ASCII 码。我只是不明白如何从我的 USB 键盘读取输入。
我已经找到了一些 sn-ps:
import usb.core
import usb.util
VENDOR_ID = 0x0922
PRODUCT_ID = 0x8003
# find the USB device
device = usb.core.find(idVendor=VENDOR_ID,
idProduct=PRODUCT_ID)
# use the first/default configuration
device.set_configuration()
# first endpoint
endpoint = device[0][(0,0)][0]
# read a data packet
attempts = 10
data = None
while data is None and attempts > 0:
try:
data = device.read(endpoint.bEndpointAddress,
endpoint.wMaxPacketSize)
except usb.core.USBError as e:
data = None
if e.args == ('Operation timed out',):
attempts -= 1
continue
print data
如果我取消注释导致“设备正忙”异常的以下行“device.set_configuration()”,我要么收到错误 16“设备正忙”,要么什么都没有...(我确实替换了 VENDOR_ID 和 PRODUCT_ID用我的键盘ID)
【问题讨论】:
-
您的设备被操作系统用作输入设备。你不能直接使用它。您没有提供操作系统的任何信息。
-
有没有办法将它与内核临时分离,以便在我的 python 应用程序中使用它?
-
查看USB设备类的
is_kernel_driver_active和detach_kernel_driver方法。 libusb 文档对此进行了一些讨论。