import tkinter as tk
from tkinter import ttk
from tkinter import *
import serial.tools.list_ports
import serial
# --- Serial Frame Global variables ---
serial_connection = None
# --- Transport Frame Global variables ---
# --- Defines for Serial Frame
def connect_serial():
global serial_connection
try:
port = port_options.get()
if not port:
raise ValueError("No port selected")
if port not in [port.device for port in serial.tools.list_ports.comports()]:
raise ValueError(f"Selected port '{port}' is no longer available")
baud_rate = baud_options.get()
if not baud_rate:
raise ValueError("No baud rate selected")
serial_connection = serial.Serial(port, baud_rate)
connect_button.config(state=tk.DISABLED)
disconnect_button.config(state=tk.NORMAL)
status_label.config(text=f"Connected to {port} at {baud_rate} baud")
root.geometry("1080x720") # Set the new width and height of the window
except ValueError as ve:
status_label.config(text=str(ve))
except serial.SerialException:
status_label.config(text="Failed to connect to serial port")
def disconnect_serial():
global serial_connection
if serial_connection is not None:
serial_connection.close()
connect_button.config(state=tk.NORMAL)
disconnect_button.config(state=tk.DISABLED)
status_label.config(text="Disconnected")
# Restore the window size
root.geometry("") # Reset the window size to its default
def update_com_ports():
port_list = [port.device for port in serial.tools.list_ports.comports()]
port_combo['values'] = port_list
root.after(1000, update_com_ports) # Schedule this function to run every 1000 milliseconds (1 second)
# --- Initialize Tkinter ---
root = tk.Tk()
img = PhotoImage(file='icon.png')
root.iconphoto(False, img)
root.title("Serial Monitor")
# --- Variables for Serial ---
#Frame Get available ports and baud rates
port_list = [port.device for port in serial.tools.list_ports.comports()]
baud_list = ["110", "300", "600", "1200", "2400", "4800", "9600", "14400", "19200", "38400", "57600", "115200", "128000", "256000"]
# Variables for selected options
port_options = tk.StringVar()
baud_options = tk.StringVar()
# --- Create Frames ---
left_frame = tk.Frame(root, bg="cyan")
right_frame = tk.Frame(root, bg="pink")
serial_frame = tk.Frame(left_frame, bg="blue")
transport_frame = tk.Frame(left_frame, bg="green")
transport2_frame = tk.Frame(right_frame, bg="yellow")
# --- Create Serial Widgets ---
serial_title = tk.Label(serial_frame, text="Serial Settings") # Set label background color to blue
port_label = tk.Label(serial_frame, text="COM Port:") # Set label background color to blue
baud_label = tk.Label(serial_frame, text="Baud Rate:") # Set label background color to blue
port_combo = ttk.Combobox(serial_frame, textvariable=port_options, values=port_list, state="readonly")
baud_combo = ttk.Combobox(serial_frame, textvariable=baud_options, values=baud_list, state="readonly")
connect_button = tk.Button(serial_frame, text="Connect", command=connect_serial)
disconnect_button = tk.Button(serial_frame, text="Disconnect", command=disconnect_serial, state=tk.DISABLED)
status_label = tk.Label(serial_frame, text="") # Set label background color to blue
# --- Create Transport Widgets ---
transport_title = tk.Label(transport_frame, text="Transport Controls", bg="green")
start_button = tk.Button(transport_frame, text="Start")
stop_button = tk.Button(transport_frame, text="Stop")
# --- Create test Widgets ---
transport2_title = tk.Label(transport2_frame, text="Transport Controls", bg="green")
start2_button = tk.Button(transport2_frame, text="Start")
stop2_button = tk.Button(transport2_frame, text="Stop")
# --- Publish Grid layout ---
left_frame.grid(row=0, column=0, padx=10, pady=10)
right_frame.grid(row=0, column=1, padx=10, pady=10)
serial_frame.grid(row=0, column=0, padx=10, pady=10) # Grid the serial frame
transport_frame.grid(row=1, column=0, padx=10, pady=10)
transport2_frame.grid(row=1, column=0, padx=10, pady=10)
# --- Publish Serial Widgets ---
serial_title.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
port_label.grid(row=1, column=0, padx=10, pady=5)
baud_label.grid(row=2, column=0, padx=10, pady=5)
port_combo.grid(row=1, column=1, padx=10, pady=5)
baud_combo.grid(row=2, column=1, padx=10, pady=5)
connect_button.grid(row=1, column=2, padx=10, pady=5)
disconnect_button.grid(row=2, column=2, padx=10, pady=5)
status_label.grid(row=3, column=0, columnspan=3, padx=10, pady=10)
# --- Publish Transport Widgets ---
transport_title.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
start_button.grid(row=1, column=1, padx=10, pady=5)
stop_button.grid(row=2, column=1, padx=10, pady=5)
# --- Publish Test Widgets ---
transport2_title.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
start2_button.grid(row=1, column=1, padx=10, pady=5)
stop2_button.grid(row=2, column=1, padx=10, pady=5)
update_com_ports() # Starts the function to update COM ports periodically
# Start Tkinter event loop
root.mainloop()