From b955ab15b7eff26ad2b296713c4f6ddd296173a2 Mon Sep 17 00:00:00 2001 From: Marcell Nagy <133755508+marcelldls@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:14:22 +0100 Subject: [PATCH] Add exception handling, debug config (#5) --- .vscode/launch.json | 22 ++++++++++- src/thorlabs_mff_fastcs/controllers.py | 52 +++++++++++++++----------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 36d8f50..b7780ad 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,6 +18,26 @@ // Enable break on exception when debugging tests (see: tests/conftest.py) "PYTEST_RAISE": "1", }, + }, + { + "name": "Debug With Device", + "type": "debugpy", + "request": "launch", + "justMyCode": false, + "module": "thorlabs_mff_fastcs", + "purpose": [ + "debug-test" + ], + "console": "integratedTerminal", + "args": [ + "ioc", + "BL01C-EA-FLIP-01", + "/dev/serial/by-id/usb-Thorlabs_APT_Filter_Flipper_37007189-if00-port0", + "--baud", + "115200", + "--output-path", + "." + ], } ] -} +} \ No newline at end of file diff --git a/src/thorlabs_mff_fastcs/controllers.py b/src/thorlabs_mff_fastcs/controllers.py index fc0ea41..4e3da51 100755 --- a/src/thorlabs_mff_fastcs/controllers.py +++ b/src/thorlabs_mff_fastcs/controllers.py @@ -1,6 +1,8 @@ from __future__ import annotations import asyncio +import os +import signal from collections.abc import Callable, Coroutine from dataclasses import dataclass from datetime import datetime @@ -113,11 +115,15 @@ async def put( attr: AttrW, value: Any, ) -> None: - if attr.dtype is bool: - value = int(value) - await controller.conn.send_command( - self.cmd(value), - ) + try: + if attr.dtype is bool: + value = int(value) + await controller.conn.send_command( + self.cmd(value), + ) + except Exception as e: + print(f"An error occurred: {e}") + os.kill(os.getpid(), signal.SIGTERM) @dataclass @@ -133,25 +139,29 @@ async def update( controller: ThorlabsMFF, attr: AttrR, ) -> None: - if self.cache is not None: - response = await self.cache.get_response( - self.update_period, - controller.conn.send_query( + try: + if self.cache is not None: + response = await self.cache.get_response( + self.update_period, + controller.conn.send_query( + self.cmd(), + self.response_size, + ), + ) + else: + response = await controller.conn.send_query( self.cmd(), self.response_size, - ), - ) - else: - response = await controller.conn.send_query( - self.cmd(), - self.response_size, - ) + ) - response = self.response_handler(response) - if attr.dtype is bool: - await attr.set(int(response)) - else: - await attr.set(response) + response = self.response_handler(response) + if attr.dtype is bool: + await attr.set(int(response)) + else: + await attr.set(response) + except Exception as e: + print(f"An error occurred: {e}") + os.kill(os.getpid(), signal.SIGTERM) class ThorlabsMFF(Controller):