def _open(self) -> None: print('USB OPEN START') try: # find the first USB device that matches the filter self._dev = find(idVendor=self._idVendor, idProduct=self._idProduct) if self._dev is None: raise DriverException("Could not open specified device") # Detach kernel driver try: if self._dev.is_kernel_driver_active(0): try: self._dev.detach_kernel_driver(0) except USBError as e: raise DriverException("Could not detach kernel driver") except NotImplementedError: pass # for non unix systems # set the active configuration. With no arguments, the first # configuration will be the active one self._dev.set_configuration() # get an endpoint instance cfg = self._dev.get_active_configuration() self._interfaceNumber = cfg[(0, 0)].bInterfaceNumber interface = find_descriptor(cfg, bInterfaceNumber=self._interfaceNumber, bAlternateSetting=get_interface( self._dev, self._interfaceNumber)) claim_interface(self._dev, self._interfaceNumber) self._epOut = find_descriptor( interface, custom_match=lambda e: endpoint_direction(e.bEndpointAddress ) == ENDPOINT_OUT) self._epIn = find_descriptor( interface, custom_match=lambda e: endpoint_direction(e.bEndpointAddress ) == ENDPOINT_IN) if self._epOut is None or self._epIn is None: raise DriverException("Could not initialize USB endpoint") self._queue = Queue() self._loop = self.USBLoop(self._epIn, self._packetSize, self._queue) self._loop.start() self._driver_open = True print('USB OPEN SUCCESS') except IOError as e: self._close() raise DriverException(str(e))
def _open(self): # Most of this is straight from the PyUSB example documentation dev = findDeviceUSB( idVendor=self.idVendor, idProduct=self.idProduct, custom_match=lambda d: (d.bus == self.bus or self.bus is None) and (d.address == self.address or self.address is None)) if dev is None: raise DriverError("Could not open device (not found)") # make sure the kernel driver is not active try: if dev.is_kernel_driver_active(0): try: dev.detach_kernel_driver(0) except USBError as e: exit("could not detach kernel driver: {}".format(e)) except NotImplementedError: pass # for non unix systems dev.set_configuration() cfg = dev.get_active_configuration() interfaceNumber = cfg[(0, 0)].bInterfaceNumber intf = find_descriptor(cfg, bInterfaceNumber=interfaceNumber, bAlternateSetting=get_interface( dev, interfaceNumber)) claim_interface(dev, interfaceNumber) endpoint_out_matcher = \ lambda e: endpoint_direction(e.bEndpointAddress) == ENDPOINT_OUT epOut = find_descriptor(intf, custom_match=endpoint_out_matcher) assert epOut is not None endpoint_in_matcher = \ lambda e: endpoint_direction(e.bEndpointAddress) == ENDPOINT_IN ep_in = find_descriptor(intf, custom_match=endpoint_in_matcher) assert ep_in is not None self._epOut = epOut self._epIn = ep_in self._dev = dev self._intNum = interfaceNumber