PyUSB没有后端可用,即使在路径中有dll,显式加载

2024-04-24 22:35:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在pyusb中收到一个no-backend错误,尽管我已经在路径中安装了libusb1,并且在前面的代码中加载得很好。我的代码是

import usb.core
import usb.util
import usb.backend.libusb1
import sys

class USBConnection:
def __init__(self, vendor_id, product_id):
    usb.backend.libusb1.get_backend(find_library=lambda x: 'C:\Windows\System32\libusb-1.0.dll')
    self.device = usb.core.find(idVendor=vendor_id, idProduct=product_id)
    if self.device is None:
        sys.exit('Could not find device')
    else:
        print 'Device detected'
    self.device.set_configuration()
    self.configuration = self.device.get_active_configuration()
    self.description = self.configuration[(0, 0)]
    self.end_point = usb.util.find_descriptor(self.description, custome_match=lambda e: usb.util.endpoint_direction
                                              (e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
    assert self.end_point is not None

def write(self, data):
    self.end_point.write(data)

if __name__ == '__main__':
    channel = USBConnection(vendor_id='2A19', product_id='1002')

运行此代码将返回

^{pr2}$

事实上,我的路径中确实有C:\windows\system32,其中包含了llibusb-1.0.dll,并安装了libusb1的最新版本,但是我仍然得到这个错误。尽管事实上

    usb.backend.libusb1.get_backend(find_library=lambda x: 'C:\Windows\System32\libusb-1.0.dll')

不返回错误。更奇怪的是

backend = usb.backend.libusb1.get_backend(find_library=lambda x: 'C:\Windows\System32\libusb-1.0.dll')

在调试器中执行,返回值为None的后端。有什么好办法解决这个问题吗?如果有帮助的话,我用的是Windows7。在


Tags: lambda代码importselfidbackendgetdevice