Skip to content

Commit

Permalink
Add exception handling, debug config (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelldls authored Oct 7, 2024
1 parent 502bbd9 commit b955ab1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
22 changes: 21 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"."
],
}
]
}
}
52 changes: 31 additions & 21 deletions src/thorlabs_mff_fastcs/controllers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit b955ab1

Please sign in to comment.