Skip to content

Commit 977275e

Browse files
Add wavelength setter
1 parent 66edce3 commit 977275e

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

src/pqnstack/pqn/drivers/thorlabs_polarimeter.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
CMD_ENABLE_ROTATION = "INP:ROT:STAT 1"
2323
CMD_DISABLE_CALC = "SENS:CALC 0"
2424
CMD_DISABLE_ROTATION = "INP:ROT:STAT 0"
25+
CMD_SET_WAVELENGTH_METERS = "SENS:CORR:WAV"
2526
QRY_IS_CALC_ENABLED = "SENS:CALC?"
2627
QRY_IS_ROTATION_ENABLED = "INP:ROT:STAT?"
2728
QRY_WAVELENGTH_METERS = "SENS:CORR:WAV?"
@@ -60,26 +61,30 @@ class PAX1000IR2(Instrument):
6061
_last_dop: float = field(default=0, init=False)
6162
_last_power_w: float = field(default=0, init=False)
6263

63-
def _read_and_write(self, cmd: str, *, expect_response: bool) -> str:
64+
def _write(self, cmd: str) -> None:
6465
if self._instr is None:
6566
msg = "Start the device first."
6667
raise DeviceNotStartedError(msg)
6768
instr: Any = self._instr
68-
if expect_response:
69-
try:
70-
instr.write(f"{cmd}\n")
71-
return str(instr.read()).strip()
72-
except (VisaError, OSError):
73-
try:
74-
return str(instr.query(cmd)).strip()
75-
except (VisaError, OSError):
76-
return ""
7769
try:
7870
instr.write(f"{cmd}\n")
7971
except (VisaError, OSError):
8072
with contextlib.suppress(VisaError, OSError):
8173
instr.write(cmd)
82-
return ""
74+
75+
def _query(self, cmd: str) -> str:
76+
if self._instr is None:
77+
msg = "Start the device first."
78+
raise DeviceNotStartedError(msg)
79+
instr: Any = self._instr
80+
try:
81+
instr.write(f"{cmd}\n")
82+
return str(instr.read()).strip()
83+
except (VisaError, OSError):
84+
try:
85+
return str(instr.query(cmd)).strip()
86+
except (VisaError, OSError):
87+
return ""
8388

8489
def _list_usb_resources(self) -> tuple[str, ...]:
8590
assert self._rm is not None
@@ -141,12 +146,15 @@ def _open_resource(self, resource_name: str) -> None:
141146
raise RuntimeError(msg) from exc
142147

143148
def _write_and_confirm(self, set_cmd: str, qry_cmd: str, expect: str | float) -> bool:
144-
_ = self._read_and_write(set_cmd, expect_response=False)
149+
try:
150+
self._write(set_cmd)
151+
except DeviceNotStartedError:
152+
return False
145153
expected_prefix = str(expect)
146154
last_response = ""
147155
for _ in range(10):
148156
try:
149-
last_response = self._read_and_write(qry_cmd, expect_response=True)
157+
last_response = self._query(qry_cmd)
150158
except DeviceNotStartedError:
151159
last_response = ""
152160
if last_response.startswith(expected_prefix):
@@ -166,11 +174,22 @@ def _init_settings(self) -> None:
166174

167175
def _read_wavelength_cache(self) -> None:
168176
try:
169-
raw_value = self._read_and_write(QRY_WAVELENGTH_METERS, expect_response=True)
170-
self._wavelength_nm_cache = float(raw_value)
177+
raw_value = self._query(QRY_WAVELENGTH_METERS)
178+
value_m = float(raw_value)
179+
self._wavelength_nm_cache = value_m * 1e9
171180
except (ValueError, TypeError):
172181
self._wavelength_nm_cache = float("nan")
173182

183+
@log_operation
184+
def set_wavelength_nm(self, wavelength_nm: float) -> None:
185+
try:
186+
value_m = float(wavelength_nm) * 1e-9
187+
except (TypeError, ValueError) as exc:
188+
msg = f"Invalid wavelength: {wavelength_nm}"
189+
raise ValueError(msg) from exc
190+
self._write(f"{CMD_SET_WAVELENGTH_METERS} {value_m}")
191+
self._read_wavelength_cache()
192+
174193
def start(self) -> None:
175194
if self._instr is not None:
176195
return
@@ -188,17 +207,18 @@ def start(self) -> None:
188207
self.operations.update(
189208
{
190209
"read": self.read,
210+
"set_wavelength_nm": self.set_wavelength_nm,
191211
}
192212
)
193213
atexit.register(self.close)
194214

195215
def close(self) -> None:
196216
if self._instr is not None:
197217
with contextlib.suppress(Exception):
198-
_ = self._read_and_write(CMD_DISABLE_CALC, expect_response=False)
199-
_ = self._read_and_write(CMD_DISABLE_ROTATION, expect_response=False)
200-
_ = self._read_and_write(QRY_IS_CALC_ENABLED, expect_response=True)
201-
_ = self._read_and_write(QRY_IS_ROTATION_ENABLED, expect_response=True)
218+
self._write(CMD_DISABLE_CALC)
219+
self._write(CMD_DISABLE_ROTATION)
220+
_ = self._query(QRY_IS_CALC_ENABLED)
221+
_ = self._query(QRY_IS_ROTATION_ENABLED)
202222
with contextlib.suppress(Exception):
203223
self._instr.close()
204224
self._instr = None
@@ -231,7 +251,7 @@ def read(self) -> dict[str, float]:
231251
if self._instr is None:
232252
msg = "Start the device first."
233253
raise DeviceNotStartedError(msg)
234-
raw_reply = self._read_and_write(QRY_LATEST, expect_response=True)
254+
raw_reply = self._query(QRY_LATEST)
235255

236256
token_strs = [p for p in raw_reply.replace(";", ",").split(",") if p]
237257
parsed_values: list[float | str] = []

0 commit comments

Comments
 (0)