parameters: (default_port=None, default_baudrate=9600, default_rts=None, default_dtr=None)
docstring: Command line tool, entry point
docstring_summary: Command line tool, entry point
docstring_tokens: Command line tool entry point
function: def main(default_port=None, default_baudrate=9600, default_rts=None, default_dtr=None):
      """Command line tool, entry point"""
  
      import argparse
  
      parser = argparse.ArgumentParser(
          description="Miniterm - A simple terminal program for the serial port.")
  
      parser.add_argument(
          "port",
          nargs='?',
          help="serial port name ('-' to show port list)",
          default=default_port)
  
      parser.add_argument(
          "baudrate",
          nargs='?',
          type=int,
          help="set baud rate, default: %(default)s",
          default=default_baudrate)
  
      group = parser.add_argument_group("port settings")
  
      group.add_argument(
          "--parity",
          choices=['N', 'E', 'O', 'S', 'M'],
          type=lambda c: c.upper(),
          help="set parity, one of {N E O S M}, default: N",
          default='N')
  
      group.add_argument(
          "--rtscts",
          action="store_true",
          help="enable RTS/CTS flow control (default off)",
          default=False)
  
      group.add_argument(
          "--xonxoff",
          action="store_true",
          help="enable software flow control (default off)",
          default=False)
  
      group.add_argument(
          "--rts",
          type=int,
          help="set initial RTS line state (possible values: 0, 1)",
          default=default_rts)
  
      group.add_argument(
          "--dtr",
          type=int,
          help="set initial DTR line state (possible values: 0, 1)",
          default=default_dtr)
  
      group.add_argument(
          "--ask",
          action="store_true",
          help="ask again for port when open fails",
          default=False)
  
      group = parser.add_argument_group("data handling")
  
      group.add_argument(
          "-e", "--echo",
          action="store_true",
          help="enable local echo (default off)",
          default=False)
  
      group.add_argument(
          "--encoding",
          dest="serial_port_encoding",
          metavar="CODEC",
          help="set the encoding for the serial port (e.g. hexlify, Latin1, UTF-8), default: %(default)s",
          default='UTF-8')
  
      group.add_argument(
          "-f", "--filter",
          action="append",
          metavar="NAME",
          help="add text transformation",
          default=[])
  
      group.add_argument(
          "--eol",
          choices=['CR', 'LF', 'CRLF'],
          type=lambda c: c.upper(),
          help="end of line mode",
          default='CRLF')
  
      group.add_argument(
          "--raw",
          action="store_true",
          help="Do no apply any encodings/transformations",
          default=False)
  
      group = parser.add_argument_group("hotkeys")
  
      group.add_argument(
          "--exit-char",
          type=int,
          metavar='NUM',
          help="Unicode of special character that is used to exit the application, default: %(default)s",
          default=0x1d)  # GS/CTRL+]
  
      group.add_argument(
          "--menu-char",
          type=int,
          metavar='NUM',
          help="Unicode code of special character that is used to control miniterm (menu), default: %(default)s",
          default=0x14)  # Menu: CTRL+T
  
      group = parser.add_argument_group("diagnostics")
  
      group.add_argument(
          "-q", "--quiet",
          action="store_true",
          help="suppress non-error messages",
          default=False)
  
      group.add_argument(
          "--develop",
          action="store_true",
          help="show Python traceback on error",
          default=False)
  
      args = parser.parse_args()
  
      if args.menu_char == args.exit_char:
          parser.error('--exit-char can not be the same as --menu-char')
  
      if args.filter:
          if 'help' in args.filter:
              sys.stderr.write('Available filters:\n')
              sys.stderr.write('\n'.join(
                  '{:<10} = {.__doc__}'.format(k, v)
                  for k, v in sorted(TRANSFORMATIONS.items())))
              sys.stderr.write('\n')
              sys.exit(1)
          filters = args.filter
      else:
          filters = ['default']
  
      while True:
          # no port given on command line -> ask user now
          if args.port is None or args.port == '-':
              try:
                  args.port = ask_for_port()
              except KeyboardInterrupt:
                  sys.stderr.write('\n')
                  parser.error('user aborted and port is not given')
              else:
                  if not args.port:
                      parser.error('port is not given')
          try:
              serial_instance = serial.serial_for_url(
                  args.port,
                  args.baudrate,
                  parity=args.parity,
                  rtscts=args.rtscts,
                  xonxoff=args.xonxoff,
                  do_not_open=True)
  
              if not hasattr(serial_instance, 'cancel_read'):
                  # enable timeout for alive flag polling if cancel_read is not available
                  serial_instance.timeout = 1
  
              if args.dtr is not None:
                  if not args.quiet:
                      sys.stderr.write('--- forcing DTR {}\n'.format('active' if args.dtr else 'inactive'))
                  serial_instance.dtr = args.dtr
              if args.rts is not None:
                  if not args.quiet:
                      sys.stderr.write('--- forcing RTS {}\n'.format('active' if args.rts else 'inactive'))
                  serial_instance.rts = args.rts
  
              serial_instance.open()
          except serial.SerialException as e:
              sys.stderr.write('could not open port {}: {}\n'.format(repr(args.port), e))
              if args.develop:
                  raise
              if not args.ask:
                  sys.exit(1)
              else:
                  args.port = '-'
          else:
              break
  
      miniterm = Miniterm(
          serial_instance,
          echo=args.echo,
          eol=args.eol.lower(),
          filters=filters)
      miniterm.exit_character = unichr(args.exit_char)
      miniterm.menu_character = unichr(args.menu_char)
      miniterm.raw = args.raw
      miniterm.set_rx_encoding(args.serial_port_encoding)
      miniterm.set_tx_encoding(args.serial_port_encoding)
  
      if not args.quiet:
          sys.stderr.write('--- Miniterm on {p.name}  {p.baudrate},{p.bytesize},{p.parity},{p.stopbits} ---\n'.format(
              p=miniterm.serial))
          sys.stderr.write('--- Quit: {} | Menu: {} | Help: {} followed by {} ---\n'.format(
              key_description(miniterm.exit_character),
              key_description(miniterm.menu_character),
              key_description(miniterm.menu_character),
              key_description('\x08')))
  
      miniterm.start()
      try:
          miniterm.join(True)
      except KeyboardInterrupt:
          pass
      if not args.quiet:
          sys.stderr.write("\n--- exit ---\n")
      miniterm.join()
      miniterm.close()
