From 0eb5f3691849d24a44017789cb1399752fec38d1 Mon Sep 17 00:00:00 2001 From: Moritz Lottermann Date: Thu, 4 Apr 2019 09:55:41 +0200 Subject: [PATCH 1/3] Baudrate.py running with python3 --- baudrate.py | 73 +++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/baudrate.py b/baudrate.py index e8f7667..f2167d8 100755 --- a/baudrate.py +++ b/baudrate.py @@ -91,7 +91,8 @@ def _gen_char_list(self): def _print(self, data): if self.verbose: - sys.stderr.write(data) + sys.stderr.buffer.write(data) + sys.stderr.buffer.flush() def Open(self): self.serial = serial.Serial(self.port, timeout=self.timeout) @@ -169,12 +170,11 @@ def Detect(self): if self.ctlc: break - self._print("\n") + # self._print("\n") return self.BAUDRATES[self.index] def HandleKeypress(self, *args): userinput = RawInput() - while not self.ctlc: c = userinput() if c in self.UPKEYS: @@ -203,8 +203,8 @@ def MinicomConfig(self, name=None): if name is not None and name: try: open("/etc/minicom/minirc.%s" % name, "w").write(config) - except Exception, e: - print "Error saving minicom config file:", str(e) + except Exception as e: + print("Error saving minicom config file:", str(e)) success = False return (success, config) @@ -223,21 +223,21 @@ def Close(self): def usage(): baud = Baudrate() - print "" - print "Baudrate v%s" % baud.VERSION - print "Craig Heffner, http://www.devttys0.com" - print "" - print "Usage: %s [OPTIONS]" % sys.argv[0] - print "" - print "\t-p Specify the serial port to use [/dev/ttyUSB0]" - print "\t-t Set the timeout period used when switching baudrates in auto detect mode [%d]" % baud.READ_TIMEOUT - print "\t-c Set the minimum ASCII character threshold used during auto detect mode [%d]" % baud.MIN_CHAR_COUNT - print "\t-n Save the resulting serial configuration as and automatically invoke minicom (implies -a)" - print "\t-a Enable auto detect mode" - print "\t-b Display supported baud rates and exit" - print "\t-q Do not display data read from the serial port" - print "\t-h Display help" - print "" + print("") + print("Baudrate v%s" % baud.VERSION) + print("Craig Heffner, http://www.devttys0.com") + print("") + print("Usage: %s [OPTIONS]" % sys.argv[0]) + print("") + print("\t-p Specify the serial port to use [/dev/ttyUSB0]") + print("\t-t Set the timeout period used when switching baudrates in auto detect mode [%d]" % baud.READ_TIMEOUT) + print("\t-c Set the minimum ASCII character threshold used during auto detect mode [%d]" % baud.MIN_CHAR_COUNT) + print("\t-n Save the resulting serial configuration as and automatically invoke minicom (implies -a)") + print("\t-a Enable auto detect mode") + print("\t-b Display supported baud rates and exit") + print("\t-q Do not display data read from the serial port") + print("\t-h Display help") + print("") sys.exit(1) def main(): @@ -252,8 +252,8 @@ def main(): try: (opts, args) = GetOpt(sys.argv[1:], 'p:t:c:n:abqh') - except GetoptError, e: - print e + except GetoptError as e: + print(e) usage() for opt, arg in opts: @@ -279,43 +279,44 @@ def main(): baud = Baudrate(port, threshold=threshold, timeout=timeout, name=name, verbose=verbose, auto=auto) if display: - print "" + print("") for rate in baud.BAUDRATES: - print "\t%s" % rate - print "" + print("\t{}".format(rate)) + print("") else: - print "" - print "Starting baudrate detection on %s, turn on your serial device now." % port - print "Press Ctl+C to quit." - print "" + print("") + print("Starting baudrate detection on %s, turn on your serial device now." % port) + print("Press Up/Down to switch baudrates.") + print("Press Ctl+C to quit.") + print("") baud.Open() try: rate = baud.Detect() - print "\nDetected baudrate: %s" % rate + print("\nDetected baudrate: {}".format(rate)) if name is None: - print "\nSave minicom configuration as: ", + print("\nSave minicom configuration as: ", end=" ") name = sys.stdin.readline().strip() - print "" + print("") (ok, config) = baud.MinicomConfig(name) if name and name is not None: if ok: if not run: - print "Configuration saved. Run minicom now [n/Y]? ", + print("Configuration saved. Run minicom now [n/Y]? ", end=" ") yn = sys.stdin.readline().strip() - print "" + print("") if yn == "" or yn.lower().startswith('y'): run = True if run: subprocess.call(["minicom", name]) else: - print config + print(config) else: - print config + print(config) except KeyboardInterrupt: pass From 7b846470088cb1d2794fe9cc40b9aae440febed2 Mon Sep 17 00:00:00 2001 From: Moritz Lottermann Date: Thu, 4 Apr 2019 10:41:56 +0200 Subject: [PATCH 2/3] Use python3 as default --- baudrate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/baudrate.py b/baudrate.py index f2167d8..3eb122c 100755 --- a/baudrate.py +++ b/baudrate.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import sys import time From 1673f4a1a9a80f0406d997e5abb8be333b99817b Mon Sep 17 00:00:00 2001 From: Moritz Lottermann Date: Thu, 4 Apr 2019 10:43:46 +0200 Subject: [PATCH 3/3] Removed printline in comment --- baudrate.py | 1 - 1 file changed, 1 deletion(-) diff --git a/baudrate.py b/baudrate.py index 3eb122c..aa75b67 100755 --- a/baudrate.py +++ b/baudrate.py @@ -170,7 +170,6 @@ def Detect(self): if self.ctlc: break - # self._print("\n") return self.BAUDRATES[self.index] def HandleKeypress(self, *args):