小编Gui*_*eur的帖子

如何使用Python获取原始的USB键盘数据?

我正在Python中使用PyUSB,因为我将不得不监听USB端口以从电子卡中检索数据。目前,我必须通过读取连接到Raspberry-Pi的小键盘(USB连接)的直接输入来训练自己。当然,我不想读取键入的字符串,例如,我希望获得ASCII码。我只是不知道如何从USB键盘读取输入。

我已经找到了一些片段:

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
Run Code Online (Sandbox Code Playgroud)

如果我取消注释以下行“ device.set_configuration()”,这会导致“设备正忙”异常,那么我会收到错误16“设备正忙”或根本不显示任何错误...(我确实用键盘的VENDOR_ID和PRODUCT_ID替换了ids)

python pyusb

4
推荐指数
1
解决办法
8616
查看次数

标签 统计

python ×1

pyusb ×1