From b4ffeb6e675798ea5a8649fbbc261c065fdf4a40 Mon Sep 17 00:00:00 2001 From: jherkenhoff <22686781+jherkenhoff@users.noreply.github.com> Date: Wed, 9 Aug 2023 12:39:23 +0200 Subject: [PATCH] Add support for single-board CAEN devices (#36) * Add handling for caen single-board response * Fixed regular expression typo * Added comma in regex * Another typo in regex --- src/hvps/commands/caen/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/hvps/commands/caen/__init__.py b/src/hvps/commands/caen/__init__.py index 63bfa75..353d1ea 100644 --- a/src/hvps/commands/caen/__init__.py +++ b/src/hvps/commands/caen/__init__.py @@ -43,6 +43,7 @@ def _parse_response(response: bytes) -> (int, str): Returns: (int, str): The board number and the value of the response. + If the response does not include a board number, we implicitly assume that it's the only board and always return board number = 0 Raises: ValueError: If the response is invalid, cannot be decoded, or does not match the expected pattern. @@ -59,11 +60,11 @@ def _parse_response(response: bytes) -> (int, str): except UnicodeDecodeError: raise ValueError(f"Invalid response: {response}") - regex = re.compile(r"^#BD:(\d{2}),CMD:OK(?:,VAL:(.+))?$") + regex = re.compile(r"^#(?:BD:(?P\d{2}),)?CMD:OK(?:,VAL:(?P.+))?$") match = regex.match(response) if match is None: raise ValueError(f"Invalid response: '{response}'. Could not match regex") - bd = int(match.group(1)) - value: str | None = match.group(2) if match.group(2) else None + bd = int(match.group("bd")) if match.group("bd") else 0 + value: str | None = match.group("val") if match.group("val") else None return bd, value