diff --git a/baudrate.py b/baudrate.py index e8f7667..aa75b67 100755 --- a/baudrate.py +++ b/baudrate.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import sys import time @@ -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,10 @@ def Detect(self): if self.ctlc: break - 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 +202,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 +222,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 +251,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 +278,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