diff --git a/README.md b/README.md new file mode 100644 index 0000000..80303dd --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# baudrate + +## Usage + +``` +Baudrate v1.1 +Craig Heffner, http://www.devttys0.com + +Usage: baudrate.py [OPTIONS] + + -p Specify the serial port to use [/dev/ttyUSB0] + -t Set the timeout period used when switching baudrates in auto detect mode [1] + -c Set the minimum ASCII character threshold used during auto detect mode [25] + -n Save the resulting serial configuration as and automatically invoke minicom (implies -a) + -a Enable auto detect mode + -b Display supported baud rates and exit + -q Do not display data read from the serial port + -h Display help +``` + +### Auto Detect On Linux + +```bash +sudo python2 baudrate.py -a -t 200 +``` + +### Auto Detect On Windows + +``` +python2 baudrate.py -a -t 200 -p COM1 +``` + +![](detect.gif) \ No newline at end of file diff --git a/baudrate.py b/baudrate.py index e8f7667..9f81676 100755 --- a/baudrate.py +++ b/baudrate.py @@ -42,18 +42,28 @@ def __call__(self): class Baudrate: - VERSION = '1.0' - READ_TIMEOUT = 5 + VERSION = '1.1' + READ_TIMEOUT = 1 BAUDRATES = [ -# "1200", -# "1800", -# "2400", -# "4800", + "1200", + "1800", + "2400", + "4800", "9600", - "38400", "19200", + "38400", "57600", "115200", + "230400", + "256000", + "460800", + "691200", + "921600", + "1036800", + "1036800", + "1152000", + #"1382400", + #"1843200", ] UPKEYS = ['u', 'U', 'A'] @@ -97,19 +107,23 @@ def Open(self): self.serial = serial.Serial(self.port, timeout=self.timeout) self.NextBaudrate(0) - def NextBaudrate(self, updn): - - self.index += updn + def NextBaudrate(self, updn, accurate=False): + if accurate == True: + self.serial.baudrate += updn * 1200 + self.serial.flush() + sys.stderr.write('\n\n@@@@@@@@@@@@@@@@@@@@@ Baudrate: %s @@@@@@@@@@@@@@@@@@@@@\n\n' % self.serial.baudrate) + else: + self.index += updn - if self.index >= len(self.BAUDRATES): - self.index = 0 - elif self.index < 0: - self.index = len(self.BAUDRATES) - 1 + if self.index >= len(self.BAUDRATES): + self.index = 0 + elif self.index < 0: + self.index = len(self.BAUDRATES) - 1 - sys.stderr.write('\n\n@@@@@@@@@@@@@@@@@@@@@ Baudrate: %s @@@@@@@@@@@@@@@@@@@@@\n\n' % self.BAUDRATES[self.index]) + sys.stderr.write('\n\n@@@@@@@@@@@@@@@@@@@@@ Baudrate: %s @@@@@@@@@@@@@@@@@@@@@\n\n' % self.BAUDRATES[self.index]) - self.serial.flush() - self.serial.baudrate = self.BAUDRATES[self.index] + self.serial.flush() + self.serial.baudrate = self.BAUDRATES[self.index] self.serial.flush() def Detect(self): @@ -124,7 +138,7 @@ def Detect(self): if not self.auto_detect: self.thread = Thread(None, self.HandleKeypress, None, (self, 1)) self.thread.start() - + # iii = 10 while True: if start_time == 0: start_time = time.time() @@ -143,7 +157,6 @@ def Detect(self): count += 1 else: clear_counters = True - self._print(byte) if count >= self.threshold and whitespace > 0 and punctuation > 0 and vowels > 0: @@ -155,7 +168,7 @@ def Detect(self): if timed_out and self.auto_detect: start_time = 0 - self.NextBaudrate(-1) + self.NextBaudrate(-1, True) clear_counters = True timed_out = False @@ -170,7 +183,7 @@ def Detect(self): break self._print("\n") - return self.BAUDRATES[self.index] + return self.serial.baudrate def HandleKeypress(self, *args): userinput = RawInput() @@ -230,7 +243,7 @@ def usage(): 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-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" @@ -246,7 +259,7 @@ def main(): auto = False run = False threshold = 25 - timeout = 5 + timeout = 3 name = None port = '/dev/ttyUSB0' @@ -258,7 +271,7 @@ def main(): for opt, arg in opts: if opt == '-t': - timeout = int(arg) + timeout = int(arg) * 0.001 elif opt == '-c': threshold = int(arg) elif opt == '-p': diff --git a/detect.gif b/detect.gif new file mode 100644 index 0000000..07e443e Binary files /dev/null and b/detect.gif differ