Search code examples
pythonpython-3.xusbraspbianpyusb

I keep getting "usb.core.USBError: [Errno 16] Resource busy" when trying to read from a USB device using pyusb


Im trying to communicate with this device with Python using the Pyusb module

The device is a DualSense Ps5 controller and I'm running Rasbian Lite (no desktop env)

My code so far is:

import usb.core

dev = usb.core.find(idVendor=0x054c,idProduct=0x0ce6)
ep = dev[0].interfaces()[5].endpoints()[0]

i = dev[0].interfaces()[5].bInterfaceNumber

dev.reset()

if dev.is_kernel_driver_active(i):
    dev.detach_kernel_driver(i)
    
dev.set_configuration()

eaddr = ep.bEndpointAdress

r = dev.read(eaddr,1024)
print(len(r))

The error I keep getting is:

Traceback (most recent call last):
  File "readUsb.py", line 25, in <module>
    dev.set_configuration()
  File "/usr/local/lib/python3.7/dist-packages/usb/core.py", line 905, in set_configuration
    self._ctx.managed_set_configuration(self, configuration)
  File "/usr/local/lib/python3.7/dist-packages/usb/core.py", line 113, in wrapper
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/usb/core.py", line 159, in managed_set_configuration
    self.backend.set_configuration(self.handle, cfg.bConfigurationValue)
  File "/usr/local/lib/python3.7/dist-packages/usb/backend/libusb1.py", line 812, in set_configuration
    _check(self.lib.libusb_set_configuration(dev_handle.handle, config_value))
  File "/usr/local/lib/python3.7/dist-packages/usb/backend/libusb1.py", line 604, in _check
    raise USBError(_strerror(ret), ret, _libusb_errno[ret])
usb.core.USBError: [Errno 16] Resource busy

I know very little about how usb devices communicate and the interface I chose is sort of arbitrary. Any advice you may have on how to free up this resource or communicate with the device in general is appreciated.


Solution

  • The following worked for me:

    if dev.is_kernel_driver_active(i):
        try:
            dev.detach_kernel_driver(i)
        except usb.core.USBError as e:
            sys.exit("Could not detatch kernel driver from interface({0}): {1}".format(i, str(e)))
    

    if this does not work use it but also remove the dev.set_configuration() and it should work.