Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions baudrate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import sys
import time
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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 <serial port> Specify the serial port to use [/dev/ttyUSB0]"
print "\t-t <seconds> Set the timeout period used when switching baudrates in auto detect mode [%d]" % baud.READ_TIMEOUT
print "\t-c <num> Set the minimum ASCII character threshold used during auto detect mode [%d]" % baud.MIN_CHAR_COUNT
print "\t-n <name> Save the resulting serial configuration as <name> 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 <serial port> Specify the serial port to use [/dev/ttyUSB0]")
print("\t-t <seconds> Set the timeout period used when switching baudrates in auto detect mode [%d]" % baud.READ_TIMEOUT)
print("\t-c <num> Set the minimum ASCII character threshold used during auto detect mode [%d]" % baud.MIN_CHAR_COUNT)
print("\t-n <name> Save the resulting serial configuration as <name> 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():
Expand All @@ -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:
Expand All @@ -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

Expand Down