首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PyUSB从usb设备读取/写入时出现问题(超时)

使用PyUSB从usb设备读取/写入时出现问题(超时)
EN

Stack Overflow用户
提问于 2016-07-29 20:31:04
回答 1查看 12.3K关注 0票数 1

我正在尝试连接到我的Ubuntu 16系统(Python版本2.7.12)的设备。当我尝试写入设备时,我得到一个写入超时(类似于从设备读取)。

根据我所掌握的少量文档,除了标准端点(EP)0之外,HID接口还在接口3中实现,该器件支持EP4 in (设备到主机)中断传输类型和EP5 OUT(主机到设备)中断传输类型。它还指出,USB批量是基于具有64字节消息格式的请求-响应消息对,其中第一个字节是“操作码”。对于'get version‘命令,操作码是0x02,其余的被忽略。

我对Python和USB都是新手,所以这可能是错误的,但我正在尝试写入'OUT‘接口,然后从'IN’接口读取。我注意到,当我将设备插入Ubuntu系统时,我得到了一个新设备/dev/usb/iddev0,但似乎无法写入,所以尝试连接到接口3并抓取输入/输出接口。此外,我还添加了我认为是对PYUSB_DEBUG的调用,但我没有看到比没有它更多的输出。

使用下面的代码,我确实看到了设备,并且我得到了一个IN/OUT接口,但是写/读总是超时。我不知道我错过了什么。非常感谢你的帮助

代码语言:javascript
运行
复制
import usb.core
import usb.util
from usb.core import *
import sys
import os
import binascii
import time
import serial    
import itertools


idV = 0x2abf
idP = 0x0505

# doesnt seem to write anything to log?!
os.environ['PYUSB_DEBUG'] = 'debug'
#os.environ['PYUSB_LOG_FILENAME'] = "pyusb.log" #never written


print "finding idVendor = {}, idProduct= {}".format(idV, idP)
device = usb.core.find(idVendor=idV, idProduct=idP)

if device is None:
    print ("Device not found")
    exit()


# free up the device from the kernal
for cfg in device:
    for intf in cfg:
        if device.is_kernel_driver_active(intf.bInterfaceNumber):
            try:
                device.detach_kernel_driver(intf.bInterfaceNumber)
            except usb.core.USBError as e:
                sys.exit("Could not detach kernel driver from interface({0}): {1}".format(intf.bInterfaceNumber, str(e)))

# try default conf
print "setting configuration"
device.set_configuration()
print "config set"

print "trying to claim device"
try:
    usb.util.claim_interface(device, 0)
    print "claimed device"
except usb.core.USBError as e:
    print "Error occurred claiming " + str(e)
    sys.exit("Error occurred on claiming")
print "device claimed"

# get enpoint instance
cfg = device.get_active_configuration()
print "***********"
for intf in cfg:
    print "intf= " + str(intf)
print "***********"

# from document:
# The HID interface is implemented on Interface 3, in addition to standard endpoint (er)0, the device supports
# EP4 IN (device to host) interrupt transfer type, and EP5 OUT (host to device) interrupt transfer type
#  Note: EP$ seems to come back as 0x84 while EP5 comes back as 0x05
intf = cfg[(3, 0)]

# get the BULK OUT descriptor
epo = usb.util.find_descriptor(
    intf,
    # match our first out endpoint
    custom_match= \
        lambda e: \
            usb.util.endpoint_direction(e.bEndpointAddress) == \
            usb.util.ENDPOINT_OUT)

assert epo is not None

# get the BULK IN descriptor
epi = usb.util.find_descriptor(
    intf,
    # match our first out endpoint
    custom_match= \
        lambda e: \
            usb.util.endpoint_direction(e.bEndpointAddress) == \
            usb.util.ENDPOINT_IN)

assert epi is not None

print "write the data"
# commands are 64 bytes long, first byte is command code, 02 is 'get version', it doesn't need any of the other bytes set
try:
    # don't think I can use [0x00]*63 because it will be all pointers to same object?, call them out to be safe
    test = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
    mybuff = b'\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    #device.write(epo,mybuff,1)         #timeout on write
    #epo.write(mybuff)                  #timeout on write
    epo.write(mybuff.encode('utf-8'))   #timeout on write
except usb.core.USBError as e:
    print "Write USBError: " + str(e)
    sys.exit()

print "done writing"
print "try read?"
try:
    #info = device.read(0x84, 8)
    info = epo.read(epi.bEndpointAddress,epi.wMaxPacketSize)
except usb.core.USBError as e:
    print "Read USBError: " + str(e)
    sys.exit()

print "read: " + str(info)

输出:

代码语言:javascript
运行
复制
finding idVendor = 10943, idProduct= 1285
setting configuration
config set
trying to claim device
claimed device
device claimed
***********
< skipping audio interfaces>
intf=     INTERFACE 3: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x3
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x2
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x0
     bInterfaceProtocol :    0x0
     iInterface         :    0x8 PlaylistControl
      ENDPOINT 0x84: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x84 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0xa
      ENDPOINT 0x5: Interrupt OUT ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x5 OUT
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0xa
***********
write the data
Write USBError: [Errno 110] Operation timed out
EN

回答 1

Stack Overflow用户

发布于 2016-12-26 06:04:26

我假设你在测试代码时重用了设备,当你多次执行代码时,你的问题就会出现。你应该使用

代码语言:javascript
运行
复制
usb.util.dispose_resources(device)

在代码的末尾。尝试使用

代码语言:javascript
运行
复制
device.reset()

之后

代码语言:javascript
运行
复制
device = usb.core.find(...)

在对设备执行任何其他操作之前,如果您怀疑之前的代码运行是不干净的,则会关闭。

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

https://stackoverflow.com/questions/38658907

复制
相关文章

相似问题

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