def main(): """Entry point.""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('device', nargs='?', default='ftdi:///?', help='serial port device name') argparser.add_argument('-S', '--no-smb', action='store_true', default=False, help='use regular I2C mode vs. SMBbus scan') argparser.add_argument('-P', '--vidpid', action='append', help='specify a custom VID:PID device ID, ' 'may be repeated') argparser.add_argument('-V', '--virtual', type=FileType('r'), help='use a virtual device, specified as YaML') argparser.add_argument('-v', '--verbose', action='count', default=0, 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)) 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.log.addHandler(StreamHandler(stderr)) FtdiLogger.set_formatter(formatter) FtdiLogger.set_level(loglevel) if args.virtual: #pylint: disable-msg=import-outside-toplevel from pyftdi.usbtools import UsbTools # Force PyUSB to use PyFtdi test framework for USB backends UsbTools.BACKENDS = ('pyftdi.tests.backend.usbvirt', ) # Ensure the virtual backend can be found and is loaded backend = UsbTools.find_backend() loader = backend.create_loader()() loader.load(args.virtual) try: add_custom_devices(Ftdi, args.vidpid) except ValueError as exc: argparser.error(str(exc)) I2cBusScanner.scan(args.device, not args.no_smb) except (ImportError, IOError, NotImplementedError, ValueError) as exc: print('\nError: %s' % exc, file=stderr) if debug: print(format_exc(chain=False), file=stderr) exit(1) except KeyboardInterrupt: exit(2)
def main(): testmod(modules[__name__]) debug = to_bool(environ.get('FTDI_DEBUG', 'off')) if debug: formatter = logging.Formatter( '%(asctime)s.%(msecs)03d %(levelname)-7s' ' %(name)-18s [%(lineno)4d] %(message)s', '%H:%M:%S') else: formatter = logging.Formatter('%(message)s') level = environ.get('FTDI_LOGLEVEL', 'info').upper() try: loglevel = getattr(logging, level) except AttributeError: raise ValueError(f'Invalid log level: {level}') FtdiLogger.log.addHandler(logging.StreamHandler(stdout)) FtdiLogger.set_level(loglevel) FtdiLogger.set_formatter(formatter) # Force PyUSB to use PyFtdi test framework for USB backends UsbTools.BACKENDS = ('backend.usbvirt', ) # Ensure the virtual backend can be found and is loaded backend = UsbTools.find_backend() try: # obtain the loader class associated with the virtual backend global MockLoader MockLoader = backend.create_loader() except AttributeError: raise AssertionError('Cannot load virtual USB backend') ut_main(defaultTest='suite')
def main(): import doctest doctest.testmod(modules[__name__]) debug = to_bool(environ.get('FTDI_DEBUG', 'off')) if debug: formatter = logging.Formatter( '%(asctime)s.%(msecs)03d %(levelname)-7s' ' %(name)-20s [%(lineno)4d] %(message)s', '%H:%M:%S') else: formatter = logging.Formatter('%(message)s') level = environ.get('FTDI_LOGLEVEL', 'warning').upper() try: loglevel = getattr(logging, level) except AttributeError as exc: raise ValueError(f'Invalid log level: {level}') from exc FtdiLogger.log.addHandler(logging.StreamHandler(stdout)) FtdiLogger.set_level(loglevel) FtdiLogger.set_formatter(formatter) virtualize() try: ut_main(defaultTest='suite') except KeyboardInterrupt: pass
def main(): """Main routine""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('device', nargs='?', default='ftdi:///?', help='serial port device name') argparser.add_argument('-x', '--hexdump', action='store_true', help='dump EEPROM content as ASCII') argparser.add_argument('-X', '--hexblock', type=int, help='dump EEPROM as indented hexa blocks') argparser.add_argument('-i', '--input', type=FileType('rt'), help='input ini file to load EEPROM content') argparser.add_argument('-l', '--load', default='all', choices=('all', 'raw', 'values'), help='section(s) to load from input file') argparser.add_argument('-o', '--output', type=FileType('wt'), help='output ini file to save EEPROM content') argparser.add_argument('-s', '--serial-number', help='set serial number') argparser.add_argument('-m', '--manufacturer', help='set manufacturer name') argparser.add_argument('-p', '--product', help='set product name') argparser.add_argument('-c', '--config', action='append', help='change/configure a property ' 'as key=value pair') argparser.add_argument('-e', '--erase', action='store_true', help='erase the whole EEPROM content') argparser.add_argument('-u', '--update', action='store_true', help='perform actual update, use w/ care') argparser.add_argument('-P', '--vidpid', action='append', help='specify a custom VID:PID device ID, ' 'may be repeated') argparser.add_argument('-V', '--virtual', type=FileType('r'), help='use a virtual device, specified as YaML') argparser.add_argument('-v', '--verbose', action='count', default=0, 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)) 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) FtdiLogger.log.addHandler(StreamHandler(stderr)) if args.virtual: from pyftdi.usbtools import UsbTools # Force PyUSB to use PyFtdi test framework for USB backends UsbTools.BACKENDS = ('pyftdi.tests.backend.usbvirt', ) # Ensure the virtual backend can be found and is loaded backend = UsbTools.find_backend() loader = backend.create_loader()() loader.load(args.virtual) try: add_custom_devices(Ftdi, args.vidpid) except ValueError as exc: argparser.error(str(exc)) eeprom = FtdiEeprom() eeprom.open(args.device) if args.erase: eeprom.erase() if args.input: eeprom.load_config(args.input, args.load) if args.serial_number: eeprom.set_serial_number(args.serial_number) if args.manufacturer: eeprom.set_manufacturer_name(args.manufacturer) if args.product: eeprom.set_product_name(args.product) for conf in args.config or []: if conf == '?': helpstr = ', '.join(sorted(eeprom.properties)) print( fill(helpstr, initial_indent=' ', subsequent_indent=' ')) exit(1) for sep in ':=': if sep in conf: name, value = conf.split(sep, 1) if not value: argparser.error('Configuration %s without value' % conf) helpio = StringIO() eeprom.set_property(name, value, helpio) helpstr = helpio.getvalue() if helpstr: print( fill(helpstr, initial_indent=' ', subsequent_indent=' ')) exit(1) break else: argparser.error('Missing name:value separator in %s' % conf) if args.hexdump: print(hexdump(eeprom.data)) if args.hexblock is not None: indent = ' ' * args.hexblock for pos in range(0, len(eeprom.data), 16): hexa = ' '.join( ['%02x' % x for x in eeprom.data[pos:pos + 16]]) print(indent, hexa, sep='') if args.update: if eeprom.commit(False): eeprom.reset_device() if args.verbose > 0: eeprom.dump_config() if args.output: eeprom.save_config(args.output) except (ImportError, IOError, NotImplementedError, ValueError) as exc: print('\nError: %s' % exc, 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 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('-P', '--pdelay', type=float, help='pulse DTR at start-up (delay in seconds)') 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('-T', '--reltime', action='store_true', help='show relative time, not host time') argparser.add_argument('-o', '--rawlog', type=FileType('wb'), help='output (unformatted) log file') argparser.add_argument('-O', '--logfile', help='output formatted, rotatable log file') argparser.add_argument('-y', '--syslog', help='push log to syslog daemon ' 'facility:[host[:port[:format]]]') argparser.add_argument('-g', '--filterlog', action='store_true', help='enable filter log feature, flip-flop with' ' [Ctrl]+G') argparser.add_argument('-c', '--color', action='store_true', help='show available colors and exit') argparser.add_argument('-s', '--silent', action='store_true', help='silent mode') argparser.add_argument('-v', '--verbose', action='count', default=0, help='increase verbosity') argparser.add_argument('-d', '--debug', action='store_true', help='enable debug mode') args = argparser.parse_args() debug = args.debug if args.color: fmtcls = get_term_formatter(not is_term()) fmtcls(stdout, None).show_colors() exit(0) if not args.device: argparser.error('Serial device not specified') loglevel = max(DEBUG, ERROR - (10 * (args.verbose or 0))) loglevel = min(ERROR, loglevel) localfmt = Formatter( '%(levelname)s %(asctime)s.%(msecs)03d ' '%(message)s', '%H:%M:%S') FtdiLogger.set_formatter(localfmt) FtdiLogger.set_level(loglevel if args.verbose > 3 else ERROR) if args.filterlog: fmtcls = get_term_formatter(not is_term()) logfilter = fmtcls(stdout, None, basetime=-1 if args.reltime else None) else: logfilter = None miniterm = MiniTerm(device=args.device, baudrate=to_int(args.baudrate), parity='N', rtscts=args.hwflow, logfilter=logfilter, logfile=args.rawlog, filelog=(args.logfile, localfmt, loglevel), syslog=(args.syslog, loglevel), debug=args.debug) if args.pdelay: miniterm.pulse_dtr(args.pdelay) miniterm.run(args.fullmode, args.loopback, args.silent, args.localecho, args.crlf) except (IOError, ValueError) as exc: print('\nError: %s' % exc, 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__) argparser.add_argument('-f', '--fullmode', dest='fullmode', action='store_true', help='use full terminal mode, exit with ' '[Ctrl]+B') argparser.add_argument('device', nargs='?', 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('-P', '--vidpid', action='append', help='specify a custom VID:PID device ID, ' 'may be repeated') argparser.add_argument('-V', '--virtual', type=FileType('r'), help='use a virtual device, specified as YaML') 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) FtdiLogger.log.addHandler(StreamHandler(stderr)) if args.virtual: from pyftdi.usbtools import UsbTools # Force PyUSB to use PyFtdi test framework for USB backends UsbTools.BACKENDS = ('pyftdi.tests.backend.usbvirt', ) # Ensure the virtual backend can be found and is loaded backend = UsbTools.find_backend() loader = backend.create_loader()() loader.load(args.virtual) try: add_custom_devices(Ftdi, args.vidpid) except ValueError as exc: argparser.error(str(exc)) miniterm = MiniTerm(device=args.device, baudrate=to_bps(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 exc: print('\nError: %s' % exc, file=stderr) if debug: print(format_exc(chain=False), file=stderr) sysexit(1) except KeyboardInterrupt: sysexit(2)
def main(): """Main routine""" debug = False try: argparser = ArgumentParser(description=modules[__name__].__doc__) argparser.add_argument('device', nargs='?', default='ftdi:///?', help='serial port device name') argparser.add_argument('-x', '--hexdump', action='store_true', help='dump EEPROM content as ASCII') argparser.add_argument('-o', '--output', type=FileType('wt'), help='output ini file to save EEPROM content') argparser.add_argument('-s', '--serial-number', help='set serial number') argparser.add_argument('-m', '--manufacturer', help='set manufacturer name') argparser.add_argument('-p', '--product', help='set product name') argparser.add_argument('-e', '--erase', action='store_true', help='erase the whole EEPROM content') argparser.add_argument('-u', '--update', action='store_true', help='perform actual update, use w/ care') argparser.add_argument('-v', '--verbose', action='count', default=0, 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)) 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) FtdiLogger.log.addHandler(StreamHandler(stderr)) eeprom = FtdiEeprom() eeprom.open(args.device) if args.erase: eeprom.erase() if args.serial_number: eeprom.set_serial_number(args.serial_number) if args.manufacturer: eeprom.set_manufacturer_name(args.manufacturer) if args.product: eeprom.set_product_name(args.product) if args.hexdump: print(hexdump(eeprom.data)) if args.update: eeprom.commit(False) if args.verbose > 0: eeprom.dump_config() if args.output: eeprom.save_config(args.output) except (IOError, ValueError) as exc: print('\nError: %s' % exc, 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) FtdiLogger.log.addHandler(StreamHandler(stderr)) init_term(args.fullmode) miniterm = MiniTerm(device=args.device, baudrate=to_bps(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 exc: print('\nError: %s' % exc, 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 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)