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