diff --git a/device_commands.py b/device_commands.py index 741743a..8f63737 100644 --- a/device_commands.py +++ b/device_commands.py @@ -1,7 +1,7 @@ from enum import IntEnum from serial import Serial from serial.tools import list_ports -import device_conversion as cnv +from . import device_conversion as cnv from datetime import datetime #### ---- Constants @@ -154,12 +154,20 @@ def send_STATE(prt): def get_STATE(prt): ''' Get decoded state of the device in byte format (2 bytes). ''' + import time - print("Received "+str(prt.inWaiting())+" bytes.") - if prt.inWaiting()!=2: - print("Error. Couldn't get STATE data. prt.inWaiting():", prt.inWaiting()) - print("Flushing input data:", prt.read(prt.inWaiting())) - # print("Flushing input data:", prt.read(2), prt.read(2)) + # Wait a bit more if data hasn't arrived yet + waiting_bytes = prt.inWaiting() + if waiting_bytes != 2: + # Give device a bit more time to respond + time.sleep(0.05) + waiting_bytes = prt.inWaiting() + + print("Received "+str(waiting_bytes)+" bytes.") + if waiting_bytes != 2: + print("Error. Couldn't get STATE data. prt.inWaiting():", waiting_bytes) + if waiting_bytes > 0: + print("Flushing input data:", prt.read(waiting_bytes)) return None out_bytes = prt.read(2) diff --git a/device_interaction.py b/device_interaction.py index 6e2cb78..3dbf96c 100644 --- a/device_interaction.py +++ b/device_interaction.py @@ -3,7 +3,7 @@ import time from datetime import datetime -import device_commands as cmd +from . import device_commands as cmd #### ---- Constants @@ -68,20 +68,41 @@ def create_port_connection(): def reset_port_settings(prt): # Reset port settings and check status + # First, flush any pending data in the input buffer + if prt.inWaiting() > 0: + flushed = prt.read(prt.inWaiting()) + print(f"Flushed {len(flushed)} bytes before reset: {flushed.hex()}") + cmd.send_DEFAULT_ENABLE(prt) - time.sleep(WAIT_AFTER_SEND) - status = cmd.get_STATE(prt).hex() - if status is not None: - print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")") - print("") + + # Try multiple times with increasing delays to get response + max_attempts = 3 + for attempt in range(max_attempts): + wait_time = WAIT_AFTER_SEND * (attempt + 1) # Increase wait time with each attempt + time.sleep(wait_time) + + status_bytes = cmd.get_STATE(prt) + if status_bytes is not None: + status = status_bytes.hex() + print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")") + print("") + return # Success + + if attempt < max_attempts - 1: + print(f"Attempt {attempt + 1} failed, retrying with longer delay...") + + # If all attempts failed, print warning but don't raise exception + print("Warning: Could not get STATE response after reset, but device may still be reset.") + print("") def request_state(prt): # Request data cmd.send_STATE(prt) time.sleep(WAIT_AFTER_SEND) - status = cmd.get_STATE(prt).hex() - if status is not None: + status_bytes = cmd.get_STATE(prt) + if status_bytes is not None: + status = status_bytes.hex() print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")") print("") @@ -91,8 +112,9 @@ def send_control_parameters(prt, params): hexstring = cmd.encode_Input(params) cmd.send_DECODE_ENABLE(prt,hexstring) time.sleep(WAIT_AFTER_SEND) - status = cmd.get_STATE(prt).hex() - if status is not None: + status_bytes = cmd.get_STATE(prt) + if status_bytes is not None: + status = status_bytes.hex() print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")") print("") else: @@ -103,8 +125,9 @@ def send_task_command(prt, sending_param): hexstring = cmd.create_TaskEnableCommand(sending_param) cmd.send_TASK_ENABLE(prt,hexstring) time.sleep(WAIT_AFTER_SEND) - status = cmd.get_STATE(prt).hex() - if status is not None: + status_bytes = cmd.get_STATE(prt) + if status_bytes is not None: + status = status_bytes.hex() print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")") print("") else: @@ -114,9 +137,10 @@ def request_data(prt): # Request data cmd.send_TRANS_ENABLE(prt) time.sleep(WAIT_AFTER_SEND) - data = cmd.get_DATA(prt).hex() + data_bytes = cmd.get_DATA(prt) data_dict = [] - if data is not None: + if data_bytes is not None: + data = data_bytes.hex() data_dict = cmd.decode_DATA(data) return data_dict diff --git a/gui.py b/gui.py index 36c469b..f651a1b 100644 --- a/gui.py +++ b/gui.py @@ -70,31 +70,31 @@ def get_screen_size(): screen_width, screen_height = window.get_screen_size() window.close() - COMPACT_LAYOUT = screen_width <= 1280 or screen_height <= 800 + COMPACT_LAYOUT = True margin_w, margin_h = WINDOW_MARGIN min_w, min_h = MIN_WINDOW_SIZE window_width = min(screen_width, max(min_w, screen_width - margin_w)) - window_height = min(screen_height, max(min_h, screen_height - margin_h)) + window_height = min(screen_height, max(min_h, screen_height - margin_h))//2 WINDOW_SIZE = (window_width, window_height) if COMPACT_LAYOUT: SET_TEXT_WIDTH = 30 SET_TEXT_WIDTH_NEW = 34 - graph_width = min(int(screen_width / 3.6), int(window_width / 3.1)) - graph_height = max(110, int(screen_height / 6.5)) - H_SEPARATOR_PAD = (1, 12) - OUTPUT_TEXT_PAD = (5, (12, 5)) + graph_width = min(int(screen_width / 7.2), int(window_width / 5.6)) + graph_height = max(90, int(screen_height / 16)) + H_SEPARATOR_PAD = (1, 8) + OUTPUT_TEXT_PAD = (5, (8, 3)) else: SET_TEXT_WIDTH = 34 SET_TEXT_WIDTH_NEW = 40 - graph_width = int(screen_width / 3.5) - graph_height = int(screen_width / (3 * 2.75)) - H_SEPARATOR_PAD = (1, 20) - OUTPUT_TEXT_PAD = (5, (20, 5)) + graph_width = int(screen_width / 4) + graph_height = int(screen_width / (3 * 3.5)) + H_SEPARATOR_PAD = (1, 15) + OUTPUT_TEXT_PAD = (5, (15, 5)) - graph_width = max(220, graph_width) + graph_width = max(180, graph_width) GRAPH_CANVAS_SIZE = (graph_width, graph_height) return WINDOW_SIZE