无法使用pyusb写入USB设备接收超时错误

问题描述

我正在尝试使用python3和pyusb进行一些基本的USB通信,并且在写入USB设备时遇到了困难。经过大量研究,我开发了以下代码,可以识别并声明设备没有错误,但是在尝试写入设备时收到超时。我相信我使用了正确的端点(HID /接口0,无论如何都尝试了所有端点,但均未成功。)FWIW'* IDN?'是SCPI命令,我的愿望是简单地在设备中写入和读取SCPI(ASCII)字符串。

有关设备,收到的错误和示例代码的信息如下。事实证明,它们都在Buster / 10.4下的R-Pi3 +上运行。任何指针将不胜感激。谢谢。

尝试运行并收到错误

pi@raspBerrypi:~/src $ sudo python3 test6.py
Ladybug device found
writing: *IDN?

Traceback (most recent call last):
  File "test6.py",line 36,in <module>
    dev.write(0x81,msg,1000)
  File "/usr/local/lib/python3.7/site-packages/usb/core.py",line 982,in write
    self.__get_timeout(timeout)
  File "/usr/local/lib/python3.7/site-packages/usb/backend/libusb1.py",line 860,in intr_write
    timeout)
  File "/usr/local/lib/python3.7/site-packages/usb/backend/libusb1.py",line 938,in __write
    _check(retval)
  File "/usr/local/lib/python3.7/site-packages/usb/backend/libusb1.py",line 602,in _check
    raise USBTimeoutError(_strerror(ret),ret,_libusb_errno[ret])
usb.core.USBTimeoutError: [Errno 110] Operation timed out

设备信息:

DEVICE ID 1a0d:15d8 on Bus 001 Address 004 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x200 USB 2.0
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :   0x40 (64 bytes)
 idvendor               : 0x1a0d
 idProduct              : 0x15d8
 bcdDevice              :  0x100 Device 1.0
 iManufacturer          :    0x1 LadyBug Technologies LLC
 iProduct               :    0x2 LB5908A
 iSerialNumber          :    0x3 189546
 bNumConfigurations     :    0x1
  CONfigURATION 1: 500 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x47 (71 bytes)
   bNumInterfaces       :    0x2
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0
   bmAttributes         :   0x80 Bus Powered
   bMaxPower            :   0xfa (500 mA)
    INTERFACE 0: Human Interface Device ====================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x2
     bInterfaceClass    :    0x3 Human Interface Device
     bInterfaceSubClass :    0x0
     bInterfaceProtocol :    0x0
     iInterface         :    0x0
      ENDPOINT 0x81: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x81 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x80 (128 bytes)
       bInterval        :    0x1
      ENDPOINT 0x1: Interrupt OUT ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x1 OUT
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x80 (128 bytes)
       bInterval        :    0x1
    INTERFACE 1: Application Specific ======================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x1
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x3
     bInterfaceClass    :   0xfe Application Specific
     bInterfaceSubClass :    0x3
     bInterfaceProtocol :    0x1
     iInterface         :    0x0
      ENDPOINT 0x82: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x82 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x2: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x2 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x83: Interrupt IN ==========================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x83 IN
       bmAttributes     :    0x3 Interrupt
       wMaxPacketSize   :   0x40 (64 bytes)
       bInterval        :    0x1

代码

import usb.core
import usb.util
import time

# find the device
dev = usb.core.find(idvendor=0x1a0d,idProduct=0x15d8)

# was it found?
if dev is None:
    raise ValueError('Ladybug device not found')
else:
    print ("Ladybug device found")

# required to detach device from OS,othewise I receive a 'device busy' error
for cfg in dev:
  for intf in cfg:
    if dev.is_kernel_driver_active(intf.bInterfaceNumber):
      try:
        dev.detach_kernel_driver(intf.bInterfaceNumber)
      except usb.core.USBError as e:
        sys.exit("Could not detatch kernel driver from interface({0}): {1}".format(intf.bInterfaceNumber,str(e)))


dev.set_configuration()

usb.util.claim_interface(dev,0)


data = ('null')
msg = "*IDN?\r\n"


print ("writing: " + msg)

# write the data
dev.write(0x81,1000)


print ("reading...")

data = dev.read(0x1,100,1000)


print (data)

解决方法

我认为应该是:

# write the data
dev.write(0x02,msg,1000) # bulk out
print ("reading...")`
data = dev.read(0x82,100,1000) # bulk in