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



我正在尝试使用Python3和pyusb进行一些基本的USB通信,但在写入USB设备时遇到了困难。经过大量研究,我开发了下面的代码,可以毫无错误地识别和声明设备,但当我试图写入设备时,会收到超时。我相信我使用的是正确的端点(HID/Interface0,在任何情况下都尝试过,但没有成功。(FWIW是'*IDN?'是一个SCPI命令,我的愿望是简单地向设备写入SCPI(ASCII(字符串,然后从设备中读取。

关于设备的信息,收到的错误,以及我的示例代码如下。Fyi这一切都在巴斯特/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)

我认为应该是:

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

相关内容

  • 没有找到相关文章

最新更新