在pyusb中调用后端模块时出错。“AttributeError: 'module'对象没有'backend'属性”
我最近为这个项目安装了pyusb,目的是想给一个USB LED留言板写入数据,但遇到了这个错误:
AttributeError: 'module' object has no attribute 'backend'
我不知道为什么会这样,我查看了pyusb模块的文件,里面确实有一个叫“backend”的文件夹,里面也有正确的文件。
这是我所有的代码:
import usb.core
import usb.util
import sys
backend = usb.backend.libusb01.get_backend(find_library=lambda C: "Users\absolute\Desktop\libusb-win32-bin-1.2.6.0\lib\msvc_x64")
#LED Display Message device identify
MessageDevice = usb.core.find(idVendor=0x1D34, idProduct=0x0013, backend=backend)
if MessageDevice is None:
raise ValueError('LED Message Display Device could not be found.')
MessageDevice.set_configuration()
# get an endpoint instance
cfg = MessageDevice.get_active_configuration()
interface_number = cfg[(0,0)].bInterfaceNumber
print interface_number
alternate_settting = usb.control.get_interface(interface_number)
intf = usb.util.find_descriptor(
cfg, bInterfaceNumber = interface_number,
bAlternateSetting = alternate_setting
)
ep = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT
)
assert ep is not None
# write the data
ep.write('\x00\x06\xFE\xBA\xAF\xFF\xFF\xFF')
需要关注的代码:
backend = usb.backend.libusb01.get_backend(find_library=lambda C: "Users\absolute\Desktop\libusb-win32-bin-1.2.6.0\lib\msvc_x64")
我还注意到在其他人的代码中根本没有backend这个部分。但是当我尝试把代码中的backend部分去掉时,显示:
MessageDevice = usb.core.find(idVendor=0x1D34, idProduct=0x0013)
File "C:\Python27\lib\site-packages\usb\core.py", line 846, in find
raise ValueError('No backend available')
ValueError: No backend available
一些额外的信息:
- Windows 8 64位
- Python 2.7
- pyusb-1.0.0a2
1 个回答
6
我知道这个问题已经有4个月了,但如果这能帮到你,我觉得你可能缺少一个导入语句:
import usb.backend.libusb1
更多细节可以查看这个链接:https://github.com/walac/pyusb/blob/master/docs/tutorial.rst#specifying-libraries-by-hand。