From c93ee20f762587e1957b53501ab3f86531e839f3 Mon Sep 17 00:00:00 2001 From: Paolo Date: Wed, 8 May 2024 10:43:59 +0200 Subject: [PATCH] v2.6 --- backend/functions.py | 19 +++++++++++++------ main.py | 13 ++++--------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/backend/functions.py b/backend/functions.py index cc0048f..4a9d73d 100644 --- a/backend/functions.py +++ b/backend/functions.py @@ -85,7 +85,7 @@ def read_data_handler(data_buffer): # Remove the extracted line from the buffer data_buffer = data_buffer[line_index + len(NEWLINE):] - print(f"Received line: {line}") #DEBUG + #print(f"Received line: {line}") #DEBUG # Process the line (convert to json and send it to the UDP server) csv_to_json(udp_socket, line, UDP_PORT, VALUES, SEPARATOR) @@ -113,8 +113,17 @@ def csv_to_json(udp_socket, line_csv, UDP_PORT, VALUES, SEPARATOR): for value, csv_part in zip(VALUES, csv_parts): # Check if the csv part is not NULL if value != "NOPE": + # Convert value to int or float if possible + try: + csv_part = int(csv_part) # Try converting to integer + except ValueError: + try: + csv_part = float(csv_part) # Try converting to float + except ValueError: + pass # If not convertible, keep the value as string + # Add the value and csv part to the json_data dictionary - json_data[value] = csv_part.strip() + json_data[value] = csv_part # Send the data to the UDP server send_json_to_udp(udp_socket, json_data, UDP_PORT) @@ -125,13 +134,11 @@ def csv_to_json(udp_socket, line_csv, UDP_PORT, VALUES, SEPARATOR): # Send JSON data to the UDP server def send_json_to_udp(udp_socket, json_data, UDP_PORT): try: - print(f"Sending JSON data: {json_data}") #DEBUG # Serialize the JSON data to a string - json_string = json.dumps(json_data) - # Encode the JSON string to bytes - json_bytes = json_string.encode('utf-8') + json_bytes = json.dumps(json_data).encode('utf-8') # Send JSON data to the server udp_socket.sendto(json_bytes, (LOCALHOST_IP, int(UDP_PORT))) # Check with cmd: nc -ul + #print(f"Sending JSON data: {json_data}") #DEBUG except Exception as e: print(f"Error sending JSON data to UDP server: {e}") diff --git a/main.py b/main.py index d05fd93..e2f98b6 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,6 @@ class GUI: def __init__(self): - # Create a new instance of Tkinter application self.app = tk.Tk() @@ -19,9 +18,6 @@ def __init__(self): self.UDP_PORT=5000 self.SERIAL_PORT='/dev/ttyUSB0' - # Flag - self.connected = False - global controller_thread controller_thread = None @@ -32,7 +28,7 @@ def __init__(self): self.screen_width = self.app.winfo_screenwidth() self.screen_height = self.app.winfo_screenheight() self.window_width = 500 - self.window_height = 580 + self.window_height = 600 self.x_coordinate = (self.screen_width - self.window_width) // 2 self.y_coordinate = (self.screen_height - self.window_height) // 2 @@ -97,11 +93,11 @@ def __init__(self): self.textbox_serial_port.grid(row=15, column=1, columnspan=10) # Connect button - self.connect_button = tk.Button(self.frame, text="Connect", command=self.connect_thread, font=("Arial", 14, "bold"), width=7, height=2) + self.connect_button = tk.Button(self.frame, text="Connect", command=self.connect_thread, font=("Arial", 14, "bold"), width=8, height=2) self.connect_button.grid(row=16, column=0, columnspan=5, pady=15) # Disconnect button - self.disconnect_button = tk.Button(self.frame, text="Disconnect", command=self.disconnect, font=("Arial", 14, "bold"), width=7, height=2, state="disabled") + self.disconnect_button = tk.Button(self.frame, text="Disconnect", command=self.disconnect, font=("Arial", 14, "bold"), width=8, height=2, state="disabled") self.disconnect_button.grid(row=16, column=7, columnspan=5, pady=15) # Connected status @@ -129,8 +125,7 @@ def browse_file_config_model(self): def connect_thread(self): global controller_thread # Start a new thread for the connect function - controller_thread = threading.Thread(target=self.connect, daemon=True) - controller_thread.start() + controller_thread = threading.Thread(target=self.connect, daemon=True).start() def connect(self): # Get the port from the textbox