简体   繁体   中英

Device Not Available on PyUSB

Playing around with PyUSB a bit to see if it offers some insight as to why a WebUSB library I'm using isn't finding my device. I installed libusb1 on the Mac via Homebrew with brew install libusb .

Ran lsusb -vv to get device details. Also set a couple of environment variables for PyUSB:

export PYUSB_LOG_FILENAME=pysubdebug.log and export PYUSB_DEBUG=debug

import usb

VENDOR_ID = 0x0483
PRODUCT_ID = 0x5740
DATA_SIZE = 1

device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)

>>> device
<DEVICE ID 0483:5740 on Bus 020 Address 014>

>>> device.is_kernel_driver_active(0)
False

>>> device.set_configuration()
Traceback (abridged)
usb.core.USBError: [Errno 19] No such device (it may have been disconnected)

>>> cfg = device.get_active_configuration()
Traceback (abridged)
usb.core.USBError: [Errno None] Configuration not set

From the log file it looks like python (which is in a venv) is using a file located at /usr/local/lib/ :

2020-10-27 12:20:41,706 DEBUG:usb.backend.libusb1:_LibUSB.__init__(<CDLL '/usr/local/lib/libusb-1.0.dylib', handle 7f8ba652c7f0 at 0x1101a5f70>)
2020-10-27 12:20:41,712 INFO:usb.core:find(): using backend "usb.backend.libusb1"

Brewed files:

$brew ls libusb
/usr/local/Cellar/libusb/1.0.23/include/libusb-1.0/libusb.h
/usr/local/Cellar/libusb/1.0.23/lib/libusb-1.0.0.dylib
/usr/local/Cellar/libusb/1.0.23/lib/pkgconfig/libusb-1.0.pc
/usr/local/Cellar/libusb/1.0.23/lib/ (2 other files)
/usr/local/Cellar/libusb/1.0.23/share/libusb/ (13 files)

And confirming that PyUSB is looking in the right location:

ls -l /usr/local/lib/libusb-1.0.dylib
lrwxr-xr-x  #details# /usr/local/lib/libusb-1.0.dylib@ -> ../Cellar/libusb/1.0.23/lib/libusb-1.0.dylib

Do I need to be creating a function to Specify a library by hand ? Doesn't seem like that's the issue.

Maybe there's a configuration step I'm missing.

Being used to http:// data transfer, USB seems to be more fragile and platform specific.

For example, where I can generally load a web app in as many browsers as I like, a USB device can only be "claimed" by a single device, app (and perhaps process).

I found a (commercial) app, called Serial , through which I could (using the free demo) confirm that my device either is or isn't "claimed".

Some Apple users were able to use kextunload to "unclaim" a device.

On OSX, the kextstat terminal app yielded some details about which kernels were claiming which devices:

kextstat | grep usb
   22    8 0xffffff7f813ec000 0x8000     0x8000     com.apple.driver.usb.AppleUSBCommon (1.0) C2917767-E187-3F86-8E1D-3342A98EF53A <6 5 3 1>
   53    0 0xffffff7f817cf000 0x5000     0x5000     com.apple.driver.usb.AppleUSBHostPacketFilter (1.0) 2569DC26-1911-36D4-9BE3-A727E9535BB2 <23 22 8 7 6 5 3 1>
   54    1 0xffffff7f816a8000 0x56000    0x56000    com.apple.driver.usb.AppleUSBXHCI (1.2) 0E02208C-A8FC-3966-9C74-F09EF887E7E7 <23 22 12 8 7 6 5 3 1>
   etc...

But I kept getting an error that they were "in use" and couldn't be unloaded.

Ultimately I REBOOTED the computer, with the USB device plugged directly in using a good USB cable, and thanks to the PyUSB tutorial docs and this post was able to at least pull in some data from the device with this code:


import usb.core
import usb.util

# got these using the command lsusb -vv
VENDOR_ID = 0x0483
PRODUCT_ID = 0x5740
DATA_SIZE = 1

device = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)

device.is_kernel_driver_active(0)

device.set_configuration()

device.read(0x81, 255, 1000000)

Look:

array('B', [51, 48, 51, 32, 86, 49, 46, 48, 50, 32, 53, 56, 51, 51, 98, 49, 49, 56, 32, 48, 48, 48, 48, 48, 48, 48, 48, 32, 48, 48, 48, 48, 48, 48, 48, 48, 32, 48, 48, 48, 50, 57, 99, 97, 54, 32, 48, 48, 48, 48, 48, 48, 48, 54, 13, 10])

Gotcha

When I first tried to read the timeout was too short and/or the buffer was too small and then subsequent read requests would return usb.core.USBError: [Errno 32] Pipe error .

I imagine this is because python was busy trying to do something or needed to be disconnected. The workaround was just to exit() python and try again with higher parameters.

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