8

I am currently working on PyUSB. As I am new to USB, I don’t know, how can I do the following.

I have successfully connected to my USB Device hardware from Python PyUSB. In the code I required to reset the USB Device hardware. Which I did by sending a command to the hardware. Now after hardware reset, I want to release the current USB device from Python PyUSB. And then I want to connect again to the USB Device Hardware after it come back from reset.

Please let me know, how can I release the USB Device Connection and interfaces etc so that I can reconnect?

Thank you very much in advance.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
user977601
  • 503
  • 2
  • 7
  • 16

3 Answers3

6
my_device = usb.core.find(...)

...

# necessary to allow further claim_interface calls
#   (bulk read), generally not needed
usb.util.dispose_resources(my_device)
Liviu
  • 1,859
  • 2
  • 22
  • 48
  • "this function releases all internal resources allocated by the device, like device handle and interface policy." https://github.com/pyusb/pyusb/blob/ffe6faf42c6ad273880b0b464b9bbf44c1d4b2e9/usb/util.py#L206 – Liviu Jan 02 '19 at 09:18
4
#!/usr/bin/python
from usb.core import find as finddev
dev = finddev(idVendor=0x1234, idProduct=0x5678)
dev.reset()
Jacek Krysztofik
  • 1,266
  • 1
  • 13
  • 29
  • Not in the business anymore, but the `reset` command also does hardware reset in addition to resources disposal as seen [here](https://github.com/pyusb/pyusb/blob/ffe6faf42c6ad273880b0b464b9bbf44c1d4b2e9/usb/core.py#L913). This might be indeed what the author of the question wants. – Liviu Jan 02 '19 at 09:25
0

While the above answer is perfectly valid, I've encountered multiple times when resetting an USB device is not sufficient.

A good way to deal with those is to remove power from the USB controller, forcing them to restart as if they were plugged out / plugged in.

TL;DR: I've built a small python script to reset usb devices or usb controllers. See this link worked for me where no other reset solution worked (some bad USB UPSes) Usage:

usb_reset --reset
#or
usb_reset --list && usb_reset -d 1234:1234

Original idea using bash comes from here

    for i in /sys/bus/pci/drivers/[uoex]hci_hcd/*:*; do
      [ -e "$i" ] || continue
      echo "Resetting ${i%/*}/${i##*/}"
      echo "${i##*/}" > "${i%/*}/unbind"
      echo "${i##*/}" > "${i%/*}/bind"
    done
Orsiris de Jong
  • 2,819
  • 1
  • 26
  • 48