我一直在尝试使用 Python 制作一个程序,该程序将命令发送到 DYMO labelmanager PnP usb 设备。我尝试安装 pyUSB 并尝试了 pyUSB 教程中提供的代码,以了解 USB 通信的工作原理,但它不起作用。pyUSB教程中的代码:
(我已更改 idVendor 和 idProduct 以应对我的设备。它找到了设备但写入失败)
import usb.core
import usb.util
# find our device
dev = usb.core.find(idVendor=0x0922, idProduct=0x1001)
# 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()
interface_number = cfg[(0,0)].bInterfaceNumber
alternate_setting = 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('test')
它给出了一个错误:
Traceback (most recent call last):
File "C:\Python27\proc\labelprinttest.py", line 18, in <module>
alternate_setting = usb.control.get_interface(interface_number)
TypeError: get_interface() takes exactly 2 arguments (1 given)
问题出在哪里?
(当然,读到该函数需要 2 个参数,并且只给出了 1 个,但我试图调查,但我不知道另一个需要的参数是什么)