M1 MBP上的Python试图连接到USB设备-无后端错误:没有可用的后端



我正在尝试使用Python连接到我的USB设备。

最终结果应该是连接到我的血压计,但我已经无法连接到任何设备。

我在这里找到的简单代码如下。我从Apple Menu获得的产品和供应商ID>关于这款Mac>系统信息

import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0x0781, idProduct=0x55a4)
# was it found?
if dev is None:
raise ValueError('Device not found')
# set the active configuration. With no arguments, the first
# configuration will be the active one
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
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('test')

但我总是从dev = usb.core.find(idVendor=0x0781, idProduct=0x55a4)得到NoBackendError: No backend available

对于连接,我在Python环境中安装了pyusb,并在mac上安装了Homebrew libusb。

我不知道如何通过迭代我所有连接的产品和供应商ID来获得连接,甚至不知道如何获得一个简单的列表。

如果pyusb找不到libusb的动态库,则会出现此错误。

使用Homebrew安装libusb是不够的。Homebrew将相关文件放在/opt/homebrew/Cellar/libusb/1.0.24/lib中,并在/opt/homebrew/lib中创建符号链接。但是pyusb不知道这些路径。

您有两个主要选项:

  • /opt/homebrew/lib添加到环境变量DYLD_LIBRARY_PATH中。对于永久设置,请将其添加到~/.zshenv
export DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH"
  • 在主目录中创建一个符号链接。这利用了~/lib是库的默认回退路径这一事实:
ln -s /opt/homebrew/lib ~/lib

最新更新