Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# baudrate

## Usage

```
Baudrate v1.1
Craig Heffner, http://www.devttys0.com

Usage: baudrate.py [OPTIONS]

-p <serial port> Specify the serial port to use [/dev/ttyUSB0]
-t <millisecond> Set the timeout period used when switching baudrates in auto detect mode [1]
-c <num> Set the minimum ASCII character threshold used during auto detect mode [25]
-n <name> Save the resulting serial configuration as <name> 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)
61 changes: 37 additions & 24 deletions baudrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -230,7 +243,7 @@ def usage():
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-t <millisecond> 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"
Expand All @@ -246,7 +259,7 @@ def main():
auto = False
run = False
threshold = 25
timeout = 5
timeout = 3
name = None
port = '/dev/ttyUSB0'

Expand All @@ -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':
Expand Down
Binary file added detect.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.