From 662a42776fea14e4d7dfcb4cda0fe331d1a8df1a Mon Sep 17 00:00:00 2001 From: awe Date: Wed, 18 Feb 2026 18:26:41 +0300 Subject: [PATCH] fix --- laser_control/controller.py | 29 +++++++++++++++++++++++------ pyproject.toml | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/laser_control/controller.py b/laser_control/controller.py index 1e0c6ac..41b5139 100644 --- a/laser_control/controller.py +++ b/laser_control/controller.py @@ -31,8 +31,8 @@ from .constants import WAIT_AFTER_SEND_SEC logger = logging.getLogger(__name__) # Default PI regulator coefficients (match firmware defaults) -DEFAULT_PI_P = 1 -DEFAULT_PI_I = 1 +DEFAULT_PI_P = 2560 # 10 * 256 +DEFAULT_PI_I = 128 # 0.5 * 256 class LaserController: @@ -150,7 +150,7 @@ class LaserController: pi_coeff2_i=self._pi2_i, message_id=self._message_id, ) - self._send(cmd) + self._send_and_read_state(cmd) logger.debug("Manual mode set: T1=%.2f T2=%.2f I1=%.2f I2=%.2f", validated['temp1'], validated['temp2'], validated['current1'], validated['current2']) @@ -228,7 +228,7 @@ class LaserController: pi_coeff2_p=self._pi2_p, pi_coeff2_i=self._pi2_i, ) - self._send(cmd) + self._send_and_read_state(cmd) logger.info("Variation task started: type=%s min=%.3f max=%.3f step=%.3f", validated['variation_type'].name, validated['min_value'], @@ -238,7 +238,7 @@ class LaserController: def stop_task(self) -> None: """Stop the current task by sending DEFAULT_ENABLE (reset).""" cmd = Protocol.encode_default_enable() - self._send(cmd) + self._send_and_read_state(cmd) logger.info("Task stopped (DEFAULT_ENABLE sent)") def get_measurements(self) -> Optional[Measurements]: @@ -303,7 +303,7 @@ class LaserController: def reset(self) -> None: """Send a hardware reset command to the device.""" cmd = Protocol.encode_default_enable() - self._send(cmd) + self._send_and_read_state(cmd) logger.info("Device reset command sent") # ---- Internal helpers ------------------------------------------------- @@ -315,6 +315,23 @@ class LaserController: self._protocol.send_raw(cmd) time.sleep(WAIT_AFTER_SEND_SEC) + def _send_and_read_state(self, cmd: bytes) -> int: + """Send command and read the 2-byte STATE response the device always returns. + + Commands DECODE_ENABLE, TASK_ENABLE and DEFAULT_ENABLE each trigger a + STATE reply from the firmware. If we don't consume those bytes here, + they accumulate in the serial buffer and corrupt the next DATA read. + + Returns the decoded state code (0x0000 = OK). + """ + self._send(cmd) + raw = self._protocol.receive_raw(2) + if raw and len(raw) == 2: + state = Protocol.decode_state(raw) + logger.debug("STATE response after command: 0x%04x", state) + return state + return 0 + # ---- Context manager support ----------------------------------------- def __enter__(self): diff --git a/pyproject.toml b/pyproject.toml index 48be018..f7889cf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [build-system] requires = ["setuptools"] -build-backend = "setuptools.backends.legacy:build" +build-backend = "setuptools.build_meta" [project] name = "laser_control"