diff options
author | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2008-06-16 22:00:14 +0000 |
---|---|---|
committer | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2008-06-16 22:00:14 +0000 |
commit | 9c592b3a8d2b044f43771677abd369cf868e2ce9 (patch) | |
tree | 81dbe5420ecb1c18d2b5cbbb155508395f7fab2d | |
parent | d5d5198c756e7545fdeaa3e7011d6333dbcc7992 (diff) | |
download | pyserial-git-9c592b3a8d2b044f43771677abd369cf868e2ce9.tar.gz |
- update version
- update miniterm.py (patch from Colin)
- make exit and upload chars configurable
-rw-r--r-- | pyserial/CHANGES.txt | 5 | ||||
-rw-r--r-- | pyserial/examples/miniterm.py | 122 | ||||
-rw-r--r-- | pyserial/serial/__init__.py | 2 |
3 files changed, 103 insertions, 26 deletions
diff --git a/pyserial/CHANGES.txt b/pyserial/CHANGES.txt index c08852f..3dee174 100644 --- a/pyserial/CHANGES.txt +++ b/pyserial/CHANGES.txt @@ -244,9 +244,14 @@ New Features: - ``sendBreak()`` accepts a ``duration`` argument. Default duration increased. +- miniterm.py has a new feature to send a file (upload) and configurable + special characters for exit and upload. Refactored internals to class based + structure (upload and class refactoring by Colin D Bennett) + Bugfixes: - [Bug 1451535] TCP/serial redirect example "--help" +- update VERSION variable Bugfixes (posix): diff --git a/pyserial/examples/miniterm.py b/pyserial/examples/miniterm.py index 06f9576..613af3d 100644 --- a/pyserial/examples/miniterm.py +++ b/pyserial/examples/miniterm.py @@ -11,35 +11,64 @@ import sys, os, serial, threading EXITCHARCTER = '\x1d' #GS/ctrl+] +UPLOADCHARACTER = '\x15' # Upload: ctrl+u #first choose a platform dependant way to read single characters from the console +global console + if os.name == 'nt': import msvcrt - def getkey(): - while 1: - z = msvcrt.getch() - if z == '\0' or z == '\xe0': #functions keys - msvcrt.getch() - else: - if z == '\r': - return '\n' - return z + class Console: + def __init__(self): + pass + + def setup(self): + pass # Do nothing for 'nt' + def cleanup(self): + pass # Do nothing for 'nt' + + def getkey(): + while 1: + z = msvcrt.getch() + if z == '\0' or z == '\xe0': #functions keys + msvcrt.getch() + else: + if z == '\r': + return '\n' + return z + + console = Console() + elif os.name == 'posix': import termios, sys, os - fd = sys.stdin.fileno() - old = termios.tcgetattr(fd) - new = termios.tcgetattr(fd) - new[3] = new[3] & ~termios.ICANON & ~termios.ECHO & ~termios.ISIG - new[6][termios.VMIN] = 1 - new[6][termios.VTIME] = 0 - termios.tcsetattr(fd, termios.TCSANOW, new) - def getkey(): - c = os.read(fd, 1) - return c - def clenaup_console(): - termios.tcsetattr(fd, termios.TCSAFLUSH, old) - sys.exitfunc = clenaup_console #terminal modes have to be restored on exit... + class Console: + def __init__(self): + self.fd = sys.stdin.fileno() + + def setup(self): + self.old = termios.tcgetattr(self.fd) + new = termios.tcgetattr(self.fd) + new[3] = new[3] & ~termios.ICANON & ~termios.ECHO & ~termios.ISIG + new[6][termios.VMIN] = 1 + new[6][termios.VTIME] = 0 + termios.tcsetattr(self.fd, termios.TCSANOW, new) + #s = '' # We'll save the characters typed and add them to the pool. + + def getkey(self): + c = os.read(self.fd, 1) + return c + + def cleanup(self): + termios.tcsetattr(self.fd, termios.TCSAFLUSH, self.old) + + console = Console() + + def cleanup_console(): + console.cleanup() + + console.setup() + sys.exitfunc = cleanup_console #terminal modes have to be restored on exit... else: raise "Sorry no implementation for your platform (%s) available." % sys.platform @@ -114,21 +143,51 @@ class Miniterm: """loop and copy console->serial until EXITCHARCTER character is found""" while self.alive: try: - c = getkey() + c = console.getkey() except KeyboardInterrupt: c = '\x03' if c == EXITCHARCTER: self.stop() break # exit app + elif c == UPLOADCHARACTER: # upload text file + sys.stderr.write('\nFile to upload: ') + sys.stderr.flush() + console.cleanup() + filename = sys.stdin.readline().rstrip('\r\n') + if filename != '': + try: + file = open(filename, 'r') + sys.stderr.write('Sending file %s ' % filename) + while True: + line = file.readline().rstrip('\r\n') + if not line: + break + self.serial.write(line) + self.serial.write('\r\n') + # Wait for output buffer to drain. + self.serial.flush() + sys.stderr.write('.') # Progress indicator. + sys.stderr.write('\nFile %s sent.\n' % filename) + except IOError: + print 'Error opening file %s' % filename + console.setup() + elif c == '\n': self.serial.write(self.newline) # send newline character(s) if self.echo: - sys.stdout.write(c) #local echo is a real newline in any case + sys.stdout.write(c) # local echo is a real newline in any case else: self.serial.write(c) # send character if self.echo: sys.stdout.write(c) +def key_description(character): + """generate a readable description for a key""" + ascii_code = ord(character) + if ascii_code < 32: + return 'Ctrl+%c' % (ord('@') + ascii_code) + else: + return repr(ascii_code) def main(): @@ -181,12 +240,21 @@ Miniterm - A simple terminal program for the serial port.""") parser.add_option("-q", "--quiet", dest="quiet", action="store_true", help="suppress non error messages", default=False) + parser.add_option("", "--exit-char", dest="exit_char", action="store", type='int', + help="ASCII code of special charcter that is used to exit the application", default=0x1d) + + parser.add_option("", "--upload-char", dest="upload_char", action="store", type='int', + help="ASCII code of special charcter that is used to send a file", default=0x15) (options, args) = parser.parse_args() if options.cr and options.lf: parser.error("ony one of --cr or --lf can be specified") + global EXITCHARCTER, UPLOADCHARACTER + EXITCHARCTER = chr(options.exit_char) + UPLOADCHARACTER = chr(options.upload_char) + port = options.port baudrate = options.baudrate if args: @@ -226,13 +294,17 @@ Miniterm - A simple terminal program for the serial port.""") sys.exit(1) if not options.quiet: - sys.stderr.write('--- Miniterm on %s: %d,%s,%s,%s. Type Ctrl-] to quit. ---\n' % ( + sys.stderr.write('--- Miniterm on %s: %d,%s,%s,%s. ---\n' % ( miniterm.serial.portstr, miniterm.serial.baudrate, miniterm.serial.bytesize, miniterm.serial.parity, miniterm.serial.stopbits, )) + sys.stderr.write('--- Quit: %s | Upload: %s ---\n' % ( + key_description(EXITCHARCTER), + key_description(UPLOADCHARACTER) + )) if options.dtr_state is not None: if not options.quiet: sys.stderr.write('--- forcing DTR %s\n' % (options.dtr_state and 'active' or 'inactive')) diff --git a/pyserial/serial/__init__.py b/pyserial/serial/__init__.py index ac916bb..d9d5e75 100644 --- a/pyserial/serial/__init__.py +++ b/pyserial/serial/__init__.py @@ -6,7 +6,7 @@ # this is distributed under a free software license, see license.txt import sys, os, string -VERSION = string.split("$Revision: 1.3 $")[1] #extract CVS version +VERSION = '2.3' #chose an implementation, depending on os if os.name == 'nt': #sys.platform == 'win32': |