def main(): """Main routine""" optparser, options = get_options() try: miniterm = MiniTerm(device=options.device, baudrate=to_int(options.baudrate), logfile=options.logfile, debug=options.debug) miniterm.run(os.name in ('posix', ) and options.fullmode or False, options.reset, options.select) except (AssertionError, IOError, ValueError), e: print >> sys.stderr, '\nError: %s' % e if options.debug: import traceback print >> sys.stderr, traceback.format_exc() sys.exit(1)
def open(self, devclass, scheme, vdict, pdict, default_vendor): from serial import SerialException if self._port is None: raise SerialException("Port must be configured before use.") portstr = self.portstr if not portstr.startswith(scheme): raise SerialException("Invalid URL") plloc = portstr[len(scheme):].split('/') plcomps = plloc[0].split(':') + [''] * 2 try: plcomps[0] = vdict.get(plcomps[0], plcomps[0]) if plcomps[0]: vendor = to_int(plcomps[0]) else: vendor = None product_ids = pdict.get(vendor, None) if not product_ids: product_ids = pdict[default_vendor] plcomps[1] = product_ids.get(plcomps[1], plcomps[1]) if plcomps[1]: product = to_int(plcomps[1]) else: product = None if not plloc[1]: raise SerialException('Invalid device port') if plloc[1] == '?': show_devices = True else: interface = to_int(plloc[1]) show_devices = False except (IndexError, ValueError): raise SerialException('Invalid device URL') sernum = None idx = 0 if plcomps[2]: try: idx = to_int(plcomps[2]) if idx > 255: idx = 0 raise ValueError if idx: idx -= 1 except ValueError: sernum = plcomps[2] try: self.udev = devclass() if not vendor or not product or sernum or idx: # Need to enumerate USB devices to find a matching device vendors = vendor and [vendor] or \ set(vdict.values()) vps = set() for v in vendors: products = pdict.get(v, []) for p in products: vps.add((v, products[p])) devices = devclass.find_all(vps) candidates = [] if sernum: if sernum not in [dev[2] for dev in devices]: raise SerialException("No USB device with S/N %s" % \ sernum) for v, p, s, i, d in devices: if s != sernum: continue if vendor and vendor != v: continue if product and product != p: continue candidates.append((v, p, s, i, d)) else: for v, p, s, i, d in devices: if vendor and vendor != v: continue if product and product != p: continue candidates.append((v, p, s, i, d)) if not show_devices: try: vendor, product, ifport, ifcount, description = \ candidates[idx] except IndexError: raise SerialException("No USB device #%d" % idx) if show_devices: self.show_devices(scheme, vdict, pdict, candidates) raise SystemExit(candidates and \ 'Please specify the USB device' or \ 'No USB-Serial device has been detected') if vendor not in pdict: raise SerialException('Vendor ID 0x%04x not supported' % \ vendor) if product not in pdict[vendor].values(): raise SerialException('Product ID 0x%04x not supported' % \ product) self.udev.open(vendor, product, interface, idx, sernum) except IOError: raise SerialException('Unable to open USB port %s' % self.portstr) self._isOpen = True self._reconfigurePort() self._product = product