我的电脑时,PyUsb无法识别我的USB设备

时间:2016-03-14 08:03:15

标签: python python-2.7 python-module libusb pyusb

我正在尝试在我的PC和PIC18F4550之间进行通信,但程序没有检测到它,而计算机正在Device Manager中显示它。

import usb.core

dev = usb.core.find(idVendor = 0x04D8, idProduct = 0xFEAA)

检查USB设备的功能:

def find(find_all = False, backend = None, custom_match = None, **args):
    def device_iter(k, v):
        for dev in backend.enumerate_devices():
            d = Device(dev, backend)
            if  _interop._reduce(lambda a, b: a and b,map(operator.eq,v,map(lambda i:getattr(d,i),k)),True)and (custom_match is None or custom_match(d)):
                yield d
        if backend is None:
            import usb.backend.libusb1 as libusb1
            import usb.backend.libusb0 as libusb0
            import usb.backend.openusb as openusb

            for m in (libusb1, openusb, libusb0):
                backend = m.get_backend()
                if backend is not None:
                    _logger.info('find(): using backend "%s"', m.__name__)
                       break
                 else:
                    raise ValueError('No backend available')

        k, v = args.keys(), args.values()

        if find_all:
            return device_iter(k, v)
        else:
            try:
                return _interop._next(device_iter(k, v))
            except StopIteration:
                return None

运行代码时遇到的错误。

Traceback (most recent call last):
 File "C:\modules\motor.py", line 29, in <module>
   dev = usb.core.find(idVendor=0x04D8,idProduct=0xFEAA)
 File "C:\Python27\lib\site-packages\usb\core.py", line 1199, in find
   raise ValueError('No backend available')
 ValueError: No backend available

在它用于正常执行之前,但过去几天它显示此错误。我不明白突然发生了什么。使用PyUSB模块有什么问题吗?

我看到他们中的一些人在使用USB通信时遇到了同样的问题。

我已经解决了这个问题。解决方案是PyUSB模块将搜索libusb0.dll和libusb-1.0.dll文件,这些文件是与我们需要包含在PATH环境变量中的USB设备进行通信的后端。

1 个答案:

答案 0 :(得分:1)

每当我们使用PyUSB模块与PC进行USB通信时,PyUSB模块将检查 PATH environment variable 中的libusb0.dll和libusb-1.0.dll文件(作为后端)在 C:\windows\System32 位置,然后与USB设备建立通信。由于我使用libusb-win32-wizard来创建设备驱动程序,因此它使用libusb0.dll。可以使用以下DEBUG程序找到执行过程:

import os
os.environ['PYUSB_DEBUG'] = 'debug'
import usb.core
print list(usb.core.find(find_all=True))

当我在 Shell 中执行上述程序时,我得到的输出是:

2016-03-26 11:41:44,280 ERROR:usb.libloader:'Libusb 1' could not be found
2016-03-26 11:41:44,280 ERROR:usb.backend.libusb1:Error loading libusb 1.0 backend
2016-03-26 11:41:44,280 ERROR:usb.libloader:'OpenUSB library' could not be found
2016-03-26 11:41:44,280 ERROR:usb.backend.openusb:Error loading OpenUSB backend
2016-03-26 11:41:44,280 INFO:usb.core:find(): using backend "usb.backend.libusb0"
2016-03-26 11:41:44,280 DEBUG:usb.backend.libusb0:_LibUSB.enumerate_devices()
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E530>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E5D0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E6C0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E7B0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E8A0>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200E990>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200EA80>)
2016-03-26 11:41:44,296 DEBUG:usb.backend.libusb0:_LibUSB.get_device_descriptor(<usb.backend.libusb0._usb_device object at 0x0200EB70>)
[<DEVICE ID 046d:c05a on Bus 000 Address 001>, <DEVICE ID 046d:c31d on Bus 000 Address 002>, <DEVICE ID 046d:c31d on Bus 000 Address 003>, <DEVICE ID 046d:c31d on Bus 000 Address 004>, <DEVICE ID 04d8:feaa on Bus 000 Address 005>, <DEVICE ID 046d:082b on Bus 000 Address 006>, <DEVICE ID 046d:082b on Bus 000 Address 007>, <DEVICE ID 046d:082b on Bus 000 Address 008>]

所以在这里,因为我在find_all=True函数中将参数作为usb.core.find()给出,它返回连接到PC的每个设备ID。同样在前4行中它会出错,因为我们使用了libusb0.dll的lib-usb-win32-wizard,因此在第5行它给了INFO:usb.core:find(): using backend "usb.backend.libusb0",这意味着它使用libusb0.dll与USB设备进行通信。 / p>