Note on libusb

pyUSB

使用說明

disconnect (釋放 USB 資源)的方法

I suppose you are reusing the device while testing your code and your problems occur when you are executing it multiple times. You should use

usb.util.dispose_resources(device)

Try using

device.reset()

after

device = usb.core.find(...)

and before doing anything else with the device if you suspect the earlier code run closed unclean.

Code Examples:

@methodtrace(utils.logger)
def runTest(self):
    try:
        self.dev.set_configuration()
        self.test_attributes()
        self.test_write_read()
    finally:
        usb.util.dispose_resources(self.dev)
def close_device(self):
    usb.util.dispose_resources(self._device)
    self._device.reset()
    self._device = None
    self.usbtimeout_ms = 0.0
    self._opened = False
def initialize():
    # reset all devices on load
    match = lambda dev: (dev.idVendor == defines.VendorId and
                         dev.idProduct in defines.ProductIds)
    devices = usb.core.find(find_all=True, custom_match=match)
    # create the handles and associate protocols
    for dev in devices:
        try:
            dev.reset()
            usb.util.dispose_resources(dev)
        except:
            pass
 
def shutdown():
    # dispose usb resources
    match = lambda dev: (dev.idVendor == defines.VendorId and
                         dev.idProduct in defines.ProductIds)
    devices = usb.core.find(find_all=True, custom_match=match)
    for dev in devices:
        try:
            usb.util.dispose_resources(dev)
        except:
            pass
def disconnect(self):
        """Disconnect the XESS Board from the USB link."""
        if self._dev != None:
            usb.util.dispose_resources(self._dev)
            # linux has a hard time when deleting USB ports that no longer exist,
            # so keep the USB devices on a discard pile so they won't get cleaned.
            self._usb_discard_pile.append(self._dev)
            self._dev = None
    def __del__(self):
        """ Release USB interface """
        if self.device:
            usb.util.dispose_resources(self.device)
        self.device = None

參考資料:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License