简体   繁体   中英

Cannot write to USB device with pyusb (receive timeout errors)

I'm attempting some basic USB communications using Python3 and pyusb and am running into a difficulty writing to a USB device. After considerable research I've developed the code below and can recognize and claim the device without errors, but am receiving a timeout when I attempt to write to the device. I believe I'm using the correct endpoints (HID/Interface 0, and in any event have tried them all without success.) FWIW the '*IDN?' is a SCPI command and my desire is to simply write and then read SCPI (ASCII) strings to and from the device.

Info regarding the device, the error received, and my example code is below. Fyi it is all running on a R-Pi3+ under Buster/10.4. Any pointers would be greatly appreciated. Thanks.

Run attempt and error received:

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 info:

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

Code:

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,msg,1000)


print ("reading...")

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


print (data)

I think it should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM