Skip to content

Commit

Permalink
v2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Paolo-Beci committed May 8, 2024
1 parent 43acdf4 commit c93ee20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
19 changes: 13 additions & 6 deletions backend/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 <UDP_PORT>
#print(f"Sending JSON data: {json_data}") #DEBUG

except Exception as e:
print(f"Error sending JSON data to UDP server: {e}")
Expand Down
13 changes: 4 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class GUI:
def __init__(self):

# Create a new instance of Tkinter application
self.app = tk.Tk()

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c93ee20

Please sign in to comment.