function_tokens: def main ( default_port = None , default_baudrate = 9600 , default_rts = None , default_dtr = None ) : import argparse parser = argparse . ArgumentParser ( description = "Miniterm - A simple terminal program for the serial port." ) parser . add_argument ( "port" , nargs = '?' , help = "serial port name ('-' to show port list)" , default = default_port ) parser . add_argument ( "baudrate" , nargs = '?' , type = int , help = "set baud rate, default: %(default)s" , default = default_baudrate ) group = parser . add_argument_group ( "port settings" ) group . add_argument ( "--parity" , choices = [ 'N' , 'E' , 'O' , 'S' , 'M' ] , type = lambda c : c . upper ( ) , help = "set parity, one of {N E O S M}, default: N" , default = 'N' ) group . add_argument ( "--rtscts" , action = "store_true" , help = "enable RTS/CTS flow control (default off)" , default = False ) group . add_argument ( "--xonxoff" , action = "store_true" , help = "enable software flow control (default off)" , default = False ) group . add_argument ( "--rts" , type = int , help = "set initial RTS line state (possible values: 0, 1)" , default = default_rts ) group . add_argument ( "--dtr" , type = int , help = "set initial DTR line state (possible values: 0, 1)" , default = default_dtr ) group . add_argument ( "--ask" , action = "store_true" , help = "ask again for port when open fails" , default = False ) group = parser . add_argument_group ( "data handling" ) group . add_argument ( "-e" , "--echo" , action = "store_true" , help = "enable local echo (default off)" , default = False ) group . add_argument ( "--encoding" , dest = "serial_port_encoding" , metavar = "CODEC" , help = "set the encoding for the serial port (e.g. hexlify, Latin1, UTF-8), default: %(default)s" , default = 'UTF-8' ) group . add_argument ( "-f" , "--filter" , action = "append" , metavar = "NAME" , help = "add text transformation" , default = [ ] ) group . add_argument ( "--eol" , choices = [ 'CR' , 'LF' , 'CRLF' ] , type = lambda c : c . upper ( ) , help = "end of line mode" , default = 'CRLF' ) group . add_argument ( "--raw" , action = "store_true" , help = "Do no apply any encodings/transformations" , default = False ) group = parser . add_argument_group ( "hotkeys" ) group . add_argument ( "--exit-char" , type = int , metavar = 'NUM' , help = "Unicode of special character that is used to exit the application, default: %(default)s" , default = 0x1d ) # GS/CTRL+] group . add_argument ( "--menu-char" , type = int , metavar = 'NUM' , help = "Unicode code of special character that is used to control miniterm (menu), default: %(default)s" , default = 0x14 ) # Menu: CTRL+T group = parser . add_argument_group ( "diagnostics" ) group . add_argument ( "-q" , "--quiet" , action = "store_true" , help = "suppress non-error messages" , default = False ) group . add_argument ( "--develop" , action = "store_true" , help = "show Python traceback on error" , default = False ) args = parser . parse_args ( ) if args . menu_char == args . exit_char : parser . error ( '--exit-char can not be the same as --menu-char' ) if args . filter : if 'help' in args . filter : sys . stderr . write ( 'Available filters:\n' ) sys . stderr . write ( '\n' . join ( '{:<10} = {.__doc__}' . format ( k , v ) for k , v in sorted ( TRANSFORMATIONS . items ( ) ) ) ) sys . stderr . write ( '\n' ) sys . exit ( 1 ) filters = args . filter else : filters = [ 'default' ] while True : # no port given on command line -> ask user now if args . port is None or args . port == '-' : try : args . port = ask_for_port ( ) except KeyboardInterrupt : sys . stderr . write ( '\n' ) parser . error ( 'user aborted and port is not given' ) else : if not args . port : parser . error ( 'port is not given' ) try : serial_instance = serial . serial_for_url ( args . port , args . baudrate , parity = args . parity , rtscts = args . rtscts , xonxoff = args . xonxoff , do_not_open = True ) if not hasattr ( serial_instance , 'cancel_read' ) : # enable timeout for alive flag polling if cancel_read is not available serial_instance . timeout = 1 if args . dtr is not None : if not args . quiet : sys . stderr . write ( '--- forcing DTR {}\n' . format ( 'active' if args . dtr else 'inactive' ) ) serial_instance . dtr = args . dtr if args . rts is not None : if not args . quiet : sys . stderr . write ( '--- forcing RTS {}\n' . format ( 'active' if args . rts else 'inactive' ) ) serial_instance . rts = args . rts serial_instance . open ( ) except serial . SerialException as e : sys . stderr . write ( 'could not open port {}: {}\n' . format ( repr ( args . port ) , e ) ) if args . develop : raise if not args . ask : sys . exit ( 1 ) else : args . port = '-' else : break miniterm = Miniterm ( serial_instance , echo = args . echo , eol = args . eol . lower ( ) , filters = filters ) miniterm . exit_character = unichr ( args . exit_char ) miniterm . menu_character = unichr ( args . menu_char ) miniterm . raw = args . raw miniterm . set_rx_encoding ( args . serial_port_encoding ) miniterm . set_tx_encoding ( args . serial_port_encoding ) if not args . quiet : sys . stderr . write ( '--- Miniterm on {p.name}  {p.baudrate},{p.bytesize},{p.parity},{p.stopbits} ---\n' . format ( p = miniterm . serial ) ) sys . stderr . write ( '--- Quit: {} | Menu: {} | Help: {} followed by {} ---\n' . format ( key_description ( miniterm . exit_character ) , key_description ( miniterm . menu_character ) , key_description ( miniterm . menu_character ) , key_description ( '\x08' ) ) ) miniterm . start ( ) try : miniterm . join ( True ) except KeyboardInterrupt : pass if not args . quiet : sys . stderr . write ( "\n--- exit ---\n" ) miniterm . join ( ) miniterm . close ( )