Skip to content

Commit 48a3bec

Browse files
authored
fix thorlabs filter wheel position read error (#215)
* second call if to get position if first isn't int * ignore empty outputs when waiting for reading * Improve exception handling
1 parent 428a4f2 commit 48a3bec

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

microscope/filterwheels/thorlabs.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
## along with Microscope. If not, see <http://www.gnu.org/licenses/>.
1919

2020
import io
21+
import string
2122
import threading
2223
import warnings
2324

@@ -80,29 +81,36 @@ def _do_set_position(self, new_position: int) -> None:
8081

8182
def _do_get_position(self):
8283
# Thorlabs positions start at 1, hence the -1
83-
return int(self._send_command("pos?")) - 1
84+
try:
85+
return int(self._send_command("pos?")) - 1
86+
except TypeError:
87+
raise microscope.DeviceError(
88+
"Unable to get position of %s", self.__class__.__name__
89+
)
8490

8591
def _readline(self):
8692
"""Custom _readline to overcome limitations of the serial implementation."""
87-
result = [None]
93+
result = []
8894
with self._lock:
89-
while result[-1] not in ("\n", ""):
90-
result.append(self.connection.read())
91-
return "".join(result[1:])
95+
while not result or result[-1] not in ("\n", ""):
96+
char = self.connection.read()
97+
# Do not allow lines to be empty.
98+
if result or (char not in string.whitespace):
99+
result.append(char)
100+
return "".join(result)
92101

93102
def _send_command(self, command):
94103
"""Send a command and return any result."""
95-
result = None
96104
with self._lock:
97105
self.connection.write(command + self.eol)
98106
response = "dummy"
99-
while response not in [command, ""]:
107+
while command not in response and ">" not in response:
100108
# Read until we receive the command echo.
101-
response = self._readline().strip("> \n\r")
109+
response = self._readline().strip()
102110
if command.endswith("?"):
103111
# Last response was the command. Next is result.
104-
result = self._readline().strip()
105-
return result
112+
return self._readline().strip()
113+
return None
106114

107115

108116
class ThorlabsFW102C(ThorlabsFilterWheel):

0 commit comments

Comments
 (0)