def find(vps, sn = None): """lookup a usb device based on vid, pid and serial number""" devices = UsbTools.find_all(vps) # do we have any devices? if len(devices) == 0: return None, 'no device found' if sn is not None: # filter using the serial number devices_sn = [d for d in devices if d[2] == sn] if len(devices_sn) == 0: # we have devices, but none with this serial number s = [] s.append('no device with this serial number') s.append('devices found:') for d in devices: s.append('%04x:%04x sn %r' % (d[0], d[1], d[2])) return None, '\n'.join(s) else: devices = devices_sn # no devices if len(devices) == 0: return None, 'no device found' # multiple devices if len(devices) > 1: s = [] s.append('multiple devices found:') for d in devices: s.append('%04x:%04x sn %r' % (d[0], d[1], d[2])) return None, '\n'.join(s) # 1 device return devices[0], None
def find(vps, sn=None): """lookup a usb device based on vid, pid and serial number""" devices = UsbTools.find_all(vps) # do we have any devices? if len(devices) == 0: return None, "no device found" if sn is not None: # filter using the serial number devices_sn = [d for d in devices if d[2] == sn] if len(devices_sn) == 0: # we have devices, but none with this serial number s = [] s.append("no device with this serial number") s.append("devices found:") for d in devices: s.append("%04x:%04x sn %r" % (d[0], d[1], d[2])) return None, "\n".join(s) else: devices = devices_sn # no devices if len(devices) == 0: return None, "no device found" # multiple devices if len(devices) > 1: s = [] s.append("multiple devices found:") for d in devices: s.append("%04x:%04x sn %r" % (d[0], d[1], d[2])) return None, "\n".join(s) # 1 device return devices[0], None
def open(self, vendor, product, interface=1, index=0, serial=None, description=None): """open a new interface to the specified device""" self.usb_dev = UsbTools.get_device(vendor, product, index, serial, description) config = self.usb_dev.get_active_configuration() # check for a valid interface if interface > config.bNumInterfaces: raise usbdev_error('invalid interface: %d' % interface) self._set_interface(config, interface)
def open(self, vendor, product, interface=1, index=0, serial=None, description=None): """open a new interface to the specified device""" self.usb_dev = UsbTools.get_device(vendor, product, index, serial, description) config = self.usb_dev.get_active_configuration() # check for a valid interface if interface > config.bNumInterfaces: raise usbdev_error("invalid interface: %d" % interface) self._set_interface(config, interface)
def open(self, vid, pid, itf=0, idx=0, sn=None, descr=None): """open a new interface to the specified device""" self.usb_dev = UsbTools.get_device(vid, pid, idx, sn, descr) cfg = self.usb_dev.get_active_configuration() # check for a valid interface if itf >= cfg.bNumInterfaces: raise usbdev_error('invalid interface: %d' % itf) self._set_interface(cfg, itf)
def open_ft2232(self, vps, itf, sn): # find the device on USB devices = UsbTools.find_all(vps) if sn is not None: # filter based on device serial number devices = [dev for dev in devices if dev[2] == sn] if len(devices) == 0: raise IOError("No such device") self.vid = devices[0][0] self.pid = devices[0][1] self.sn = devices[0][2] self.ftdi = Ftdi() self.freq = self.ftdi.open_mpsse(self.vid, self.pid, itf, serial=self.sn, frequency=_FREQ) self.wrbuf = array.array('B') self.gpio_init() self.state_reset() self.sir_end_state = 'IDLE' self.sdr_end_state = 'IDLE'
def close(self): """close the interface""" UsbTools.release_device(self.usb_dev)