首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pyUSB的问题

pyUSB的问题
EN

Stack Overflow用户
提问于 2013-06-19 17:28:42
回答 1查看 2.6K关注 0票数 0

我一直在尝试用Python编写一个程序,它可以向DYMO labelmanager PnP usb设备发送命令。我试着安装pyUSB,并尝试了pyUSB教程中提供的代码,试图弄清楚一点USB通信是如何工作的,但它不起作用。来自pyUSB教程的代码:

(我已经更改了idVendor和idProduct以适应我的设备。它会找到设备,但写入失败)

代码语言:javascript
运行
复制
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')

它会给出一个错误:

代码语言:javascript
运行
复制
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个参数,但我尝试过调查,但我不知道另一个需要的参数是什么)

EN

回答 1

Stack Overflow用户

发布于 2013-06-19 17:51:25

get_interface()的定义如下:

代码语言:javascript
运行
复制
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)调用它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17187684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档