Fix for web
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from serial import Serial
|
from serial import Serial
|
||||||
from serial.tools import list_ports
|
from serial.tools import list_ports
|
||||||
import device_conversion as cnv
|
from . import device_conversion as cnv
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
#### ---- Constants
|
#### ---- Constants
|
||||||
@ -154,12 +154,20 @@ def send_STATE(prt):
|
|||||||
def get_STATE(prt):
|
def get_STATE(prt):
|
||||||
''' Get decoded state of the device in byte format (2 bytes).
|
''' Get decoded state of the device in byte format (2 bytes).
|
||||||
'''
|
'''
|
||||||
|
import time
|
||||||
|
|
||||||
print("Received "+str(prt.inWaiting())+" bytes.")
|
# Wait a bit more if data hasn't arrived yet
|
||||||
if prt.inWaiting()!=2:
|
waiting_bytes = prt.inWaiting()
|
||||||
print("Error. Couldn't get STATE data. prt.inWaiting():", prt.inWaiting())
|
if waiting_bytes != 2:
|
||||||
print("Flushing input data:", prt.read(prt.inWaiting()))
|
# Give device a bit more time to respond
|
||||||
# print("Flushing input data:", prt.read(2), prt.read(2))
|
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
|
return None
|
||||||
|
|
||||||
out_bytes = prt.read(2)
|
out_bytes = prt.read(2)
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import device_commands as cmd
|
from . import device_commands as cmd
|
||||||
|
|
||||||
|
|
||||||
#### ---- Constants
|
#### ---- Constants
|
||||||
@ -68,20 +68,41 @@ def create_port_connection():
|
|||||||
|
|
||||||
def reset_port_settings(prt):
|
def reset_port_settings(prt):
|
||||||
# Reset port settings and check status
|
# 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)
|
cmd.send_DEFAULT_ENABLE(prt)
|
||||||
time.sleep(WAIT_AFTER_SEND)
|
|
||||||
status = cmd.get_STATE(prt).hex()
|
# Try multiple times with increasing delays to get response
|
||||||
if status is not None:
|
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("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
||||||
print("")
|
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):
|
def request_state(prt):
|
||||||
# Request data
|
# Request data
|
||||||
cmd.send_STATE(prt)
|
cmd.send_STATE(prt)
|
||||||
time.sleep(WAIT_AFTER_SEND)
|
time.sleep(WAIT_AFTER_SEND)
|
||||||
status = cmd.get_STATE(prt).hex()
|
status_bytes = cmd.get_STATE(prt)
|
||||||
if status is not None:
|
if status_bytes is not None:
|
||||||
|
status = status_bytes.hex()
|
||||||
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
@ -91,8 +112,9 @@ def send_control_parameters(prt, params):
|
|||||||
hexstring = cmd.encode_Input(params)
|
hexstring = cmd.encode_Input(params)
|
||||||
cmd.send_DECODE_ENABLE(prt,hexstring)
|
cmd.send_DECODE_ENABLE(prt,hexstring)
|
||||||
time.sleep(WAIT_AFTER_SEND)
|
time.sleep(WAIT_AFTER_SEND)
|
||||||
status = cmd.get_STATE(prt).hex()
|
status_bytes = cmd.get_STATE(prt)
|
||||||
if status is not None:
|
if status_bytes is not None:
|
||||||
|
status = status_bytes.hex()
|
||||||
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
||||||
print("")
|
print("")
|
||||||
else:
|
else:
|
||||||
@ -103,8 +125,9 @@ def send_task_command(prt, sending_param):
|
|||||||
hexstring = cmd.create_TaskEnableCommand(sending_param)
|
hexstring = cmd.create_TaskEnableCommand(sending_param)
|
||||||
cmd.send_TASK_ENABLE(prt,hexstring)
|
cmd.send_TASK_ENABLE(prt,hexstring)
|
||||||
time.sleep(WAIT_AFTER_SEND)
|
time.sleep(WAIT_AFTER_SEND)
|
||||||
status = cmd.get_STATE(prt).hex()
|
status_bytes = cmd.get_STATE(prt)
|
||||||
if status is not None:
|
if status_bytes is not None:
|
||||||
|
status = status_bytes.hex()
|
||||||
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
print("Received: STATE. State status:", cmd.decode_STATE(status), "("+cmd.flipfour(status)+")")
|
||||||
print("")
|
print("")
|
||||||
else:
|
else:
|
||||||
@ -114,9 +137,10 @@ def request_data(prt):
|
|||||||
# Request data
|
# Request data
|
||||||
cmd.send_TRANS_ENABLE(prt)
|
cmd.send_TRANS_ENABLE(prt)
|
||||||
time.sleep(WAIT_AFTER_SEND)
|
time.sleep(WAIT_AFTER_SEND)
|
||||||
data = cmd.get_DATA(prt).hex()
|
data_bytes = cmd.get_DATA(prt)
|
||||||
data_dict = []
|
data_dict = []
|
||||||
if data is not None:
|
if data_bytes is not None:
|
||||||
|
data = data_bytes.hex()
|
||||||
data_dict = cmd.decode_DATA(data)
|
data_dict = cmd.decode_DATA(data)
|
||||||
return data_dict
|
return data_dict
|
||||||
|
|
||||||
|
|||||||
22
gui.py
22
gui.py
@ -70,31 +70,31 @@ def get_screen_size():
|
|||||||
screen_width, screen_height = window.get_screen_size()
|
screen_width, screen_height = window.get_screen_size()
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
COMPACT_LAYOUT = screen_width <= 1280 or screen_height <= 800
|
COMPACT_LAYOUT = True
|
||||||
|
|
||||||
margin_w, margin_h = WINDOW_MARGIN
|
margin_w, margin_h = WINDOW_MARGIN
|
||||||
min_w, min_h = MIN_WINDOW_SIZE
|
min_w, min_h = MIN_WINDOW_SIZE
|
||||||
|
|
||||||
window_width = min(screen_width, max(min_w, screen_width - margin_w))
|
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)
|
WINDOW_SIZE = (window_width, window_height)
|
||||||
|
|
||||||
if COMPACT_LAYOUT:
|
if COMPACT_LAYOUT:
|
||||||
SET_TEXT_WIDTH = 30
|
SET_TEXT_WIDTH = 30
|
||||||
SET_TEXT_WIDTH_NEW = 34
|
SET_TEXT_WIDTH_NEW = 34
|
||||||
graph_width = min(int(screen_width / 3.6), int(window_width / 3.1))
|
graph_width = min(int(screen_width / 7.2), int(window_width / 5.6))
|
||||||
graph_height = max(110, int(screen_height / 6.5))
|
graph_height = max(90, int(screen_height / 16))
|
||||||
H_SEPARATOR_PAD = (1, 12)
|
H_SEPARATOR_PAD = (1, 8)
|
||||||
OUTPUT_TEXT_PAD = (5, (12, 5))
|
OUTPUT_TEXT_PAD = (5, (8, 3))
|
||||||
else:
|
else:
|
||||||
SET_TEXT_WIDTH = 34
|
SET_TEXT_WIDTH = 34
|
||||||
SET_TEXT_WIDTH_NEW = 40
|
SET_TEXT_WIDTH_NEW = 40
|
||||||
graph_width = int(screen_width / 3.5)
|
graph_width = int(screen_width / 4)
|
||||||
graph_height = int(screen_width / (3 * 2.75))
|
graph_height = int(screen_width / (3 * 3.5))
|
||||||
H_SEPARATOR_PAD = (1, 20)
|
H_SEPARATOR_PAD = (1, 15)
|
||||||
OUTPUT_TEXT_PAD = (5, (20, 5))
|
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)
|
GRAPH_CANVAS_SIZE = (graph_width, graph_height)
|
||||||
|
|
||||||
return WINDOW_SIZE
|
return WINDOW_SIZE
|
||||||
|
|||||||
Reference in New Issue
Block a user