无法使用 pyusb 写入 USB 设备(收到超时错误)

我正在尝试使用 Python3 和 pyusb 进行一些基本的 USB 通信,但在写入 USB 设备时遇到了困难。经过大量研究,我开发了下面的代码,并且可以毫无错误地识别和声明设备,但是当我尝试写入设备时收到超时。我相信我使用的是正确的端点(HID/Interface 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?rn"

print ("writing: " + msg)

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

print ("reading...")

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

print (data)
stack overflow Cannot write to USB device with pyusb (receive timeout errors)
原文答案

答案:

作者头像

我认为应该是:

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