From 94a78d8e3fc39bb842aca7e71ed5e51391e32bea Mon Sep 17 00:00:00 2001 From: rpartzsch Date: Wed, 8 Jan 2025 17:54:30 +0100 Subject: [PATCH] ENH: added error catching when address is wrong --- motor_stage_ui/PIStagesInterface.py | 15 +++++++++++++-- motor_stage_ui/motor_stage_gui.py | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/motor_stage_ui/PIStagesInterface.py b/motor_stage_ui/PIStagesInterface.py index 4843d59..5abac51 100644 --- a/motor_stage_ui/PIStagesInterface.py +++ b/motor_stage_ui/PIStagesInterface.py @@ -69,6 +69,9 @@ def _read(self): .decode() .strip(self._terminator) ) + if msg == "": + self.log.error("No responds from motor controller.") + raise ValueError return msg def _write_read(self, command: str, address: int = None): @@ -276,8 +279,16 @@ def _get_position(self, address: int) -> None: Returns: int: current position of motorstage in integer step sizes """ - msg = self._write_read("TP", address)[3:].replace("+", "").replace(":", "") - return int(msg) + try: + msg = int( + self._write_read("TP", address)[3:].replace("+", "").replace(":", "") + ) + except ValueError: + self.log.error( + "Invalid motor stage responds:, check addresses, baudrate..." + ) + raise ValueError + return msg def _calculate_value( self, amount: str, unit: str, stage: str, step_size: float diff --git a/motor_stage_ui/motor_stage_gui.py b/motor_stage_ui/motor_stage_gui.py index 2544436..e20ad1a 100644 --- a/motor_stage_ui/motor_stage_gui.py +++ b/motor_stage_ui/motor_stage_gui.py @@ -210,7 +210,7 @@ def motor_gui( name (int, optional): Name of the motorstage. Defaults to 1. """ name = QLineEdit(text=name, parent=self) - name.move(0, (address - 1) * 30 + 20) + name.move(0, index * 30 + 20) name.resize(100, 30) name.setAlignment(Qt.AlignCenter)