USB Device Release
Asked Answered
H

3

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.

Haunch answered 2/9, 2014 at 6:22 Comment(1)
Have you, please, found your answer ?Silures
S
6
my_device = usb.core.find(...)

...

# necessary to allow further claim_interface calls
#   (bulk read), generally not needed
usb.util.dispose_resources(my_device)
Silures answered 23/1, 2015 at 15:43 Comment(1)
"this function releases all internal resources allocated by the device, like device handle and interface policy." github.com/pyusb/pyusb/blob/…Silures
K
4
#!/usr/bin/python
from usb.core import find as finddev
dev = finddev(idVendor=0x1234, idProduct=0x5678)
dev.reset()
Kayser answered 25/12, 2016 at 21:55 Comment(1)
Not in the business anymore, but the reset command also does hardware reset in addition to resources disposal as seen here. This might be indeed what the author of the question wants.Silures
K
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
Kester answered 27/10, 2022 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.