Added improved handling of CR/LF in PYTERM.

The default behaviour only appreciates the LF character (unix style).
The pyterm switch -nl/--newline can be used to specify the newline combination of CR/NL.
Possible values are CR, NL, CRNL and NLCR. Default is NL.
This commit is contained in:
Janos Kutscherauer 2015-03-12 20:32:17 +01:00 committed by Oleg Hahm
parent 911bf20749
commit 6c620f273d

View File

@ -68,6 +68,8 @@ defaultrunname = "default-run"
# default logging prefix format string # default logging prefix format string
default_fmt_str = '%(asctime)s - %(levelname)s # %(message)s' default_fmt_str = '%(asctime)s - %(levelname)s # %(message)s'
defaultnewline = "LF"
class SerCmd(cmd.Cmd): class SerCmd(cmd.Cmd):
"""Main class for pyterm based on Python's Cmd class. """Main class for pyterm based on Python's Cmd class.
@ -78,7 +80,7 @@ class SerCmd(cmd.Cmd):
def __init__(self, port=None, baudrate=None, toggle=None, tcp_serial=None, def __init__(self, port=None, baudrate=None, toggle=None, tcp_serial=None,
confdir=None, conffile=None, host=None, run_name=None, confdir=None, conffile=None, host=None, run_name=None,
log_dir_name=None): log_dir_name=None, newline=None):
"""Constructor. """Constructor.
Args: Args:
@ -103,6 +105,7 @@ class SerCmd(cmd.Cmd):
self.host = host self.host = host
self.run_name = run_name self.run_name = run_name
self.log_dir_name = log_dir_name self.log_dir_name = log_dir_name
self.newline = newline
if not self.host: if not self.host:
self.host = defaulthostname self.host = defaulthostname
@ -568,6 +571,8 @@ class SerCmd(cmd.Cmd):
"""Serial or TCP reader. """Serial or TCP reader.
""" """
output = "" output = ""
crreceived = False
nlreceived = False
while (1): while (1):
# check if serial port can be accessed. # check if serial port can be accessed.
try: try:
@ -585,13 +590,19 @@ class SerCmd(cmd.Cmd):
% (self.port)) % (self.port))
self.serial_connect() self.serial_connect()
continue continue
if c == '\n' or c == '\r': if c == '\r':
if (self.newline == "LFCR" and nlreceived) or (self.newline == "CR"):
self.handle_line(output)
output = ""
elif c == '\n':
if (self.newline == "CRLF" and crreceived) or (self.newline == "LF"):
self.handle_line(output) self.handle_line(output)
output = "" output = ""
else: else:
output += c output += c
# sys.stdout.write(c)
# sys.stdout.flush() crreceived = c == '\r'
nlreceived = c == '\n'
class PytermProt(Protocol): class PytermProt(Protocol):
@ -696,11 +707,16 @@ if __name__ == "__main__":
help="Log directory name (default is hostname e.g. " help="Log directory name (default is hostname e.g. "
"%s/<hostname>)" % defaultdir, "%s/<hostname>)" % defaultdir,
default=defaultdir) default=defaultdir)
parser.add_argument("-nl", "--newline",
help="Specify the newline character(s) as a combination "
"of CR and LF. Examples: -nl=LF, -nl=CRLF. "
"(Default is %s)" % defaultnewline,
default=defaultnewline)
args = parser.parse_args() args = parser.parse_args()
myshell = SerCmd(args.port, args.baudrate, args.toggle, args.tcp_serial, myshell = SerCmd(args.port, args.baudrate, args.toggle, args.tcp_serial,
args.directory, args.config, args.host, args.run_name, args.directory, args.config, args.host, args.run_name,
args.log_dir_name) args.log_dir_name, args.newline)
myshell.prompt = '' myshell.prompt = ''
if args.server and args.tcp_port: if args.server and args.tcp_port: