|
18 | 18 | ## along with Microscope. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
|
20 | 20 | import io |
| 21 | +import string |
21 | 22 | import threading |
22 | 23 | import warnings |
23 | 24 |
|
@@ -80,29 +81,36 @@ def _do_set_position(self, new_position: int) -> None: |
80 | 81 |
|
81 | 82 | def _do_get_position(self): |
82 | 83 | # 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 | + ) |
84 | 90 |
|
85 | 91 | def _readline(self): |
86 | 92 | """Custom _readline to overcome limitations of the serial implementation.""" |
87 | | - result = [None] |
| 93 | + result = [] |
88 | 94 | 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) |
92 | 101 |
|
93 | 102 | def _send_command(self, command): |
94 | 103 | """Send a command and return any result.""" |
95 | | - result = None |
96 | 104 | with self._lock: |
97 | 105 | self.connection.write(command + self.eol) |
98 | 106 | response = "dummy" |
99 | | - while response not in [command, ""]: |
| 107 | + while command not in response and ">" not in response: |
100 | 108 | # Read until we receive the command echo. |
101 | | - response = self._readline().strip("> \n\r") |
| 109 | + response = self._readline().strip() |
102 | 110 | if command.endswith("?"): |
103 | 111 | # Last response was the command. Next is result. |
104 | | - result = self._readline().strip() |
105 | | - return result |
| 112 | + return self._readline().strip() |
| 113 | + return None |
106 | 114 |
|
107 | 115 |
|
108 | 116 | class ThorlabsFW102C(ThorlabsFilterWheel): |
|
0 commit comments