def main(): """Main routine""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('-p', '--device', required=True, help="serial port device name " "(list available ports with 'ftdi:///?')") argparser.add_argument('-b', '--baudrate', dest='baudrate', help='serial port baudrate', default='115200') argparser.add_argument('-r', '--reset', dest='reset', help='HW reset on DTR line', default=None) argparser.add_argument('-s', '--select', dest='select', help='Mode selection on RTS line', default=None) argparser.add_argument('-o', '--logfile', dest='logfile', help='path to the log file') if os.name in ('posix', ): argparser.add_argument( '-f', '--fullmode', dest='fullmode', action='store_true', help='use full terminal mode, exit with [Ctrl]+A') argparser.add_argument('-d', '--debug', dest='debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug miniterm = MiniTerm(device=args.device, baudrate=to_int(args.baudrate), logfile=args.logfile, debug=args.debug) miniterm.run(os.name in ('posix', ) and args.fullmode or False, args.reset, args.select) except Exception as e: print('\nError: %s' % e, file=stderr) if debug: print(format_exc(), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
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 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 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 (IOError, ValueError) as e: print_('\nError: %s' % e, file=sys.stderr) if options.debug: import traceback print_(traceback.format_exc(), file=sys.stderr) sys.exit(1) except KeyboardInterrupt: sys.exit(2)
def parse_url(urlstr, devclass, scheme, vdict, pdict, default_vendor): urlparts = urlsplit(urlstr) if scheme != urlparts.scheme: raise UsbToolsError("Invalid URL: %s" % urlstr) plcomps = urlparts.netloc.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 urlparts.path: raise UsbToolsError('URL string is missing device port') path = urlparts.path.strip('/') if path == '?' or (not path and urlstr.endswith('?')): show_devices = True else: interface = to_int(path) show_devices = False except (IndexError, ValueError): raise UsbToolsError('Invalid device URL: %s' % urlstr) 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] 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 UsbToolsError("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 UsbToolsError("No USB device #%d" % idx) if show_devices: UsbTools.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 UsbToolsError('Vendor ID 0x%04x not supported' % vendor) if product not in pdict[vendor].values(): raise UsbToolsError('Product ID 0x%04x not supported' % product) return vendor, product, interface, sernum, idx
def main(): """Main routine""" debug = False try: default_device = get_default_device() argparser = ArgumentParser(description=modules[__name__].__doc__) if osname in ('posix', ): argparser.add_argument('-f', '--fullmode', dest='fullmode', action='store_true', help='use full terminal mode, exit with ' '[Ctrl]+B') argparser.add_argument('-p', '--device', default=default_device, help='serial port device name (default: %s)' % default_device) argparser.add_argument('-b', '--baudrate', help='serial port baudrate (default: %d)' % MiniTerm.DEFAULT_BAUDRATE, default='%s' % MiniTerm.DEFAULT_BAUDRATE) argparser.add_argument('-w', '--hwflow', action='store_true', help='hardware flow control') argparser.add_argument('-e', '--localecho', action='store_true', help='local echo mode (print all typed chars)') argparser.add_argument('-r', '--crlf', action='count', default=0, help='prefix LF with CR char, use twice to ' 'replace all LF with CR chars') argparser.add_argument('-l', '--loopback', action='store_true', help='loopback mode (send back all received ' 'chars)') argparser.add_argument('-s', '--silent', action='store_true', help='silent mode') argparser.add_argument('-v', '--verbose', action='count', help='increase verbosity') argparser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug if not args.device: argparser.error('Serial device not specified') loglevel = max(DEBUG, ERROR - (10 * (args.verbose or 0))) loglevel = min(ERROR, loglevel) if debug: formatter = Formatter('%(asctime)s.%(msecs)03d %(name)-20s ' '%(message)s', '%H:%M:%S') else: formatter = Formatter('%(message)s') FtdiLogger.set_formatter(formatter) FtdiLogger.set_level(loglevel) miniterm = MiniTerm(device=args.device, baudrate=to_int(args.baudrate), parity='N', rtscts=args.hwflow, debug=args.debug) miniterm.run(args.fullmode, args.loopback, args.silent, args.localecho, args.crlf) except (IOError, ValueError) as e: print('\nError: %s' % e, file=stderr) if debug: print(format_exc(chain=False), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
def main(): """Main routine""" debug = False try: default_device = get_default_device() argparser = ArgumentParser(description=modules[__name__].__doc__) if platform != 'win32': argparser.add_argument('-f', '--fullmode', dest='fullmode', action='store_true', help='use full terminal mode, exit with ' '[Ctrl]+B') argparser.add_argument('-p', '--device', default=default_device, help='serial port device name (default: %s)' % default_device) argparser.add_argument('-b', '--baudrate', help='serial port baudrate (default: %d)' % MiniTerm.DEFAULT_BAUDRATE, default='%s' % MiniTerm.DEFAULT_BAUDRATE) argparser.add_argument('-w', '--hwflow', action='store_true', help='hardware flow control') argparser.add_argument('-e', '--localecho', action='store_true', help='local echo mode (print all typed chars)') argparser.add_argument('-r', '--crlf', action='count', default=0, help='prefix LF with CR char, use twice to ' 'replace all LF with CR chars') argparser.add_argument('-l', '--loopback', action='store_true', help='loopback mode (send back all received ' 'chars)') argparser.add_argument('-s', '--silent', action='store_true', help='silent mode') argparser.add_argument('-v', '--verbose', action='count', help='increase verbosity') argparser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug if not args.device: argparser.error('Serial device not specified') loglevel = max(DEBUG, ERROR - (10 * (args.verbose or 0))) loglevel = min(ERROR, loglevel) if debug: formatter = Formatter('%(asctime)s.%(msecs)03d %(name)-20s ' '%(message)s', '%H:%M:%S') else: formatter = Formatter('%(message)s') FtdiLogger.set_formatter(formatter) FtdiLogger.set_level(loglevel) miniterm = MiniTerm(device=args.device, baudrate=to_int(args.baudrate), parity='N', rtscts=args.hwflow, debug=args.debug) miniterm.run(args.fullmode, args.loopback, args.silent, args.localecho, args.crlf) except (IOError, ValueError) as e: print('\nError: %s' % e, file=stderr) if debug: print(format_exc(chain=False), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
def parse_url(urlstr, devclass, scheme, vdict, pdict, default_vendor): urlparts = urlsplit(urlstr) if scheme != urlparts.scheme: raise UsbToolsError("Invalid URL: %s" % urlstr) plcomps = urlparts.netloc.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 urlparts.path: raise UsbToolsError('URL string is missing device port') path = urlparts.path.strip('/') if path == '?' or (not path and urlstr.endswith('?')): show_devices = True else: interface = to_int(path) show_devices = False except (IndexError, ValueError): raise UsbToolsError('Invalid device URL: %s' % urlstr) 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] 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 UsbToolsError("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 UsbToolsError("No USB device #%d" % idx) if show_devices: UsbTools.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 UsbToolsError('Vendor ID 0x%04x not supported' % vendor) if product not in pdict[vendor].values(): raise UsbToolsError('Product ID 0x%04x not supported' % product) return vendor, product, interface, sernum, idx
def main(): """Main routine""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('-p', '--device', default='ftdi:///?', help="serial port device name " "(list available ports with 'ftdi:///?')") argparser.add_argument('-b', '--baudrate', dest='baudrate', help='serial port baudrate', default='115200') argparser.add_argument('-r', '--reset', dest='reset', help='HW reset on DTR line', default=None) argparser.add_argument('-s', '--select', dest='select', help='Mode selection on RTS line', default=None) argparser.add_argument('-o', '--logfile', dest='logfile', help='path to the log file') if os.name in ('posix', ): argparser.add_argument( '-f', '--fullmode', dest='fullmode', action='store_true', help='use full terminal mode, exit with [Ctrl]+A') argparser.add_argument('-d', '--debug', dest='debug', action='store_true', help='enable debug mode') argparser.add_argument('--no-color', dest='no_color', action='store_true', default=False, help='disable screen color mode.') argparser.add_argument('-F', '--twefirm', dest='twefirm', default='', help='write TWE firmware before start.') argparser.add_argument('--no-term', dest='no_term', action='store_true', default=False, help='write TWE firmware and exit program.') args = argparser.parse_args() debug = args.debug miniterm = MiniTerm(device=args.device, baudrate=to_int(args.baudrate), logfile=args.logfile, debug=args.debug, twefirm=args.twefirm, no_color=args.no_color, no_term=args.no_term) miniterm.run(os.name in ('posix', ) and args.fullmode or False, args.reset, args.select) except Exception as e: TWELogger.screen_string_black() print('\nError: %s' % e) if debug: print(format_exc()) exit(1) except KeyboardInterrupt: exit(2)