usb.core.USBError:[Errno 19]没有这样的设备(可能已断开连接)

时间:2015-05-13 20:22:57

标签: python python-2.7 libusb pyusb

我在Python中从USB设备检索值的代码如下:

import usb.core
import usb.util

VENDOR_ID = 0x0922
PRODUCT_ID = 0x8005

# 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

当我运行时,这是错误消息:

File "/Library/Python/2.7/site-packages/usb/backend/libusb1.py", line 552, in _check
raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 19] No such device (it may have been disconnected)

为什么会发生这种情况,我该如何解决?谢谢!

1 个答案:

答案 0 :(得分:1)

您应该遵循本教程中的所有必要步骤,尤其是搜索设备的部分。 https://github.com/walac/pyusb/blob/master/docs/tutorial.rst

和:您确定您的设备与定义的供应商和产品ID匹配吗? 您可以使用lsusb来识别USB设备并从那里获取ID。 http://www.linuxnix.com/2013/05/find-usb-device-details-in-linuxunix-using-lsusb-command.html