| 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 pytest |
| 26 |
from pymeasure.instruments import Instrument |
| 27 |
from pymeasure.test import expected_protocol |
| 28 |
|
| 29 |
# File to test |
| 30 |
from pymeasure.instruments.thyracont.smartline_v2 import (SmartlineV2, HotCathode, Pirani, |
| 31 |
calculate_checksum, Piezo, Sources) |
| 32 |
|
| 33 |
|
| 34 |
SmartlineV2.hot_cathode = Instrument.ChannelCreator(cls=HotCathode) |
| 35 |
SmartlineV2.pirani = Instrument.ChannelCreator(cls=Pirani) |
| 36 |
SmartlineV2.piezo = Instrument.ChannelCreator(cls=Piezo) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.parametrize("buffer, read", ( |
| 40 |
(b"0011MV079.734e2h", "9.734e2"), # all correct |
| 41 |
(b"0011\x00MV079.734e2h", "9.734e2"), # zero byte |
| 42 |
(b"00\xf911MV079.734e2h", "9.734e2"), # byte above 127 |
| 43 |
(b"0011MV079.734e2h\r", "9.734e2"), # term char still there |
| 44 |
)) |
| 45 |
def test_read_handles_errors(buffer, read): |
| 46 |
with expected_protocol(SmartlineV2, |
| 47 |
[(None, buffer)], |
| 48 |
) as inst: |
| 49 |
assert inst.read() == read |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
"address, pars, message, answer", |
| 54 |
[ |
| 55 |
(1, (0, "MR"), "0010MR00@", "0011MR11H1.2e3L1e-4w"), |
| 56 |
(1, (0, "MV"), "0010MV00D", "0011MV079.734e2h"), |
| 57 |
(2, (2, "R1", "T0.1F1.5"), "0022R108T0.1F1.5l", "0023R100h"), |
| 58 |
(2, (2, "DU", "mbar"), "0022DU04mbarc", "0023DU00~"), |
| 59 |
(1, (2, "AH", 981.5), "0012AH05981.5v", "0013AH00m"), |
| 60 |
], |
| 61 |
) |
| 62 |
def test_ask_manually(address, pars, message, answer): |
| 63 |
with expected_protocol(SmartlineV2, [(message, answer)], address=address) as inst: |
| 64 |
inst.ask_manually(*pars) |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.parametrize("message, result", ( |
| 68 |
("0010MV00", "D"), |
| 69 |
("0022DU04mbar", "c"), |
| 70 |
("0012AH05981.5", "v"), |
| 71 |
)) |
| 72 |
def test_calculateChecksum(message, result): |
| 73 |
assert calculate_checksum(message) == result |
| 74 |
|
| 75 |
|
| 76 |
# Test communication |
| 77 |
def test_transmitter_error(): |
| 78 |
with expected_protocol(SmartlineV2, [("0010MV00D", "0017MV00ERROR1X")]) as inst: |
| 79 |
with pytest.raises(ConnectionError) as exc: |
| 80 |
inst.pressure |
| 81 |
assert exc.value.args[0] == "Sensor defect or stacked out." |
| 82 |
|
| 83 |
|
| 84 |
def test_wrong_answer_command(): |
| 85 |
with expected_protocol(SmartlineV2, [("0010MV00D", "0011MX00ERROR1X")]) as inst: |
| 86 |
with pytest.raises(ConnectionError, match="Wrong response to MV: '0011MX00ERROR1X'."): |
| 87 |
inst.pressure |
| 88 |
|
| 89 |
|
| 90 |
def test_wrong_answer_checksum(): |
| 91 |
with expected_protocol(SmartlineV2, [("0010MV00D", "0011MV00ERROR1X")]) as inst: |
| 92 |
with pytest.raises(ConnectionError, match="Response checksum is wrong."): |
| 93 |
inst.pressure |
| 94 |
|
| 95 |
|
| 96 |
# Test properties |
| 97 |
def test_range(): |
| 98 |
with expected_protocol(SmartlineV2, [("0010MR00@", "0011MR11H1.2e3L1e-4w")]) as inst: |
| 99 |
assert inst.range == [1.2e3, 1e-4] |
| 100 |
|
| 101 |
|
| 102 |
def test_pressure(): |
| 103 |
with expected_protocol(SmartlineV2, [("0010MV00D", "0011MV079.734e2h")]) as inst: |
| 104 |
assert inst.pressure == 973.4 |
| 105 |
|
| 106 |
|
| 107 |
def test_pressure_over_range(): |
| 108 |
with expected_protocol(SmartlineV2, [("0010MV00D", "0011MV02ORh")]) as inst: |
| 109 |
assert inst.pressure == float("inf") |
| 110 |
|
| 111 |
|
| 112 |
def test_pressure_under_range(): |
| 113 |
with expected_protocol(SmartlineV2, [("0010MV00D", "0011MV02URn")]) as inst: |
| 114 |
assert inst.pressure == 0 |
| 115 |
|
| 116 |
|
| 117 |
def test_display_unit_getter(): |
| 118 |
with expected_protocol(SmartlineV2, [("0010DU00z", "0011DU03mbar`")]) as inst: |
| 119 |
assert inst.display_unit == "mbar" |
| 120 |
|
| 121 |
|
| 122 |
def test_display_unit_setter(): |
| 123 |
with expected_protocol(SmartlineV2, [("0022DU04mbarc", "0023DU00~")]) as inst: |
| 124 |
inst.address = 2 |
| 125 |
inst.display_unit = "mbar" |
| 126 |
|
| 127 |
|
| 128 |
def test_display_unit_setter_wrong(): |
| 129 |
with expected_protocol(SmartlineV2, []) as inst: |
| 130 |
with pytest.raises(ValueError): |
| 131 |
inst.display_unit = "abc" |
| 132 |
|
| 133 |
|
| 134 |
def test_display_orientation_getter(): |
| 135 |
with expected_protocol(SmartlineV2, [("0010DO00t", "0011DO010f")]) as inst: |
| 136 |
assert inst.display_orientation == "top" |
| 137 |
|
| 138 |
|
| 139 |
def test_display_orientation_setter(): |
| 140 |
with expected_protocol(SmartlineV2, [("0012DO010g", "0013DO00w")]) as inst: |
| 141 |
inst.display_orientation = "top" |
| 142 |
|
| 143 |
|
| 144 |
def test_display_data_getter(): |
| 145 |
with expected_protocol(SmartlineV2, [("0010DD00i", "0011DD017b")]) as inst: |
| 146 |
assert inst.display_data == Sources.RELATIVE |
| 147 |
|
| 148 |
|
| 149 |
def test_display_data_setter(): |
| 150 |
with expected_protocol(SmartlineV2, [("0012DD017c", "0013DD00l")]) as inst: |
| 151 |
inst.display_data = Sources.RELATIVE |
| 152 |
|
| 153 |
|
| 154 |
def test_set_high(): |
| 155 |
with expected_protocol(SmartlineV2, [("0012AH041000q", "0013AH00m")]) as inst: |
| 156 |
inst.set_high(1000) |
| 157 |
|
| 158 |
|
| 159 |
def test_set_low(): |
| 160 |
with expected_protocol(SmartlineV2, [("0012AL0250W", "0013AL00q")]) as inst: |
| 161 |
inst.set_low(50) |
| 162 |
|
| 163 |
|
| 164 |
def test_device_type(): |
| 165 |
with expected_protocol(SmartlineV2, [("0010TD00y", "0011TD06VSR205R")]) as inst: |
| 166 |
assert inst.device_type == "VSR205" |
| 167 |
|
| 168 |
|
| 169 |
def test_operating_hours(): |
| 170 |
with expected_protocol(SmartlineV2, |
| 171 |
[("0010OH00x", "0011OH0285h")], |
| 172 |
) as inst: |
| 173 |
assert inst.operating_hours == 21.25 |
| 174 |
|
| 175 |
|
| 176 |
def test_operating_hours_cathod(): |
| 177 |
"""For a cathode transmitter also cathode hours are returned.""" |
| 178 |
with expected_protocol(SmartlineV2, |
| 179 |
[("0010OH00x", "0011OH0542C36P")], |
| 180 |
) as inst: |
| 181 |
assert inst.operating_hours == [10.5, 9] |
| 182 |
|
| 183 |
|
| 184 |
# Test methods |
| 185 |
def test_set_default_sensor_transition(): |
| 186 |
with expected_protocol(SmartlineV2, |
| 187 |
[(b'0012ST011|', "0013ST00K")], |
| 188 |
) as inst: |
| 189 |
inst.set_default_sensor_transition() |
| 190 |
|
| 191 |
|
| 192 |
def test_set_direct_sensor_transition(): |
| 193 |
with expected_protocol(SmartlineV2, |
| 194 |
[(b'0012ST02D5E', "0013ST00K")], |
| 195 |
) as inst: |
| 196 |
inst.set_direct_sensor_transition(5) |
| 197 |
|
| 198 |
|
| 199 |
def test_set_continuous_sensor_transition(): |
| 200 |
with expected_protocol(SmartlineV2, |
| 201 |
[(b'0012ST04F1T2K', "0013ST00K")], |
| 202 |
) as inst: |
| 203 |
inst.set_continuous_sensor_transition(1, 2) |
| 204 |
|
| 205 |
|
| 206 |
# Pirani tests |
| 207 |
def test_pressure_pirani(): |
| 208 |
with expected_protocol(SmartlineV2, [("0010M100_", "0011M1079.734e2C")]) as inst: |
| 209 |
assert inst.pirani.pressure == 973.4 |
| 210 |
|
| 211 |
|
| 212 |
def test_pirani_statistics(): |
| 213 |
with expected_protocol(SmartlineV2, |
| 214 |
[("0010PM011p", b'0011PM06W0A382j')], |
| 215 |
) as inst: |
| 216 |
assert inst.pirani.statistics == (0, 95.5) |
| 217 |
|
| 218 |
|
| 219 |
# Piezo tests |
| 220 |
def test_pressure_piezo(): |
| 221 |
with expected_protocol(SmartlineV2, [("0010M200`", "0011M2079.734e2D")]) as inst: |
| 222 |
assert inst.piezo.pressure == 973.4 |
| 223 |
|
| 224 |
|
| 225 |
# Hot Cathode tests |
| 226 |
def test_pressure_hot_cathode(): |
| 227 |
with expected_protocol(SmartlineV2, [("0010M300a", "0011M3089.274e-8x")]) as inst: |
| 228 |
assert inst.hot_cathode.pressure == 9.274e-8 |
| 229 |
|
| 230 |
|
| 231 |
def test_filament_mode_getter(): |
| 232 |
with expected_protocol(SmartlineV2, [("0010FC00j", "0011FC011]")]) as inst: |
| 233 |
assert inst.hot_cathode.filament_mode == "Filament1" |
| 234 |
|
| 235 |
|
| 236 |
def test_filament_mode_setter(): |
| 237 |
with expected_protocol(SmartlineV2, [("0012FC012_", "0013FC00m")]) as inst: |
| 238 |
inst.hot_cathode.filament_mode = "Filament2" |
| 239 |
|
| 240 |
|
| 241 |
def test_filament_status(): |
| 242 |
with expected_protocol(SmartlineV2, [("0010FS00z", "0011FS011m")]) as inst: |
| 243 |
assert inst.hot_cathode.filament_status == "Filament 1 defective" |