Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Changes made:
- Function and variable names are now in snake_case for
- consistency and readability.
- Error handling in the connection function now updates
- a status label in the GUI interface to provide user feedback.
- Added a status label to display the current connection status.
- Changed the window title to "Serial Monitor" for clarity.
- Improved layout by providing spacing and organization.
- These changes should enhance the usability and readability
- of your code. If you have any further questions or
- need additional assistance, please feel free to ask!
- '''
- import tkinter as tk
- from tkinter import ttk
- import serial.tools.list_ports
- import serial
- # Global variables
- serial_connection = None
- def connect_serial():
- global serial_connection
- try:
- port = port_options.get()
- if not port:
- raise ValueError("No port selected")
- baud_rate = baud_options.get()
- serial_connection = serial.Serial(port, baud_rate)
- connect_button.config(state=tk.DISABLED)
- disconnect_button.config(state=tk.NORMAL)
- status_label.config(text="Connected to " + port)
- 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")
- # Initialize Tkinter
- root = tk.Tk()
- root.title("Serial Monitor")
- # 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()
- # Serial Connection Panel
- serial_title = tk.Label(root, text="Serial Settings")
- port_label = tk.Label(root, text="COM Port:")
- baud_label = tk.Label(root, text="Baud Rate:")
- port_combo = ttk.Combobox(root, textvariable=port_options, values=port_list)
- baud_combo = ttk.Combobox(root, textvariable=baud_options, values=baud_list)
- connect_button = tk.Button(root, text="Connect", command=connect_serial)
- disconnect_button = tk.Button(root, text="Disconnect", command=disconnect_serial, state=tk.DISABLED)
- status_label = tk.Label(root, text="")
- # Grid layout
- serial_title.grid(row=0, column=0, columnspan=3)
- port_label.grid(row=1, column=0)
- baud_label.grid(row=2, column=0)
- port_combo.grid(row=1, column=1)
- baud_combo.grid(row=2, column=1)
- connect_button.grid(row=1, column=2)
- disconnect_button.grid(row=2, column=2)
- status_label.grid(row=3, column=0, columnspan=3)
- # Start Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement