diff options
Diffstat (limited to 'pyserial/examples/miniterm.py')
-rw-r--r-- | pyserial/examples/miniterm.py | 89 |
1 files changed, 73 insertions, 16 deletions
diff --git a/pyserial/examples/miniterm.py b/pyserial/examples/miniterm.py index cc5a0bf..829a454 100644 --- a/pyserial/examples/miniterm.py +++ b/pyserial/examples/miniterm.py @@ -3,12 +3,11 @@ #very simple serial terminal #input characters are sent directly, received characters are displays as is #baudrate and echo configuartion is done through globals: -baudrate = 9600 -echo = 1 -convert_outgoing_cr = 1 -import sys, os, serial, threading +import sys, os, serial, threading, getopt +#EXITCHARCTER = '\x1b' #ESC +EXITCHARCTER = '\x04' #ctrl+d #first choosea platform dependant way to read single characters from the console if os.name == 'nt': #sys.platform == 'win32': @@ -47,9 +46,6 @@ else: raise "Sorry no implementation for your platform (%s) available." % sys.platform - -s = serial.Serial(0,baudrate) - def reader(): """loop forever and copy serial->console""" while 1: @@ -59,18 +55,79 @@ def writer(): """loop forever and copy console->serial""" while 1: c = getkey() - if c == '\x1b': break #exit on esc + if c == EXITCHARCTER: break #exit on esc s.write(c) #send character if convert_outgoing_cr and c == '\r': s.write('\n') if echo: sys.stdout.write('\n') -print "--- Miniterm --- type ESC to quit" -#start serial->console thread -r = threading.Thread(target=reader) -r.setDaemon(1) -r.start() -#enter console->serial loop -writer() -print "\n--- exit ---" +#print a short help message +def usage(): + print >>sys.stderr, """USAGE: %s [options] + Simple Terminal Programm for the serial port. + + options: + -p, --port=PORT: port, a number, defualt = 0 or a device name + -b, --baud=BAUD: baudrate, default 9600 + -r, --rtscts: enable RTS/CTS flow control (default off) + -x, --xonxoff: enable software flow control (default off) + -e, --echo: enable local echo (default off) + -c, --cr: disable CR -> CR+LF translation + + """ % sys.argv[0] + +if __name__ == '__main__': + #parse command line options + try: + opts, args = getopt.getopt(sys.argv[1:], + "hp:b:rxec", + ["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr"]) + except getopt.GetoptError: + # print help information and exit: + usage() + sys.exit(2) + + port = 0 + baudrate = 9600 + echo = 0 + convert_outgoing_cr = 1 + rtscts = 0 + xonxoff = 0 + for o, a in opts: + if o in ("-h", "--help"): #help text + usage() + sys.exit() + elif o in ("-p", "--port"): #specified port + try: + port = int(a) + except ValueError: + port = a + elif o in ("-b", "--baud"): #specified baudrate + try: + baudrate = int(a) + except ValueError: + raise ValueError, "Baudrate must be a integer number" + elif o in ("-r", "--rtscts"): + rtscts = 1 + elif o in ("-x", "--xonxoff"): + xonxoff = 1 + elif o in ("-e", "--echo"): + echo = 1 + elif o in ("-c", "--cr"): + convert_outgoing_cr = 0 + + try: + s = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff) + except: + print "could not open port" + sys.exit(1) + print "--- Miniterm --- type Ctrl-D to quit" + #start serial->console thread + r = threading.Thread(target=reader) + r.setDaemon(1) + r.start() + #enter console->serial loop + writer() + + print "\n--- exit ---" |