-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUART_SerialBaudRate.py
More file actions
92 lines (79 loc) · 3.01 KB
/
Copy pathUART_SerialBaudRate.py
File metadata and controls
92 lines (79 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import serial
import time
import re
import os
# Comprehensive list of baud rates
baud_rates = [
300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400,
57600, 76800, 115200, 230400, 460800, 500000, 576000, 921600
]
# Serial port settings
port = '/dev/serial0'
timeout = 15 # Seconds to read data for each baud rate
# Patterns to detect readable output
patterns = [r'U-Boot', r'=>\s', r'login:', r'kernel', r'booting', r'root@']
def is_readable(data):
"""Check if data contains readable ASCII or specific patterns."""
try:
text = data.decode('ascii', errors='ignore')
printable_ratio = sum(c.isprintable() for c in text) / len(text) if text else 0
if printable_ratio > 0.7: # At least 70% printable characters
return True
for pattern in patterns:
if re.search(pattern, text, re.IGNORECASE):
return True
return False
except:
return False
# Ensure user has permissions
if os.geteuid() != 0:
print("Run this script with sudo to access /dev/serial0")
exit(1)
# Ensure output directory exists
output_dir = "baud_outputs"
os.makedirs(output_dir, exist_ok=True)
# Test each baud rate with power cycle prompt
for baud in baud_rates:
print(f"\nPreparing to test baud rate: {baud}")
print("Please power OFF the Device, wait 5 seconds, then power it ON.")
input("Press Enter when the Device is powered ON...")
print(f"Testing baud rate: {baud}")
try:
# Open serial port
ser = serial.Serial(
port=port,
baudrate=baud,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
timeout=1
)
# Read data for timeout seconds
start_time = time.time()
data = b''
while time.time() - start_time < timeout:
if ser.in_waiting:
data += ser.read(ser.in_waiting)
time.sleep(0.1)
# Save output to file
filename = f"{output_dir}/output_{baud}.log"
with open(filename, 'wb') as f:
f.write(data)
# Check if output is readable
if data and is_readable(data):
print(f"Possible match at {baud} baud! Output saved to {filename}")
try:
print("Sample output:", data.decode('ascii', errors='ignore')[:200])
except:
print("Sample output (raw):", data[:200])
else:
print(f"No readable output at {baud} baud. Saved to {filename}")
ser.close()
except serial.SerialException as e:
print(f"Error at {baud} baud: {e}")
except Exception as e:
print(f"Unexpected error at {baud} baud: {e}")
print(f"\nTesting complete. Check {output_dir}/output_*.log files for details.")
print(f"Use 'cat {output_dir}/output_*.log' or open files in a text editor.")
print("If a baud rate shows readable text, test it manually with:")
print(" minicom -b <baud_rate> -o -D /dev/serial0")