def test_close_on_disconnect(self): """Validate close after disconnect.""" log = logging.getLogger('pyftdi.tests.ftdi') url = environ.get('FTDI_DEVICE', 'ftdi:///1') ftdi = Ftdi() ftdi.open_from_url(url) self.assertTrue(ftdi.is_connected, 'Unable to connect to FTDI') print('Please disconnect FTDI device') while ftdi.is_connected: try: ftdi.poll_modem_status() except FtdiError: break sleep(0.1) ftdi.close() print('Please reconnect FTDI device') while True: UsbTools.flush_cache() try: ftdi.open_from_url(url) except (FtdiError, UsbToolsError): log.debug('FTDI device not detected') sleep(0.1) except ValueError: log.warning('FTDI device not initialized') ftdi.close() sleep(0.1) else: log.info('FTDI device detected') break ftdi.poll_modem_status() ftdi.close()
def unload(self) -> None: """Unload current USB topology, release all allocated devices, and flush UsbTools cache. Note that the application should also flush UsbTools cache, or reference to 'disconnected' devices may persist. """ backend = get_backend() backend.flush_devices() count = UsbTools.release_all_devices(VirtDevice) UsbTools.flush_cache() return count
def _connect(self): if self.gpio: return if self.controller is not None: # Finicky way to get pyftdi to clean up and reconnect properly # after a hardware disconnect. Not necessary on first connect. UsbTools.release_device(self.controller._ftdi._usb_dev) self.controller.close() UsbTools.flush_cache() else: self.controller = GpioMpsseController() self._setup()
def load(self, yamlfp: BinaryIO) -> None: """Load a YaML configuration stream. :param yamlfp: YaML stream to be parsed """ backend = get_backend() with yamlfp: try: for ydef in YAML().load_all(yamlfp): self._build_root(backend, ydef) except Exception as exc: raise ValueError(f'Invalid configuration: {exc}') from exc self._validate() UsbTools.release_all_devices(VirtDevice) UsbTools.flush_cache()
def test_hotplug_discovery(self): """Demonstrate how to connect to an hotplugged FTDI device, i.e. an FTDI device that is connected after the initial attempt to enumerate it on the USB bus.""" url = environ.get('FTDI_DEVICE', 'ftdi:///1') ftdi = Ftdi() timeout = now() + 5.0 # sanity check: bail out after 10 seconds while now() < timeout: try: ftdi.open_from_url(url) break except UsbToolsError: UsbTools.flush_cache() sleep(0.05) continue self.assertTrue(ftdi.is_connected, 'Unable to connect to FTDI') print('Connected to FTDI', url)
def setUpClass(cls): cls.loader = MockLoader() with open('pyftdi/tests/resources/ft232h.yaml', 'rb') as yfp: cls.loader.load(yfp) UsbTools.flush_cache()