From 447653fd6648142a9e94d3bd0308abe1eff2fd42 Mon Sep 17 00:00:00 2001 From: sickcodes Date: Fri, 18 Sep 2020 09:15:19 +0000 Subject: [PATCH 1/7] @Loris1123's fork to Python3 --- baudrate.py | 76 ++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/baudrate.py b/baudrate.py index e8f7667..e0e281b 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,46 +278,47 @@ 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 baud.Close() - main() + main() \ No newline at end of file From 0618ff8c6ecbb8a7307eec63ddd34e13bc7e178d Mon Sep 17 00:00:00 2001 From: sickcodes Date: Fri, 18 Sep 2020 09:57:38 +0000 Subject: [PATCH 2/7] Tidy up --- README.md | 23 +++++++++++++++++++ baudrate.py | 58 ++++++++++++++++++++++++++---------------------- requirements.txt | 2 ++ 3 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 README.md create mode 100644 requirements.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..e8cf2e5 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Baudrate Python 3 - fork of baudrate + +Forked and cherry-picked @Loris1123 commit because he deleted his repo. + +Added requirements.txt + +See PR: https://github.com/devttys0/baudrate/pull/4#commits-pushed-0eb5f36 + + +# Credits + +Original author Craig Heffner @devttys0: https://github.com/devttys0/baudrate + +Upgraded to Python3 by Moritz Lottermann @Loris1123: https://github.com/Loris1123 + +Modernized by Sick.Codes @sickcodes : https://github.com/sickcodes + +# Links + +https://twitter.com/sickcodes + +https://sick.codes + diff --git a/baudrate.py b/baudrate.py index e0e281b..638d999 100755 --- a/baudrate.py +++ b/baudrate.py @@ -1,9 +1,22 @@ #!/usr/bin/env python3 +# License: MIT +# Authors: +# Craig Heffner @devttys0 https://github.com/devttys0 +# Moritz Lottermann @Loris1123 https://github.com/Loris1123 +# Sick.Codes @sickcodes https://github.com/sickcodes +# Usage: +# pip install -r requirements.txt +# sudo python baudrate.py /dev/ttyUSB0 import sys import time import serial from threading import Thread +import tty +import termios +import getch +import subprocess +from getopt import getopt as GetOpt, GetoptError class RawInput: """Gets a single character from standard input. Does not echo to the screen.""" @@ -17,11 +30,7 @@ def __call__(self): return self.impl() class RawInputUnix: - def __init__(self): - import tty, sys - def __call__(self): - import sys, tty, termios fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: @@ -33,22 +42,18 @@ def __call__(self): class RawInputWindows: - def __init__(self): - import msvcrt - def __call__(self): - import msvcrt - return msvcrt.getch() + return getch.getch() class Baudrate: - VERSION = '1.0' + VERSION = '3.0' READ_TIMEOUT = 5 BAUDRATES = [ -# "1200", -# "1800", -# "2400", -# "4800", + "1200", + "1800", + "2400", + "4800", "9600", "38400", "19200", @@ -95,6 +100,7 @@ def _print(self, data): sys.stderr.buffer.flush() def Open(self): + pass self.serial = serial.Serial(self.port, timeout=self.timeout) self.NextBaudrate(0) @@ -216,26 +222,25 @@ def Close(self): if __name__ == '__main__': - import subprocess - from getopt import getopt as GetOpt, GetoptError def usage(): baud = Baudrate() - print("") print("Baudrate v%s" % baud.VERSION) print("Craig Heffner, http://www.devttys0.com") + print("Moritz Lottermann, https://github.com/Loris1123") + print("Sick.Codes, https://sick.codes") 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("\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) @@ -321,4 +326,5 @@ def main(): baud.Close() - main() \ No newline at end of file + main() + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..008c8fd --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pySerial +getch \ No newline at end of file From b6770d0f38b0d7131372a4500953eee78f579910 Mon Sep 17 00:00:00 2001 From: sickcodes Date: Wed, 23 Sep 2020 08:46:42 +0000 Subject: [PATCH 3/7] Python3 --- README.md | 2 +- baudrate.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e8cf2e5..c3baf32 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ See PR: https://github.com/devttys0/baudrate/pull/4#commits-pushed-0eb5f36 Original author Craig Heffner @devttys0: https://github.com/devttys0/baudrate -Upgraded to Python3 by Moritz Lottermann @Loris1123: https://github.com/Loris1123 +Upgraded to Python3 by @Loris1123: https://github.com/Loris1123 Modernized by Sick.Codes @sickcodes : https://github.com/sickcodes diff --git a/baudrate.py b/baudrate.py index 638d999..00ca797 100755 --- a/baudrate.py +++ b/baudrate.py @@ -2,7 +2,7 @@ # License: MIT # Authors: # Craig Heffner @devttys0 https://github.com/devttys0 -# Moritz Lottermann @Loris1123 https://github.com/Loris1123 +# @Loris1123 https://github.com/Loris1123 # Sick.Codes @sickcodes https://github.com/sickcodes # Usage: # pip install -r requirements.txt @@ -228,7 +228,7 @@ def usage(): print("Baudrate v%s" % baud.VERSION) print("Craig Heffner, http://www.devttys0.com") - print("Moritz Lottermann, https://github.com/Loris1123") + print("@Loris1123, https://github.com/Loris1123") print("Sick.Codes, https://sick.codes") print("") print("Usage: %s [OPTIONS]" % sys.argv[0]) From 558e5ecc5523bff601e96839d8474620530f417d Mon Sep 17 00:00:00 2001 From: sickcodes Date: Thu, 22 Oct 2020 08:46:31 +0000 Subject: [PATCH 4/7] move getch into the try/except for windows --- baudrate.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/baudrate.py b/baudrate.py index 00ca797..e466ea2 100755 --- a/baudrate.py +++ b/baudrate.py @@ -14,7 +14,6 @@ from threading import Thread import tty import termios -import getch import subprocess from getopt import getopt as GetOpt, GetoptError @@ -22,6 +21,7 @@ class RawInput: """Gets a single character from standard input. Does not echo to the screen.""" def __init__(self): try: + import getch self.impl = RawInputWindows() except ImportError: self.impl = RawInputUnix() @@ -48,17 +48,30 @@ def __call__(self): class Baudrate: VERSION = '3.0' - READ_TIMEOUT = 5 + READ_TIMEOUT = 1 BAUDRATES = [ - "1200", - "1800", - "2400", - "4800", - "9600", - "38400", - "19200", - "57600", - "115200", + "110", + "300", + "600", + "1200", + "1800", + "2400", + "4800", + "9600", + "14400", + "19200", + "38400", + "57600", + "115200", + "128000", + "230400", + "250000", + "256000", + "460800", + "500000", + "921600", + "1000000", + "2000000", ] UPKEYS = ['u', 'U', 'A'] From 8df60b979d53142582bd76ac4f3464bd438cdd15 Mon Sep 17 00:00:00 2001 From: sickcodes Date: Sat, 24 Oct 2020 09:18:41 +0000 Subject: [PATCH 5/7] Auto timeout 1, getch back --- baudrate.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/baudrate.py b/baudrate.py index e466ea2..1f8646a 100755 --- a/baudrate.py +++ b/baudrate.py @@ -16,12 +16,12 @@ import termios import subprocess from getopt import getopt as GetOpt, GetoptError +import getch class RawInput: """Gets a single character from standard input. Does not echo to the screen.""" def __init__(self): try: - import getch self.impl = RawInputWindows() except ImportError: self.impl = RawInputUnix() @@ -48,7 +48,7 @@ def __call__(self): class Baudrate: VERSION = '3.0' - READ_TIMEOUT = 1 + READ_TIMEOUT = 5 BAUDRATES = [ "110", "300", @@ -263,7 +263,7 @@ def main(): auto = False run = False threshold = 25 - timeout = 5 + timeout = 1 name = None port = '/dev/ttyUSB0' From f89dd3bf76e6a6b7e77544831a4402f0b8867aec Mon Sep 17 00:00:00 2001 From: sickcodes Date: Tue, 3 Nov 2020 16:32:47 +0000 Subject: [PATCH 6/7] Added some baud rates and install instructions --- LICENSE | 20 ++++++++++++++++++++ README.md | 13 ++++++++++++- baudrate.py | 6 ++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/LICENSE b/LICENSE index 6a57464..3040860 100644 --- a/LICENSE +++ b/LICENSE @@ -20,3 +20,23 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +Copyright (c) 2020 sickcodes + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md index c3baf32..83ed9d6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,16 @@ # Baudrate Python 3 - fork of baudrate +```bash +git clone https://github.com/sickcodes/python3-baudrate +cd python3-baudrate +pipenv run pip install -r requirements.txt +pipenv run sudo python baudrate.py -a + +# or without pipenv +# pip install -r requirements.txt +# sudo python baudrate.py +``` + Forked and cherry-picked @Loris1123 commit because he deleted his repo. Added requirements.txt @@ -15,7 +26,7 @@ Upgraded to Python3 by @Loris1123: https://github.com/Loris1123 Modernized by Sick.Codes @sickcodes : https://github.com/sickcodes -# Links +# Follow for Updates https://twitter.com/sickcodes diff --git a/baudrate.py b/baudrate.py index 1f8646a..7f70b36 100755 --- a/baudrate.py +++ b/baudrate.py @@ -60,6 +60,7 @@ class Baudrate: "9600", "14400", "19200", + "28800", "38400", "57600", "115200", @@ -70,8 +71,9 @@ class Baudrate: "460800", "500000", "921600", - "1000000", - "2000000", + "2500000", + "3000000", + "3686400", ] UPKEYS = ['u', 'U', 'A'] From 68b7eefae9d79d3827808d32c462df21417950cf Mon Sep 17 00:00:00 2001 From: sickcodes Date: Tue, 29 Jun 2021 16:37:28 +0000 Subject: [PATCH 7/7] Add more obscure baudrates. --- baudrate.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/baudrate.py b/baudrate.py index 7f70b36..e52a089 100755 --- a/baudrate.py +++ b/baudrate.py @@ -56,21 +56,31 @@ class Baudrate: "1200", "1800", "2400", + "3600", "4800", + "7200", "9600", "14400", "19200", "28800", + "31250", "38400", "57600", + "76800", "115200", "128000", + "153600", "230400", "250000", "256000", + "307200", + "345600", "460800", "500000", + "512000", "921600", + "1024000", + "2000000", "2500000", "3000000", "3686400",