【问题标题】:Problems with pyUSBpyUSB 的问题
【发布时间】:2013-06-19 09:28:42
【问题描述】:

我一直在尝试使用 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 个,但我试图调查,但我不知道另一个需要的参数是什么)

【问题讨论】:

    标签: python usb pyusb


    【解决方案1】:

    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) 调用它

    【讨论】:

    • 谢谢,但没用。现在它给出了一个错误:文件“C:\Python27\lib\site-packages\usb\backend\libusb01.py”,第 384 行,在 _check raise USBError(errmsg, ret) USBError: [Errno None] libusb0-dll: err [control_msg] 发送控制消息失败,win 错误:不支持请求。
    猜你喜欢
    • 2011-07-06
    • 2014-11-03
    • 2013-02-11
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2015-06-03
    相关资源
    最近更新 更多