| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| •25 |
import importlib |
| 26 |
import time |
| 27 |
from unittest import mock |
| 28 |
|
| 29 |
import pytest |
| 30 |
|
| 31 |
from pymeasure.test import expected_protocol |
| 32 |
from pymeasure.instruments import Instrument, Channel |
| 33 |
from pymeasure.adapters import FakeAdapter, ProtocolAdapter |
| 34 |
from pymeasure.instruments.fakes import FakeInstrument |
| 35 |
from pymeasure.instruments.validators import truncated_range |
| 36 |
|
| •37 |
is_pyvisa_sim_not_installed = not bool(importlib.util.find_spec("pyvisa_sim")) |
| 38 |
|
| 39 |
|
| 40 |
class GenericInstrument(FakeInstrument): |
| 41 |
# Use truncated_range as this easily lets us test for the range boundaries |
| 42 |
fake_ctrl = Instrument.control( |
| 43 |
"", "%d", "docs", |
| 44 |
validator=truncated_range, |
| 45 |
values=(1, 10), |
| 46 |
dynamic=True, |
| 47 |
) |
| 48 |
fake_setting = Instrument.setting( |
| 49 |
"%d", "docs", |
| 50 |
validator=truncated_range, |
| 51 |
values=(1, 10), |
| 52 |
dynamic=True, |
| 53 |
) |
| 54 |
fake_measurement = Instrument.measurement( |
| 55 |
"", "docs", |
| 56 |
values={'X': 1, 'Y': 2, 'Z': 3}, |
| 57 |
map_values=True, |
| 58 |
dynamic=True, |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
@pytest.fixture() |
| 63 |
def generic(): |
| 64 |
return GenericInstrument() |
| 65 |
|
| 66 |
|
| 67 |
class GenericChannel(Channel): |
| 68 |
# Use truncated_range as this easily lets us test for the range boundaries |
| 69 |
fake_ctrl = Instrument.control( |
| 70 |
"C{ch}:control?", "C{ch}:control %d", "docs", |
| 71 |
validator=truncated_range, |
| 72 |
values=(1, 10), |
| 73 |
dynamic=True, |
| 74 |
) |
| 75 |
fake_setting = Instrument.setting( |
| 76 |
"C{ch}:setting %d", "docs", |
| 77 |
validator=truncated_range, |
| 78 |
values=(1, 10), |
| 79 |
dynamic=True, |
| 80 |
) |
| 81 |
fake_measurement = Instrument.measurement( |
| 82 |
"C{ch}:measurement?", "docs", |
| 83 |
values={'X': 1, 'Y': 2, 'Z': 3}, |
| 84 |
map_values=True, |
| 85 |
dynamic=True, |
| 86 |
) |
| 87 |
special_control = Instrument.control( |
| 88 |
"SOUR{ch}:special?", "OUTP{ch}:special %s", |
| 89 |
"""A special control with different channel specifiers for get and set.""", |
| 90 |
cast=str, |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
class ChannelInstrument(Instrument): |
| 95 |
def __init__(self, adapter, name="ChannelInstrument", **kwargs): |
| 96 |
super().__init__(adapter, name, **kwargs) |
| 97 |
self.add_child(GenericChannel, "A") |
| 98 |
self.add_child(GenericChannel, "B") |
| 99 |
|
| 100 |
|
| 101 |
def test_fake_instrument(): |
| 102 |
fake = FakeInstrument() |
| 103 |
fake.write("Testing") |
| 104 |
assert fake.read() == "Testing" |
| 105 |
assert fake.read() == "" |
| 106 |
assert fake.values("5") == [5] |
| 107 |
|
| 108 |
|
| 109 |
class Test_includeSCPI_parameter: |
| 110 |
def test_not_defined_includeSCPI_raises_warning(self): |
| 111 |
with pytest.warns(FutureWarning) as record: |
| 112 |
Instrument(name="test", adapter=ProtocolAdapter()) |
| 113 |
msg = str(record[0].message) |
| 114 |
assert msg == ("It is deprecated to specify `includeSCPI` implicitly, use " |
| 115 |
"`includeSCPI=False` or inherit the `SCPIMixin` class instead.") |
| 116 |
|
| 117 |
def test_not_defined_includeSCPI_is_interpreted_as_true(self): |
| 118 |
inst = Instrument(name="test", adapter=ProtocolAdapter()) |
| 119 |
assert inst.SCPI is True |
| 120 |
|
| 121 |
|
| •122 |
@pytest.mark.skipif( |
| 123 |
is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library" |
| 124 |
) |
| 125 |
@pytest.mark.parametrize("adapter", (("COM1", 87, "USB"))) |
| 126 |
def test_init_visa(adapter): |
| 127 |
Instrument(adapter, "def", visa_library="@sim") |
| 128 |
pass # Test that no error is raised |
| 129 |
|
| 130 |
|
| 131 |
@pytest.mark.xfail() # I do not know, when this error is raised |
| 132 |
def test_init_visa_fail(): |
| 133 |
with pytest.raises(Exception, match="Invalid adapter"): |
| 134 |
Instrument("abc", "def", visa_library="@xyz") |
| 135 |
|
| 136 |
|
| •137 |
@pytest.mark.skipif( |
| 138 |
is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library" |
| 139 |
) |
| 140 |
def test_init_includeSCPI_implicit_warning(): |
| 141 |
with pytest.warns(FutureWarning, match="includeSCPI"): |
| 142 |
Instrument("COM1", "def", visa_library="@sim") |
| 143 |
|
| 144 |
|
| •145 |
@pytest.mark.skipif( |
| 146 |
is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library" |
| 147 |
) |
| 148 |
def test_init_includeSCPI_explicit_warning(): |
| 149 |
with pytest.warns(FutureWarning, match="includeSCPI"): |
| 150 |
Instrument("COM1", "def", visa_library="@sim", includeSCPI=True) |
| 151 |
|
| 152 |
|
| 153 |
def test_global_preprocess_reply(): |
| 154 |
with pytest.warns(FutureWarning, match="deprecated"): |
| 155 |
inst = Instrument(FakeAdapter(), "name", preprocess_reply=lambda v: v.strip("x")) |
| 156 |
assert inst.values("x5x") == [5] |
| 157 |
|
| 158 |
|
| •159 |
@pytest.mark.skipif( |
| 160 |
is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library" |
| 161 |
) |
| 162 |
def test_instrument_in_context(): |
| 163 |
with Instrument("abc", "def", visa_library="@sim") as instr: |
| 164 |
pass |
| 165 |
assert instr.isShutdown is True |
| 166 |
|
| 167 |
|
| 168 |
def test_with_statement(): |
| 169 |
""" Test the with-statement-behaviour of the instruments. """ |
| 170 |
with FakeInstrument() as fake: |
| 171 |
# Check if fake is an instance of FakeInstrument |
| 172 |
assert isinstance(fake, FakeInstrument) |
| 173 |
|
| 174 |
# Check whether the shutdown function is already called |
| 175 |
assert fake.isShutdown is False |
| 176 |
|
| 177 |
# Check whether the shutdown function is called upon |
| 178 |
assert fake.isShutdown is True |
| 179 |
|
| 180 |
|
| 181 |
class TestInstrumentCommunication: |
| 182 |
@pytest.fixture() |
| 183 |
def instr(self): |
| 184 |
a = mock.MagicMock(return_value="5") |
| 185 |
return Instrument(a, "abc") |
| 186 |
|
| 187 |
def test_write(self, instr): |
| 188 |
instr.write("abc") |
| 189 |
assert instr.adapter.method_calls == [mock.call.write('abc')] |
| 190 |
|
| 191 |
def test_read(self, instr): |
| 192 |
instr.read() |
| 193 |
assert instr.adapter.method_calls == [mock.call.read()] |
| 194 |
|
| 195 |
def test_write_bytes(self, instr): |
| 196 |
instr.write_bytes(b"abc") |
| 197 |
assert instr.adapter.method_calls == [mock.call.write_bytes(b"abc")] |
| 198 |
|
| 199 |
def test_read_bytes(self, instr): |
| 200 |
instr.read_bytes(5) |
| 201 |
assert instr.adapter.method_calls == [mock.call.read_bytes(5)] |
| 202 |
|
| 203 |
def test_write_binary_values(self, instr): |
| 204 |
instr.write_binary_values("abc", [5, 6, 7]) |
| 205 |
assert instr.adapter.method_calls == [mock.call.write_binary_values("abc", [5, 6, 7])] |
| 206 |
|
| 207 |
|
| 208 |
class TestWaiting: |
| 209 |
@pytest.fixture() |
| 210 |
def instr(self): |
| 211 |
class Faked(Instrument): |
| 212 |
def wait_for(self, query_delay=None): |
| 213 |
self.waited = query_delay |
| 214 |
return Faked(ProtocolAdapter(), name="faked") |
| 215 |
|
| 216 |
def test_waiting(self): |
| 217 |
instr = Instrument(ProtocolAdapter(), "faked") |
| 218 |
stop = time.perf_counter() + 100 |
| 219 |
instr.wait_for(0.1) |
| 220 |
assert time.perf_counter() < stop |
| 221 |
|
| 222 |
def test_ask_calls_wait(self, instr): |
| 223 |
instr.adapter.comm_pairs = [("abc", "resp")] |
| 224 |
instr.ask("abc") |
| 225 |
assert instr.waited is None |
| 226 |
|
| 227 |
def test_ask_calls_wait_with_delay(self, instr): |
| 228 |
instr.adapter.comm_pairs = [("abc", "resp")] |
| 229 |
instr.ask("abc", query_delay=10) |
| 230 |
assert instr.waited == 10 |
| 231 |
|
| 232 |
def test_binary_values_calls_wait(self, instr): |
| 233 |
instr.adapter.comm_pairs = [("abc", "abcdefgh")] |
| 234 |
instr.binary_values("abc") |
| 235 |
assert instr.waited is None |
| 236 |
|
| 237 |
|
| 238 |
@pytest.mark.parametrize("method, write, reply", (("id", "*IDN?", "xyz"), |
| 239 |
("complete", "*OPC?", "1"), |
| 240 |
("status", "*STB?", "189"), |
| 241 |
("options", "*OPT?", "a9"), |
| 242 |
)) |
| 243 |
def test_SCPI_properties(method, write, reply): |
| 244 |
with expected_protocol( |
| 245 |
Instrument, |
| 246 |
[(write, reply)], |
| 247 |
name="test") as instr: |
| 248 |
assert getattr(instr, method) == reply |
| 249 |
|
| 250 |
|
| 251 |
@pytest.mark.parametrize("method, write", (("clear", "*CLS"), |
| 252 |
("reset", "*RST") |
| 253 |
)) |
| 254 |
def test_SCPI_write_commands(method, write): |
| 255 |
with expected_protocol( |
| 256 |
Instrument, |
| 257 |
[(write, None)], |
| 258 |
name="test") as instr: |
| 259 |
getattr(instr, method)() |
| 260 |
|
| 261 |
|
| 262 |
def test_instrument_check_errors(): |
| 263 |
with expected_protocol( |
| 264 |
Instrument, |
| 265 |
[("SYST:ERR?", "17,funny stuff"), |
| 266 |
("SYST:ERR?", "0")], |
| 267 |
name="test") as instr: |
| 268 |
assert instr.check_errors() == [[17, "funny stuff"]] |
| 269 |
|
| 270 |
|
| 271 |
@pytest.mark.parametrize("method", ("id", "complete", "status", "options", |
| 272 |
"clear", "reset", "check_errors" |
| 273 |
)) |
| 274 |
def test_SCPI_false_raises_errors(method): |
| 275 |
with pytest.raises(NotImplementedError): |
| 276 |
getattr(Instrument(FakeAdapter(), "abc", includeSCPI=False), method)() |
| 277 |
|
| 278 |
|
| 279 |
# Channel |
| 280 |
class TestMultiFunctionality: |
| 281 |
"""Test the usage of children for different functionalities.""" |
| 282 |
class SomeFunctionality(Channel): |
| 283 |
"""This Functionality needs a prepended `id`.""" |
| 284 |
|
| 285 |
def insert_id(self, command, **kwargs): |
| 286 |
return f"{self.id}:{command}" |
| 287 |
|
| 288 |
voltage = Channel.control("Volt?", "Volt %f", "Set voltage in Volts.") |
| 289 |
|
| 290 |
class InstrumentWithFunctionality(ChannelInstrument): |
| 291 |
"""Instrument with some functionality.""" |
| 292 |
|
| 293 |
def __init__(self, adapter, **kwargs): |
| 294 |
super().__init__(adapter, **kwargs) |
| 295 |
self.add_child(TestMultiFunctionality.SomeFunctionality, "X", |
| 296 |
collection="functions", prefix="f_") |
| 297 |
|
| 298 |
def test_functionality_dict(self): |
| 299 |
inst = TestMultiFunctionality.InstrumentWithFunctionality(ProtocolAdapter()) |
| 300 |
assert isinstance(inst.functions["X"], TestMultiFunctionality.SomeFunctionality) |
| 301 |
assert inst.functions["X"] == inst.f_X |
| 302 |
|
| 303 |
def test_functions_voltage_getter(self): |
| 304 |
with expected_protocol( |
| 305 |
TestMultiFunctionality.InstrumentWithFunctionality, |
| 306 |
[("X:Volt?", "123.456")] |
| 307 |
) as inst: |
| 308 |
assert inst.f_X.voltage == 123.456 |
| 309 |
|
| 310 |
def test_functions_voltage_setter(self): |
| 311 |
with expected_protocol( |
| 312 |
TestMultiFunctionality.InstrumentWithFunctionality, |
| 313 |
[("X:Volt 123.456000", None)] |
| 314 |
) as inst: |
| 315 |
inst.f_X.voltage = 123.456 |