From ac5648a9a3873475c4ea402cd977c868ef43efbd Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 15 Jul 2024 22:48:32 +0200 Subject: [PATCH] v2.9.1 --- backend/functions.py | 13 ++++++++----- main.py | 24 +++++++++++++++--------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/backend/functions.py b/backend/functions.py index 4a9d73d..5c99139 100644 --- a/backend/functions.py +++ b/backend/functions.py @@ -3,7 +3,6 @@ import json import time import serial -import json # Constants LOCALHOST_IP = "127.0.0.1" @@ -23,9 +22,12 @@ def start_connection_controller(UDP_PORT, SERIAL_PORT, VALUES, NEWLINE, SEPARATO if udp_socket and ser_socket: print("Connection established") read_serial_data(ser_socket, udp_socket, VALUES, NEWLINE, SEPARATOR, UDP_PORT) - label_connected.grid(row=18, column=1, columnspan=10) - connect_button.config(state="disabled") - disconnect_button.config(state="active") + if label_connected: + label_connected.grid(row=18, column=1, columnspan=10) + if connect_button: + connect_button.config(state="disabled") + if disconnect_button: + disconnect_button.config(state="active") else: disconnect() print("Failed to establish connection, disconnecting...") @@ -39,7 +41,7 @@ def open_stream_serial(SERIAL_PORT, BAUDRATE): print(f"Serial port {SERIAL_PORT} is open") return ser - except serial.SerialException as e: + except Exception as e: print(f"Failed to open serial port {SERIAL_PORT}: {e}") # Open a UDP JSON streaming server on the specified port @@ -146,6 +148,7 @@ def send_json_to_udp(udp_socket, json_data, UDP_PORT): # Disconnect from the serial and UDP servers def disconnect(): try: + print("Disconnecting...") global udp_socket, ser_socket if udp_socket: udp_socket.close() diff --git a/main.py b/main.py index 9014186..5ac0605 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,7 @@ def __init__(self, config_path=None, direct_connect=False): self.startup() if direct_connect: - self.connect(direct=True) + self.connect_thread(direct=True) return # Create a new instance of Tkinter application @@ -144,21 +144,27 @@ def browse_file_config_model(self): # Update the interface with new values self.update_interface() - def connect_thread(self): + def connect_thread(self, direct=False): global controller_thread # Start a new thread for the connect function - controller_thread = threading.Thread(target=self.connect, daemon=True) + controller_thread = threading.Thread(target=self.connect, args=(direct,), daemon=True) controller_thread.start() def connect(self, direct=False): if not direct: # Get the port from the textbox - self.UDP_PORT = self.textbox_udp_port.get("1.0", "end-1c") - self.SERIAL_PORT = self.textbox_serial_port.get("1.0", "end-1c") - self.VALUES = self.textbox_values.get("1.0", "end-1c").replace(" ", ",").split(",") - self.NEWLINE = self.textbox_newline.get("1.0", "end-1c") - self.SEPARATOR = self.textbox_separator.get("1.0", "end-1c") - self.BAUDRATE = self.textbox_baudrate.get("1.0", "end-1c") + if hasattr(self, 'textbox_udp_port'): + self.UDP_PORT = self.textbox_udp_port.get("1.0", "end-1c") + if hasattr(self, 'textbox_serial_port'): + self.SERIAL_PORT = self.textbox_serial_port.get("1.0", "end-1c") + if hasattr(self, 'textbox_values'): + self.VALUES = self.textbox_values.get("1.0", "end-1c").replace(" ", ",").split(",") + if hasattr(self, 'textbox_newline'): + self.NEWLINE = self.textbox_newline.get("1.0", "end-1c") + if hasattr(self, 'textbox_separator'): + self.SEPARATOR = self.textbox_separator.get("1.0", "end-1c") + if hasattr(self, 'textbox_baudrate'): + self.BAUDRATE = self.textbox_baudrate.get("1.0", "end-1c") self.save_to_config_file() # Save the data to the CONFIG file for future reuse try: