Skip to content

Commit 6022950

Browse files
Added parser for ASI info
1 parent 5470100 commit 6022950

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

microscope/controllers/asi.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import threading
2828
import time
2929
import typing
30+
import logging
3031

3132
import serial
3233

3334
import microscope.abc
3435

36+
_logger = logging.getLogger(__name__)
3537

3638
# Started to develop by copying from the ludl driver.
3739
# no support for filter, shutters, or slide loader as I dont have hardware
@@ -137,7 +139,29 @@
137139
}
138140

139141

140-
class _ASIController:
142+
def parse_info(info: bytes) -> typing.Mapping[str, str]:
143+
info = info.decode().strip()
144+
145+
items = []
146+
for line in info.split('\r'):
147+
items.append(line[:33].strip())
148+
items.append(line[33:].strip())
149+
150+
pattern = "(?P<name>.*):\s*((?P<value>\S+)(\s+)?(?P<command>[[].*[]])?(\s+)?(?P<units>.*)?)$"
151+
152+
settings = {}
153+
for item in items:
154+
match = re.search(pattern, item)
155+
settings[match.group('name').strip()] = {
156+
'value': match.group('value'),
157+
'command': None if not match.group('command') else match.group('command')[1:-1],
158+
'units': match.group('units') if len(match.group('units')) else None,
159+
}
160+
161+
return settings
162+
163+
164+
class _ASIMotionController:
141165
"""Connection to a ASI Controller and wrapper to its commands.
142166
143167
Tested with MS2000 controller and xy stage.
@@ -156,7 +180,7 @@ def __init__(self, port: str, baudrate: int, timeout: float) -> None:
156180
baudrate=baudrate,
157181
timeout=timeout,
158182
bytesize=serial.EIGHTBITS,
159-
stopbits=serial.STOPBITS_TWO,
183+
stopbits=serial.STOPBITS_ONE,
160184
parity=serial.PARITY_NONE,
161185
xonxoff=False,
162186
rtscts=False,
@@ -168,9 +192,17 @@ def __init__(self, port: str, baudrate: int, timeout: float) -> None:
168192
# We do not use the general get_description() here because
169193
# if this is not a ProScan device it would never reach the
170194
# '\rEND\r' that signals the end of the description.
195+
self.axis_info = {}
171196
try:
172-
self.command(b"INFO X")
173-
answer = self.read_multiline()
197+
for i, axis in AXIS_MAPPER.items():
198+
self.command(f"INFO {axis}".encode())
199+
answer = self._serial.read_all()
200+
if len(answer) == 0:
201+
_logger.info(f"Axis {axis} not present")
202+
continue
203+
_logger.info(f"Axis {axis} present")
204+
self.axis_info[axis] = parse_info(answer)
205+
174206
except:
175207
print(
176208
"Unable to read configuration. Is ASI controller connected?"
@@ -179,16 +211,6 @@ def __init__(self, port: str, baudrate: int, timeout: float) -> None:
179211
# parse config responce which tells us what devices are present
180212
# on this controller.
181213

182-
self._devlist = {}
183-
184-
for line in answer[4:-1]:
185-
# loop through lines 4 to second last one which are devices
186-
# present on this controller
187-
devinfo = re.split(r"\s{2,}", line.decode("ascii"))
188-
# dev address,label,id,description, type
189-
self._devlist[devinfo[0]] = devinfo[1:]
190-
191-
# print(answer)
192214

193215
def is_busy(self):
194216
pass
@@ -335,7 +357,7 @@ def changed_timeout(self, new_timeout: float):
335357

336358

337359
class _ASIStageAxis(microscope.abc.StageAxis):
338-
def __init__(self, dev_conn: _ASIController, axis: str) -> None:
360+
def __init__(self, dev_conn: _ASIMotionController, axis: str) -> None:
339361
super().__init__()
340362
self._dev_conn = dev_conn
341363
self._axis = axis
@@ -405,7 +427,7 @@ def find_limits(self, speed=100):
405427

406428

407429
class _ASIStage(microscope.abc.Stage):
408-
def __init__(self, conn: _ASIController, **kwargs) -> None:
430+
def __init__(self, conn: _ASIMotionController, **kwargs) -> None:
409431
super().__init__(**kwargs)
410432
self._dev_conn = conn
411433
self._axes = {
@@ -518,7 +540,7 @@ def __init__(
518540
self, port: str, baudrate: int = 9600, timeout: float = 0.5, **kwargs
519541
) -> None:
520542
super().__init__(**kwargs)
521-
self._conn = _ASIController(port, baudrate, timeout)
543+
self._conn = _ASIMotionController(port, baudrate, timeout)
522544
self._devices: typing.Mapping[str, microscope.abc.Device] = {}
523545
self._devices["stage"] = _ASIStage(self._conn)
524546

0 commit comments

Comments
 (0)