Skip to content

Commit af47c5e

Browse files
Fixed the Send Input
Fixed a few bugs wtih connecting and reconnecting, fixed the pause button not actually freeing the Serial port which causes other programs to not be able to access it. Fixed the issue with randomly closing null threads Fixed the issue with not checking state of NL and CR buttons when sending a message to append to it Fixed an issue with theme colors not applying to the input text field correctly
1 parent 297b6ce commit af47c5e

2 files changed

Lines changed: 63 additions & 22 deletions

File tree

Serial_Monitor.py

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ def update_widgets_color(self):
9595
except Exception as e:
9696
messagebox.showerror("Error", f"Failed to update widget colors: {e}")
9797

98+
99+
def send(self, event=None):
100+
try:
101+
text = self.input_text.get()
102+
if text and text != "Type here to send...":
103+
if self.nl_var.get():
104+
text += "\n" # Add newline if NL checkbox is checked
105+
if self.cr_var.get():
106+
text += "\r" # Add carriage return if CR checkbox is checked
107+
108+
# Send the text to the serial connection (if open)
109+
if self.serial_connection and self.serial_connection.is_open:
110+
self.serial_connection.write(text.encode('utf-8'))
111+
self.output_text.insert(tk.END, f"\nSent: {text}\n")
112+
if self.autoscroll_var.get():
113+
self.output_text.see(tk.END)
114+
115+
self.input_text.delete(0, tk.END) # Clear the input field
116+
117+
except Exception as e:
118+
messagebox.showerror("Error", f"Failed to send data: {e}")
119+
98120
def create_widgets(self):
99121
try:
100122
self.menu_button = tk.Button(self.root, text='☰', command=self.show_menu, bg=self.accent_color, fg='white')
@@ -115,11 +137,12 @@ def create_widgets(self):
115137
self.baud_combobox.grid(row=0, column=3, padx=5, pady=15, sticky='e')
116138
self.baud_combobox.bind("<<ComboboxSelected>>", self.on_baud_rate_change)
117139

118-
self.input_text = tk.Entry(self.root, bg='#3b3b3b', fg='white', width=30)
140+
self.input_text = tk.Entry(self.root, bg=self.bg_color, fg=self.fg_color, width=30)
119141
self.input_text.grid(row=1, column=0, columnspan=4, padx=(10, 20), pady=15, sticky='ew')
120142
self.input_text.insert(0, "Type here to send...")
121143
self.input_text.bind("<FocusIn>", self.clear_placeholder)
122144
self.input_text.bind("<FocusOut>", self.set_placeholder)
145+
self.input_text.bind("<Return>", self.send)
123146

124147
checkbox_frame = tk.Frame(self.root, bg=self.accent_color)
125148
checkbox_frame.grid(row=1, column=4, padx=(5, 15), pady=15, sticky='w')
@@ -326,11 +349,11 @@ def reconnect(self):
326349
if port:
327350
try:
328351
self.serial_connection = serial.Serial(port, baud_rate, timeout=1)
329-
self.output_text.insert(tk.END, f"Connected to {port} at {baud_rate} baud.\n")
352+
self.output_text.insert(tk.END, f"\n Connected to {port} at {baud_rate} baud.\n")
330353
self.is_reading = True
331354
self.start_reading()
332355
except serial.SerialException as e:
333-
self.output_text.insert(tk.END, f"Failed to connect: {e}\n")
356+
self.output_text.insert(tk.END, f"\n\tFailed to connect: {e}\n")
334357
except Exception as e:
335358
messagebox.showerror("Error", f"Failed to reconnect: {e}")
336359

@@ -344,25 +367,43 @@ def start_reading(self):
344367
def read_serial(self):
345368
try:
346369
while self.is_reading:
347-
if self.serial_connection.in_waiting:
348-
line = self.serial_connection.readline().decode('utf-8', errors='replace')
349-
with self.lock: # Acquire the lock while updating the UI
350-
self.output_text.insert(tk.END, line)
351-
if self.autoscroll_var.get():
352-
self.output_text.see(tk.END)
353-
except Exception as e:
354-
messagebox.showerror("Error", f"Failed to read from serial: {e}")
355-
370+
if self.serial_connection and self.serial_connection.is_open:
371+
if self.serial_connection.in_waiting:
372+
line = self.serial_connection.readline().decode('utf-8', errors='replace')
373+
with self.lock: # Acquire the lock while updating the UI
374+
self.output_text.insert(tk.END, line)
375+
if self.autoscroll_var.get():
376+
self.output_text.see(tk.END)
377+
else:
378+
break # Exit the loop if the serial connection is closed
379+
except (serial.SerialException, OSError) as e:
380+
# Handle the error gracefully without crashing
381+
self.output_text.insert(tk.END, f"\n Error reading from serial: {e}\n")
356382
def pause(self):
357383
try:
358-
self.is_reading = not self.is_reading
359-
if self.is_reading:
360-
self.start_reading()
361-
self.pause_button.config(text="Pause")
362-
else:
384+
if self.serial_connection and self.serial_connection.is_open:
385+
# Close the serial connection
386+
self.serial_connection.close()
387+
self.serial_connection = None # Set to None to fully release the port
388+
389+
self.is_reading = False # Stop the reading thread if it's running
363390
self.pause_button.config(text="Resume")
391+
self.output_text.insert(tk.END, "\n Serial connection paused.\n")
392+
else:
393+
# Reconnect if paused
394+
selected_port = self.port_combobox.get()
395+
selected_baud = self.baud_combobox.get()
396+
397+
if selected_port and selected_baud:
398+
self.serial_connection = serial.Serial(selected_port, int(selected_baud))
399+
self.is_reading = True # Start reading again if connection is re-established
400+
self.start_reading() # Start the reading thread again
401+
self.pause_button.config(text="Pause")
402+
self.output_text.insert(tk.END, "\n Serial connection resumed.\n")
403+
else:
404+
messagebox.showerror("Error", "No port or baud rate selected.")
364405
except Exception as e:
365-
messagebox.showerror("Error", f"Failed to pause/resume reading: {e}")
406+
messagebox.showerror("Error", f"Error toggling connection: {e}")
366407

367408
def clear_output(self):
368409
try:
@@ -381,15 +422,15 @@ def clear_placeholder(self, event):
381422
try:
382423
if self.input_text.get() == "Type here to send...":
383424
self.input_text.delete(0, tk.END)
384-
self.input_text.config(fg='black')
425+
self.input_text.config(fg=self.fg_color)
385426
except Exception as e:
386427
messagebox.showerror("Error", f"Failed to clear placeholder: {e}")
387428

388429
def set_placeholder(self, event):
389430
try:
390431
if self.input_text.get() == "":
391432
self.input_text.insert(0, "Type here to send...")
392-
self.input_text.config(fg='grey')
433+
self.input_text.config(fg=self.accent_color)
393434
except Exception as e:
394435
messagebox.showerror("Error", f"Failed to set placeholder: {e}")
395436

serial_monitor.spec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ exe = EXE(
2626
debug=False,
2727
bootloader_ignore_signals=False,
2828
strip=False,
29-
upx=False,
29+
upx=True,
3030
upx_exclude=[],
3131
runtime_tmpdir=None,
32-
console=False,
32+
console=True,
3333
disable_windowed_traceback=False,
3434
argv_emulation=False,
3535
target_arch=None,

0 commit comments

Comments
 (0)