Skip to content

Commit b4ffeb6

Browse files
authored
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
1 parent 10631e0 commit b4ffeb6

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/hvps/commands/caen/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def _parse_response(response: bytes) -> (int, str):
4343
4444
Returns:
4545
(int, str): The board number and the value of the response.
46+
If the response does not include a board number, we implicitly assume that it's the only board and always return board number = 0
4647
4748
Raises:
4849
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):
5960
except UnicodeDecodeError:
6061
raise ValueError(f"Invalid response: {response}")
6162

62-
regex = re.compile(r"^#BD:(\d{2}),CMD:OK(?:,VAL:(.+))?$")
63+
regex = re.compile(r"^#(?:BD:(?P<bd>\d{2}),)?CMD:OK(?:,VAL:(?P<val>.+))?$")
6364
match = regex.match(response)
6465
if match is None:
6566
raise ValueError(f"Invalid response: '{response}'. Could not match regex")
66-
bd = int(match.group(1))
67-
value: str | None = match.group(2) if match.group(2) else None
67+
bd = int(match.group("bd")) if match.group("bd") else 0
68+
value: str | None = match.group("val") if match.group("val") else None
6869

6970
return bd, value

0 commit comments

Comments
 (0)