Fix for web
This commit is contained in:
@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user