| 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 pytest |
| 25 |
|
| 26 |
from pymeasure.test import expected_protocol |
| 27 |
from pymeasure.instruments.kuhneelectronic import Kusg245_250A |
| 28 |
from pymeasure.instruments.kuhneelectronic.kusg245_250a import termination_character, encoding |
| 29 |
|
| 30 |
|
| 31 |
termination_character = termination_character.encode(encoding=encoding)[0] |
| 32 |
|
| 33 |
|
| 34 |
def test_voltage_5v(): |
| 35 |
with expected_protocol( |
| 36 |
Kusg245_250A, |
| 37 |
[("5", bytes([0, 1, termination_character]))], |
| 38 |
) as inst: |
| 39 |
assert inst.voltage_5v == 103.0 / 4700.0 |
| 40 |
|
| 41 |
|
| 42 |
def test_voltage_32v(): |
| 43 |
with expected_protocol( |
| 44 |
Kusg245_250A, |
| 45 |
[("8", bytes([0, 1, termination_character]))], |
| 46 |
) as inst: |
| 47 |
assert inst.voltage_32v == 1282.0 / 8200.0 |
| 48 |
|
| 49 |
|
| 50 |
def test_power_forward(): |
| 51 |
with expected_protocol( |
| 52 |
Kusg245_250A, |
| 53 |
[("6", bytes([255, termination_character]))], |
| 54 |
) as inst: |
| 55 |
assert inst.power_forward == 255 |
| 56 |
|
| 57 |
|
| 58 |
def test_power_reverse(): |
| 59 |
with expected_protocol( |
| 60 |
Kusg245_250A, |
| 61 |
[("7", bytes([255, termination_character]))], |
| 62 |
) as inst: |
| 63 |
assert inst.power_reverse == 255 |
| 64 |
|
| 65 |
|
| 66 |
def test_external_enabled(): |
| 67 |
with expected_protocol( |
| 68 |
Kusg245_250A, |
| 69 |
[ |
| 70 |
("R", "A"), |
| 71 |
("r?", bytes([1, termination_character])), |
| 72 |
("r", "A"), |
| 73 |
("r?", bytes([0, termination_character])) |
| 74 |
], |
| 75 |
) as inst: |
| 76 |
inst.external_enabled = True |
| 77 |
assert inst.external_enabled is True |
| 78 |
inst.external_enabled = False |
| 79 |
assert inst.external_enabled is False |
| 80 |
|
| 81 |
|
| 82 |
def test_bias_enabled(): |
| 83 |
with expected_protocol( |
| 84 |
Kusg245_250A, |
| 85 |
[ |
| 86 |
("X", "A"), |
| 87 |
("x?", bytes([1, termination_character])), |
| 88 |
("x", "A"), |
| 89 |
("x?", bytes([0, termination_character])) |
| 90 |
], |
| 91 |
) as inst: |
| 92 |
inst.bias_enabled = True |
| 93 |
assert inst.bias_enabled is True |
| 94 |
inst.bias_enabled = False |
| 95 |
assert inst.bias_enabled is False |
| 96 |
|
| 97 |
|
| 98 |
def test_rf_enabled(): |
| 99 |
with expected_protocol( |
| 100 |
Kusg245_250A, |
| 101 |
[ |
| 102 |
("O", "A"), |
| 103 |
("o?", bytes([1, termination_character])), |
| 104 |
("o", "A"), |
| 105 |
("o?", bytes([0, termination_character])) |
| 106 |
], |
| 107 |
) as inst: |
| 108 |
inst.rf_enabled = True |
| 109 |
assert inst.rf_enabled is True |
| 110 |
inst.rf_enabled = False |
| 111 |
assert inst.rf_enabled is False |
| 112 |
|
| 113 |
|
| 114 |
def test_pulse_mode_enabled(): |
| 115 |
with expected_protocol( |
| 116 |
Kusg245_250A, |
| 117 |
[ |
| 118 |
("P", "A"), |
| 119 |
("p?", bytes([1, termination_character])), |
| 120 |
("p", "A"), |
| 121 |
("p?", bytes([0, termination_character])) |
| 122 |
], |
| 123 |
) as inst: |
| 124 |
inst.pulse_mode_enabled = True |
| 125 |
assert inst.pulse_mode_enabled is True |
| 126 |
inst.pulse_mode_enabled = False |
| 127 |
assert inst.pulse_mode_enabled is False |
| 128 |
|
| 129 |
|
| 130 |
def test_freq_steps_fine_enabled(): |
| 131 |
with expected_protocol( |
| 132 |
Kusg245_250A, |
| 133 |
[ |
| 134 |
("fm1", "A"), |
| 135 |
("fm?", bytes([1, termination_character])), |
| 136 |
("fm0", "A"), |
| 137 |
("fm?", bytes([0, termination_character])) |
| 138 |
], |
| 139 |
) as inst: |
| 140 |
inst.freq_steps_fine_enabled = True |
| 141 |
assert inst.freq_steps_fine_enabled is True |
| 142 |
inst.freq_steps_fine_enabled = False |
| 143 |
assert inst.freq_steps_fine_enabled is False |
| 144 |
|
| 145 |
|
| 146 |
def test_frequency_coarse(): |
| 147 |
with expected_protocol( |
| 148 |
Kusg245_250A, |
| 149 |
[ |
| 150 |
("f2456", "A"), |
| 151 |
("f?", "2456MHz"), |
| 152 |
("f2400", "A"), |
| 153 |
("f?", "2400MHz"), |
| 154 |
("f2500", "A"), |
| 155 |
("f?", "2500MHz"), |
| 156 |
], |
| 157 |
) as inst: |
| 158 |
inst.frequency_coarse = 2456 |
| 159 |
assert inst.frequency_coarse == 2456 |
| 160 |
inst.frequency_coarse = 2000 # must be truncated to 2400 |
| 161 |
assert inst.frequency_coarse == 2400 |
| 162 |
inst.frequency_coarse = 3000 # must be truncated to 2500 |
| 163 |
assert inst.frequency_coarse == 2500 |
| 164 |
|
| 165 |
|
| 166 |
def test_frequency_fine(): |
| 167 |
with expected_protocol( |
| 168 |
Kusg245_250A, |
| 169 |
[ |
| 170 |
("f2456780", "A"), |
| 171 |
("f?", "2456780kHz"), |
| 172 |
("f2400000", "A"), |
| 173 |
("f?", "2400000kHz"), |
| 174 |
("f2500000", "A"), |
| 175 |
("f?", "2500000kHz"), |
| 176 |
], |
| 177 |
) as inst: |
| 178 |
inst.frequency_fine = 2456778 # must be rounded to 2456780 |
| 179 |
assert inst.frequency_fine == 2456780 |
| 180 |
inst.frequency_fine = 2000000 # must be truncated to 2400000 |
| 181 |
assert inst.frequency_fine == 2400000 |
| 182 |
inst.frequency_fine = 3000000 # must be truncated to 2500000 |
| 183 |
assert inst.frequency_fine == 2500000 |
| 184 |
|
| 185 |
|
| 186 |
def test_power_setpoint(): |
| 187 |
with expected_protocol( |
| 188 |
Kusg245_250A, |
| 189 |
[ |
| 190 |
("A123", "A"), |
| 191 |
("A?", "123"), |
| 192 |
("A000", "A"), |
| 193 |
("A?", "000"), |
| 194 |
("A250", "A"), |
| 195 |
("A?", "250"), |
| 196 |
], |
| 197 |
) as inst: |
| 198 |
inst.power_setpoint = 123 |
| 199 |
assert inst.power_setpoint == 123 |
| 200 |
inst.power_setpoint = -1 # must be truncated to 0 |
| 201 |
assert inst.power_setpoint == 0 |
| 202 |
inst.power_setpoint = 300 # must be truncated to 250 |
| 203 |
assert inst.power_setpoint == 250 |
| 204 |
|
| 205 |
|
| 206 |
def test_power_setpoint_limited(): |
| 207 |
with expected_protocol( |
| 208 |
Kusg245_250A, [("A000", "A"), ("A?", "000"), ("A020", "A"), ("A?", "020")], power_limit=20 |
| 209 |
) as inst: |
| 210 |
inst.power_setpoint = -1 # must be truncated to 0 |
| 211 |
assert inst.power_setpoint == 0 |
| 212 |
inst.power_setpoint = 300 # must be truncated to power_limit |
| 213 |
assert inst.power_setpoint == 20 |
| 214 |
|
| 215 |
|
| 216 |
def test_pulse_width(): |
| 217 |
with expected_protocol( |
| 218 |
Kusg245_250A, |
| 219 |
[ |
| 220 |
("C0125", "A"), |
| 221 |
("C?", "0125"), |
| 222 |
("C0010", "A"), |
| 223 |
("C?", "0010"), |
| 224 |
("C1000", "A"), |
| 225 |
("C?", "1000"), |
| 226 |
], |
| 227 |
) as inst: |
| 228 |
inst.pulse_width = 123 # must be rounded to 125 |
| 229 |
assert inst.pulse_width == 125 |
| 230 |
inst.pulse_width = 0 # must be truncated to 10 |
| 231 |
assert inst.pulse_width == 10 |
| 232 |
inst.pulse_width = 10000 # must be truncated to 250 |
| 233 |
assert inst.pulse_width == 1000 |
| 234 |
|
| 235 |
|
| 236 |
def test_off_time(): |
| 237 |
with expected_protocol( |
| 238 |
Kusg245_250A, |
| 239 |
[ |
| 240 |
("c0125", "A"), |
| 241 |
("c?", "0125"), |
| 242 |
("c0010", "A"), |
| 243 |
("c?", "0010"), |
| 244 |
("c1000", "A"), |
| 245 |
("c?", "1000"), |
| 246 |
], |
| 247 |
) as inst: |
| 248 |
inst.off_time = 123 # must be rounded to 125 |
| 249 |
assert inst.off_time == 125 |
| 250 |
inst.off_time = 0 # must be truncated to 10 |
| 251 |
assert inst.off_time == 10 |
| 252 |
inst.off_time = 10000 # must be truncated to 250 |
| 253 |
assert inst.off_time == 1000 |
| 254 |
|
| 255 |
|
| 256 |
def test_phase_shift(): |
| 257 |
with expected_protocol( |
| 258 |
Kusg245_250A, |
| 259 |
[ |
| 260 |
("H088", "A"), |
| 261 |
("H?", bytes([88, termination_character])), |
| 262 |
("H000", "A"), |
| 263 |
("H?", bytes([0, termination_character])), |
| 264 |
("H255", "A"), |
| 265 |
("H?", bytes([255, termination_character])), |
| 266 |
], |
| 267 |
) as inst: |
| 268 |
inst.phase_shift = 124 |
| 269 |
assert inst.phase_shift == pytest.approx(124, 0.01) |
| 270 |
inst.phase_shift = -1 # must be truncated to 0 |
| 271 |
assert inst.phase_shift == 0 |
| 272 |
inst.phase_shift = 358.6 |
| 273 |
assert inst.phase_shift == pytest.approx(358.6, 0.01) |
| 274 |
|
| 275 |
|
| 276 |
def test_reflection_limit(): |
| 277 |
with expected_protocol( |
| 278 |
Kusg245_250A, |
| 279 |
[ |
| 280 |
("B0", "A"), |
| 281 |
("B?", bytes([0, termination_character])), |
| 282 |
("B4", "A"), |
| 283 |
("B?", bytes([4, termination_character])), |
| 284 |
("B5", "A"), |
| 285 |
("B?", bytes([5, termination_character])) |
| 286 |
], |
| 287 |
) as inst: |
| 288 |
inst.reflection_limit = 0 |
| 289 |
assert inst.reflection_limit == 0 |
| 290 |
inst.reflection_limit = 182 # must be rounded to the next |
| 291 |
# higher discrete value (200) |
| 292 |
assert inst.reflection_limit == 200 |
| 293 |
inst.reflection_limit = 300 # must be truncated to 230 |
| 294 |
assert inst.reflection_limit == 230 |
| 295 |
|
| 296 |
|
| 297 |
def test_tune(): |
| 298 |
with expected_protocol( |
| 299 |
Kusg245_250A, |
| 300 |
[("b010", "A")], |
| 301 |
) as inst: |
| 302 |
inst.tune(10) |
| 303 |
|
| 304 |
|
| 305 |
def test_tune_power_limited(): |
| 306 |
with expected_protocol(Kusg245_250A, [("b020", "A")], power_limit=20) as inst: |
| 307 |
inst.tune(100) |
| 308 |
|
| 309 |
|
| 310 |
def test_clear_VSWR_error(): |
| 311 |
with expected_protocol( |
| 312 |
Kusg245_250A, |
| 313 |
[("z", "A")], |
| 314 |
) as inst: |
| 315 |
inst.clear_VSWR_error() |
| 316 |
|
| 317 |
|
| 318 |
def test_store_settings(): |
| 319 |
with expected_protocol( |
| 320 |
Kusg245_250A, |
| 321 |
[("SE", "A")], |
| 322 |
) as inst: |
| 323 |
inst.store_settings() |
| 324 |
|
| 325 |
|
| 326 |
def test_turn_off(): |
| 327 |
with expected_protocol( |
| 328 |
Kusg245_250A, |
| 329 |
[("o", "A"), |
| 330 |
("x", "A")], |
| 331 |
) as inst: |
| 332 |
inst.turn_off() |
| 333 |
|
| 334 |
|
| 335 |
def test_turn_on(): |
| 336 |
with expected_protocol( |
| 337 |
Kusg245_250A, |
| 338 |
[("X", "A"), |
| 339 |
("O", "A")], |
| 340 |
) as inst: |
| 341 |
inst.turn_on() |