我一直在尝试用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个参数,但我尝试过调查,但我不知道另一个需要的参数是什么)
发布于 2013-06-19 17:51:25
get_interface()
的定义如下:
def get_interface(dev, bInterfaceNumber):
r"""Get the current alternate setting of the interface.
dev is the Device object to which the request will be
sent to.
"""
因此,尝试使用usb.control.get_interface(dev, interface_number)
调用它
https://stackoverflow.com/questions/17187684
复制相似问题