| 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 |
import importlib.util |
| 25 |
|
| 26 |
import pytest |
| 27 |
import pyvisa |
| 28 |
|
| 29 |
from pymeasure.adapters import VISAAdapter |
| 30 |
from pymeasure.test import expected_protocol |
| 31 |
|
| 32 |
# This uses a pyvisa-sim default instrument, we could also define our own. |
| 33 |
SIM_RESOURCE = 'ASRL2::INSTR' |
| 34 |
|
| 35 |
is_pyvisa_sim_installed = bool(importlib.util.find_spec('pyvisa_sim')) |
| 36 |
if not is_pyvisa_sim_installed: |
| 37 |
pytest.skip('PyVISA tests require the pyvisa-sim library', allow_module_level=True) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def adapter(): |
| 42 |
adapter = VISAAdapter(SIM_RESOURCE, visa_library='@sim', |
| 43 |
read_termination="\n", |
| 44 |
# Large timeout allows very slow GitHub action runners to complete. |
| 45 |
timeout=60, |
| 46 |
) |
| 47 |
yield adapter |
| 48 |
# Empty the read buffer, as something might remain there after a test. |
| 49 |
# `clear` is not implemented in pyvisa-sim and `flush_read_buffer` seems to do nothing. |
| 50 |
adapter.timeout = 0 |
| 51 |
try: |
| 52 |
adapter.read_bytes(-1) |
| 53 |
except pyvisa.errors.VisaIOError as exc: |
| 54 |
if not exc.args[0].startswith("VI_ERROR_TMO"): |
| 55 |
raise |
| 56 |
# Close the connection |
| 57 |
adapter.close() |
| 58 |
|
| 59 |
|
| 60 |
def test_nested_adapter(): |
| 61 |
a0 = VISAAdapter(SIM_RESOURCE, visa_library='@sim', read_termination="\n") |
| 62 |
a = VISAAdapter(a0) |
| 63 |
assert a.resource_name == SIM_RESOURCE |
| 64 |
assert a.connection == a0.connection |
| 65 |
assert a.manager == a0.manager |
| 66 |
|
| 67 |
|
| 68 |
def test_nested_adapter_query_delay(): |
| 69 |
query_delay = 10 |
| 70 |
with pytest.warns(FutureWarning, match="query_delay"): |
| 71 |
a0 = VISAAdapter(SIM_RESOURCE, visa_library='@sim', read_termination="\n", |
| 72 |
query_delay=query_delay) |
| 73 |
a = VISAAdapter(a0) |
| 74 |
assert a.resource_name == SIM_RESOURCE |
| 75 |
assert a.connection == a0.connection |
| 76 |
assert a.query_delay == query_delay |
| 77 |
|
| 78 |
|
| 79 |
def test_ProtocolAdapter(): |
| 80 |
with expected_protocol( |
| 81 |
VISAAdapter, |
| 82 |
[(b"some bytes written", b"Response")] |
| 83 |
) as adapter: |
| 84 |
adapter.write_bytes(b"some bytes written") |
| 85 |
assert adapter.read_bytes(-1) == b"Response" |
| 86 |
|
| 87 |
|
| 88 |
def test_correct_visa_asrl_kwarg(): |
| 89 |
"""Confirm that the asrl kwargs gets passed through to the VISA connection.""" |
| 90 |
a = VISAAdapter(SIM_RESOURCE, visa_library='@sim', |
| 91 |
asrl={'read_termination': "\rx\n"}) |
| 92 |
assert a.connection.read_termination == "\rx\n" |
| 93 |
|
| 94 |
|
| 95 |
def test_open_gpib(): |
| 96 |
a = VISAAdapter(5, visa_library='@sim') |
| 97 |
assert a.resource_name == "GPIB0::5::INSTR" |
| 98 |
|
| 99 |
|
| 100 |
class TestClose: |
| 101 |
@pytest.fixture |
| 102 |
def adapterC(self): |
| 103 |
return VISAAdapter(SIM_RESOURCE, visa_library='@sim') |
| 104 |
|
| 105 |
def test_connection_session_closed(self, adapterC): |
| 106 |
# Verify first, that it works before closing |
| 107 |
assert adapterC.connection.session is not None |
| 108 |
adapterC.close() |
| 109 |
with pytest.raises(pyvisa.errors.InvalidSession, match="Invalid session"): |
| 110 |
adapterC.connection.session |
| 111 |
|
| 112 |
def test_manager_session_closed(self, adapterC): |
| 113 |
# Verify first, that it works before closing |
| 114 |
assert adapterC.manager.session is not None |
| 115 |
adapterC.close() |
| 116 |
with pytest.raises(pyvisa.errors.InvalidSession, match="Invalid session"): |
| 117 |
adapterC.manager.session |
| 118 |
|
| 119 |
|
| 120 |
def test_write_read(adapter): |
| 121 |
adapter.write(":VOLT:IMM:AMPL?") |
| 122 |
assert float(adapter.read()) == 1 |
| 123 |
|
| 124 |
|
| 125 |
def test_write_bytes_read_bytes(adapter): |
| 126 |
adapter.write_bytes(b"*IDN?\r\n") |
| 127 |
assert adapter.read_bytes(22) == b"SCPI,MOCK,VERSION_1.0\n" |
| 128 |
|
| 129 |
|
| 130 |
def test_write_bytes_read(adapter): |
| 131 |
adapter.write_bytes(b"*IDN?\r\n") |
| 132 |
assert adapter.read() == "SCPI,MOCK,VERSION_1.0" |
| 133 |
|
| 134 |
|
| 135 |
class TestReadBytes: |
| 136 |
@pytest.fixture() |
| 137 |
def adapterR(self, adapter): |
| 138 |
adapter.write("*IDN?") |
| 139 |
yield adapter |
| 140 |
|
| 141 |
def test_read_bytes(self, adapterR): |
| 142 |
assert adapterR.read_bytes(22) == b"SCPI,MOCK,VERSION_1.0\n" |
| 143 |
|
| 144 |
def test_read_all_bytes(self, adapterR): |
| 145 |
assert adapterR.read_bytes(-1) == b"SCPI,MOCK,VERSION_1.0\n" |
| 146 |
|
| 147 |
@pytest.mark.parametrize("count", (-1, 7)) |
| 148 |
def test_read_break_on_termchar(self, adapterR, count): |
| 149 |
"""Test read_bytes breaks on termchar.""" |
| 150 |
adapterR.connection.read_termination = "," |
| 151 |
assert adapterR.read_bytes(count, break_on_termchar=True) == b"SCPI," |
| 152 |
|
| 153 |
def test_read_no_break_on_termchar(self, adapterR): |
| 154 |
adapterR.connection.read_termination = "," |
| 155 |
# `break_on_termchar=False` is default value |
| 156 |
assert adapterR.read_bytes(-1) == b"SCPI,MOCK,VERSION_1.0\n" |
| 157 |
|
| 158 |
def test_read_no_break_on_newline(self, adapter): |
| 159 |
# write twice to have two newline characters in the read buffer |
| 160 |
adapter.write("*IDN?") |
| 161 |
adapter.write("*IDN?") |
| 162 |
# `break_on_termchar=False` is default value |
| 163 |
assert adapter.read_bytes(-1) == b"SCPI,MOCK,VERSION_1.0\nSCPI,MOCK,VERSION_1.0\n" |
| 164 |
|
| 165 |
|
| 166 |
def test_visa_adapter(adapter): |
| 167 |
assert repr(adapter) == f"<VISAAdapter(resource='{SIM_RESOURCE}')>" |
| 168 |
|
| 169 |
with pytest.warns(FutureWarning): |
| 170 |
assert adapter.ask("*IDN?") == "SCPI,MOCK,VERSION_1.0" |
| 171 |
|
| 172 |
adapter.write("*IDN?") |
| 173 |
assert adapter.read() == "SCPI,MOCK,VERSION_1.0" |
| 174 |
|
| 175 |
|
| 176 |
def test_visa_adapter_ask_values(adapter): |
| 177 |
with pytest.warns(FutureWarning): |
| 178 |
assert adapter.ask_values(":VOLT:IMM:AMPL?", separator=",") == [1.0] |