forked from BetterWayElectronics/BwE-UART-Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBwE_UART_Reader.py
95 lines (80 loc) · 2.56 KB
/
BwE_UART_Reader.py
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
93
94
95
import sys
import serial
import serial.tools.list_ports
import time
import datetime
import re
import os
from colorama import init, Fore, Style
bwe = """
__________ ___________
\______ \__ _ _\_ _____/
| | _/\ \/ \/ /| __)_
| | \ \ / | \\
|______ / \/\_/ /_______ /
\/ UART Reader \/
"""
print(Fore.CYAN + Style.BRIGHT + bwe + Style.RESET_ALL)
pattern = re.compile(r"^(USB-?Serial|USB Serial)\b", re.IGNORECASE)
ports = list(serial.tools.list_ports.comports())
auto_ports = []
for port in ports:
if pattern.search(port[1]):
auto_ports.append(port[0])
if auto_ports:
if len(auto_ports) > 1:
print("Multiple UART Devices Found At " + ", ".join(auto_ports) + "\n")
port = raw_input("Enter COM Port (Example COM4): ")
digits = ''.join(filter(str.isdigit, port))
port = 'COM' + digits[:2]
print("\nOpening " + port + "...\n")
else:
print("UART Reader " + port[1] + " Found. Opening " + auto_ports[0] + "...\n")
port = auto_ports[0]
else:
port = raw_input("Enter COM Port (Example COM4): ")
digits = ''.join(filter(str.isdigit, port))
port = 'COM' + digits[:2]
if not port:
print("\nError: No Port Specified. Exiting program.")
print ("\nPress Enter to Exit...")
raw_input()
sys.exit(1)
baud = 115200
sread = serial.Serial()
sread.port = port
sread.baudrate = baud
try:
sread.open()
except:
sys.stderr.write("\nError Opening COM Port %s. Press Enter to Exit." % sread.portstr)
raw_input()
sys.exit(1)
bwe2 = """
__________ ___________
\______ \__ _ _\_ _____/
| | _/\ \/ \/ /| __)_
| | \ \ / | \\
|______ / \/\_/ /_______ /
\/ UART Reader \/ {} Opened...
""".format(port)
os.system('cls')
print(Fore.CYAN + Style.BRIGHT + bwe2 + Style.RESET_ALL)
now = datetime.datetime.now()
filename = "uart_data_{}-{}-{}_-_{}-{}-{}.txt".format(now.year, now.month, now.day, now.hour, now.minute, now.second)
with open(filename, "w") as f:
try:
while 1:
data = sread.read(1)
n = sread.inWaiting()
if n:
data = data + sread.read(n)
sys.stdout.write(data)
f.write(data)
except KeyboardInterrupt:
pass
finally:
sread.close()
print("\n\nProgram Interrupted by User. Log Saved As " + filename + "\n\nPress Enter to Exit.")
raw_input()
sys.exit(1)