Skip to content

Commit

Permalink
Update BwE_UART_Reader.py
Browse files Browse the repository at this point in the history
  • Loading branch information
BetterWayElectronics authored Oct 2, 2023
1 parent b15cf0c commit dc5efca
Showing 1 changed file with 59 additions and 24 deletions.
83 changes: 59 additions & 24 deletions BwE_UART_Reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
import datetime
import re
import os
import keyboard
import pygetwindow as gw
from colorama import init, Fore, Style
import ctypes

def set_window_title(title):
ctypes.windll.kernel32.SetConsoleTitleA(title)

set_window_title('BwE UART Reader')

# UART Reader Banner
bwe = """
__________ ___________
\______ \__ _ _\_ _____/
Expand All @@ -15,18 +24,15 @@
|______ / \/\_/ /_______ /
\/ UART Reader \/
"""

print(Fore.CYAN + Style.BRIGHT + bwe + Style.RESET_ALL)

# Search for available ports
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])

auto_ports = [port[0] for port in ports if pattern.search(port[1])]

# Prompt user for port selection or auto-select if only one is found
if auto_ports:
if len(auto_ports) > 1:
print("Multiple UART Devices Found At " + ", ".join(auto_ports) + "\n")
Expand All @@ -35,32 +41,56 @@
port = 'COM' + digits[:2]
print("\nOpening " + port + "...\n")
else:
print("UART Reader " + port[1] + " Found. Opening " + auto_ports[0] + "...\n")
port = auto_ports[0]
print("UART Reader {} Found. Opening {}...\n".format(port[1], 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...")
print(Fore.GREEN + "\nError: No Port Specified. Exiting program." + Style.RESET_ALL)
print("\nPress Enter to Exit...")
raw_input()
sys.exit(1)

baud = 115200

sread = serial.Serial()
# Set up serial connection
baud = 115200
sread = serial.Serial(timeout=0.1) # 0.1 seconds timeout
sread.port = port
sread.baudrate = baud

try:
sread.open()
except:
sys.stderr.write("\nError Opening COM Port %s. Press Enter to Exit." % sread.portstr)
sys.stderr.write("Error Opening COM Port %s. Press Enter to Exit." % sread.portstr)
raw_input()
sys.exit(1)

# Global running flag
running = True

def on_key_event(e):
global running

# Get the title of the active window
active_window_title = gw.getActiveWindow().title

# Check if the active window title matches the title of the Python script's window
if 'BwE UART Reader' in active_window_title:
if e.name == 'enter' and e.event_type == keyboard.KEY_DOWN: # Enter key
#print(Fore.RED + "\nExiting..." + Style.RESET_ALL)
running = False
elif e.name == 'space' and e.event_type == keyboard.KEY_DOWN: # Space key
print(Fore.YELLOW + "\nClearing Screen..." + Style.RESET_ALL)
time.sleep(1)
os.system('cls')
print(Fore.CYAN + Style.BRIGHT + bwe2 + Style.RESET_ALL)

# Register the key event handler
keyboard.hook(on_key_event)

# UART Reader Banner 2
bwe2 = """
__________ ___________
\______ \__ _ _\_ _____/
Expand All @@ -73,23 +103,28 @@
os.system('cls')
print(Fore.CYAN + Style.BRIGHT + bwe2 + Style.RESET_ALL)

# Create file for logging UART data
now = datetime.datetime.now()
filename = "uart_data_{}-{}-{}_-_{}-{}-{}.txt".format(now.year, now.month, now.day, now.hour, now.minute, now.second)

# Main loop: read from serial port, write to file, print to console
with open(filename, "w") as f:
try:
while 1:
while running:
data = sread.read(1)
n = sread.inWaiting()
if n:
data = data + sread.read(n)
sys.stdout.write(data)
f.write(data)

if data: # Only proceed if data is not an empty string
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)
keyboard.unhook_all() # Unhook all keyboard event handlers
print(Fore.GREEN + "\nProgram Interrupted by User. Log Saved As " + filename + Style.RESET_ALL)
sys.stdout.flush()
raw_input("\nPress Enter to Exit.")
sys.exit(1)

0 comments on commit dc5efca

Please sign in to comment.