Skip to content

Commit 99ef9bd

Browse files
Fixed issues with setting getter and setter.
At least some of the settings are working
1 parent 3ee5ad0 commit 99ef9bd

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

microscope/controllers/asi.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def command(self, command: bytes) -> None:
221221
def readline(self) -> bytes:
222222
"""Read a line from the device connection until ``\\r\\n``."""
223223
with self._lock:
224+
# We actually read until the \\r cause multiline does not add the \\n
224225
line = self._serial.read_until(b"\r")
225226
# _logger.warning
226227
return line
@@ -434,11 +435,34 @@ def __init__(self, conn: _ASIMotionController, **kwargs) -> None:
434435

435436
self.homed = False
436437

438+
def _get_setting(self, command, axis, dtype):
439+
answer = self._dev_conn.get_command(bytes(f"{command} {axis}?", "ascii"))
440+
answer = answer.strip().decode()
441+
if answer[:2] == ":A":
442+
if dtype == "int":
443+
return int(answer[5:])
444+
elif dtype == "float":
445+
return float(answer[5:])
446+
else:
447+
return answer[5:]
448+
elif answer[0] == "N":
449+
# this is an error string
450+
error = answer[2:]
451+
raise Exception(
452+
f"ASI controller error: {error},{LUDL_ERRORS[error]}"
453+
)
454+
else:
455+
raise Exception(f"ASI controller error: {answer}")
456+
457+
def _set_setting(self, value, command, axis):
458+
self._dev_conn.set_command(bytes(f"{command} {axis}={value}", "ascii"))
459+
460+
437461
def _add_settings(self, settings) -> None:
438462
"""INFO command returns a list of settings that is parsed into a dict. This function takes that dict and
439463
adds settings consequently to the stage object.
440464
"""
441-
for axis_name, axis_settings in settings.items():
465+
for axis, axis_settings in settings.items():
442466
for setting_name, setting_params in axis_settings.items():
443467
if setting_params['value'].isdigit():
444468
value = int(setting_params['value'])
@@ -451,19 +475,15 @@ def _add_settings(self, settings) -> None:
451475
dtype = "str" # It might be a enum but we dont know
452476

453477
if setting_params['command'] is not None:
454-
set_function: Callable[[Any], bytes] = lambda x: self._dev_conn.get_command(
455-
bytes(f"{setting_params['command']} {axis_name}={x}", "ascii"))
456478
read_only = False
457479
else:
458-
set_function = None
459480
read_only = True
460481

461482
self.add_setting(
462-
name=f"{setting_name}_{axis_name}",
483+
name=f"{setting_name} {axis}",
463484
dtype=dtype,
464-
get_func=lambda: self._dev_conn.get_command(
465-
bytes(f"{setting_params['command']} {axis_name}", "ascii")),
466-
set_func=set_function,
485+
get_func=lambda command=setting_params['command'], axis=axis, dtype=dtype: self._get_setting(command, axis, dtype),
486+
set_func=lambda value, command=setting_params['command'], axis=axis: self._set_setting(value, command, axis),
467487
values=lambda: f"curr value is {value}. Units are {setting_params['units']}",
468488
# readonly=read_only,
469489
)

0 commit comments

Comments
 (0)