| 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 |
from unittest import mock |
| 26 |
|
| 27 |
import pytest |
| 28 |
|
| 29 |
from pymeasure.test import expected_protocol |
| 30 |
from pymeasure.instruments import Channel, Instrument |
| 31 |
from pymeasure.instruments.validators import truncated_range |
| 32 |
|
| 33 |
|
| 34 |
class GenericChannel(Channel): |
| 35 |
# Use truncated_range as this easily lets us test for the range boundaries |
| 36 |
fake_ctrl = Channel.control( |
| 37 |
"C{ch}:control?", "C{ch}:control %d", "Control docs", |
| 38 |
validator=truncated_range, |
| 39 |
values=(1, 10), |
| 40 |
dynamic=True, |
| 41 |
) |
| 42 |
fake_setting = Channel.setting( |
| 43 |
"C{ch}:setting %d", "Set docs", |
| 44 |
validator=truncated_range, |
| 45 |
values=(1, 10), |
| 46 |
dynamic=True, |
| 47 |
) |
| 48 |
fake_measurement = Channel.measurement( |
| 49 |
"C{ch}:measurement?", "Measure docs", |
| 50 |
values={'X': 1, 'Y': 2, 'Z': 3}, |
| 51 |
map_values=True, |
| 52 |
dynamic=True, |
| 53 |
) |
| 54 |
special_control = Channel.control( |
| 55 |
"SOUR{ch}:special?", "OUTP{ch}:special %s", |
| 56 |
"""Control with different channel specifiers for get and set.""", |
| 57 |
cast=str, |
| 58 |
) |
| 59 |
check_errors_control = Channel.control( |
| 60 |
"C{ch}:xy?", "C{ch}:xy %d", |
| 61 |
"""Control something, with error check on get and set.""", |
| 62 |
check_set_errors=True, |
| 63 |
check_get_errors=True, |
| 64 |
) |
| 65 |
|
| 66 |
|
| 67 |
class ChannelWithPlaceholder(Channel): |
| 68 |
"""A test channel with a different placeholder""" |
| 69 |
|
| 70 |
placeholder = "fn" |
| 71 |
|
| 72 |
test = Channel.control("{fn}test?", "test{fn} %i", """Control test.""") |
| 73 |
|
| 74 |
|
| 75 |
class ChannelInstrument(Instrument): |
| 76 |
def __init__(self, adapter, name="ChannelInstrument", **kwargs): |
| 77 |
super().__init__(adapter, name, **kwargs) |
| 78 |
self.errors = [] |
| 79 |
self.add_child(GenericChannel, "A") |
| 80 |
self.add_child(GenericChannel, "B") |
| 81 |
|
| 82 |
def check_errors(self): |
| 83 |
err = self.values("SYST:ERR?") |
| 84 |
if err: |
| 85 |
self.errors.append(err) |
| 86 |
return err |
| 87 |
|
| 88 |
|
| 89 |
class TestChannelCommunication: |
| 90 |
@pytest.fixture() |
| 91 |
def ch(self): |
| 92 |
a = mock.MagicMock(return_value="5") |
| 93 |
return Channel(a, "A") |
| 94 |
|
| 95 |
def test_write(self, ch): |
| 96 |
ch.write("abc") |
| 97 |
assert ch.parent.method_calls == [mock.call.write('abc')] |
| 98 |
|
| 99 |
def test_read(self, ch): |
| 100 |
ch.read() |
| 101 |
assert ch.parent.method_calls == [mock.call.read()] |
| 102 |
|
| 103 |
def test_write_bytes(self, ch): |
| 104 |
ch.write_bytes(b"abc") |
| 105 |
assert ch.parent.method_calls == [mock.call.write_bytes(b"abc")] |
| 106 |
|
| 107 |
def test_read_bytes(self, ch): |
| 108 |
ch.read_bytes(5) |
| 109 |
assert ch.parent.method_calls == [mock.call.read_bytes(5)] |
| 110 |
|
| 111 |
def test_write_binary_values(self, ch): |
| 112 |
ch.write_binary_values("abc", [5, 6, 7]) |
| 113 |
assert ch.parent.method_calls == [mock.call.write_binary_values("abc", [5, 6, 7])] |
| 114 |
|
| 115 |
def test_read_binary_values(self, ch): |
| 116 |
ch.read_binary_values() |
| 117 |
assert ch.parent.method_calls == [mock.call.read_binary_values()] |
| 118 |
|
| 119 |
def test_check_errors(self, ch): |
| 120 |
ch.check_errors() |
| 121 |
assert ch.parent.method_calls == [mock.call.check_errors()] |
| 122 |
|
| 123 |
def test_check_get_errors(self, ch): |
| 124 |
ch.check_get_errors() |
| 125 |
assert ch.parent.method_calls == [mock.call.check_get_errors()] |
| 126 |
|
| 127 |
def test_check_set_errors(self, ch): |
| 128 |
ch.check_set_errors() |
| 129 |
assert ch.parent.method_calls == [mock.call.check_set_errors()] |
| 130 |
|
| 131 |
|
| 132 |
def test_channel_with_different_prefix(): |
| 133 |
c = ChannelWithPlaceholder(None, "A") |
| 134 |
assert c.insert_id("id:{fn}") == "id:A" |
| 135 |
|
| 136 |
|
| 137 |
def test_channel_write(): |
| 138 |
with expected_protocol(ChannelInstrument, [("ChA:volt?", None)]) as inst: |
| 139 |
inst.ch_A.write("Ch{ch}:volt?") |
| 140 |
|
| 141 |
|
| 142 |
def test_channel_write_name_twice(): |
| 143 |
"""Verify, that any (i.e. more than one) occurrence of '{ch}' is changed.""" |
| 144 |
with expected_protocol(ChannelInstrument, [("ChA:volt:ChA?", None)]) as inst: |
| 145 |
inst.ch_A.write("Ch{ch}:volt:Ch{ch}?") |
| 146 |
|
| 147 |
|
| 148 |
def test_channel_write_without_ch(): |
| 149 |
"""Verify, that it is possible to send a command without '{ch}'.""" |
| 150 |
with expected_protocol(ChannelInstrument, [("Test", None)]) as inst: |
| 151 |
inst.ch_A.write("Test") |
| 152 |
|
| 153 |
|
| 154 |
def test_channel_control(): |
| 155 |
with expected_protocol( |
| 156 |
ChannelInstrument, |
| 157 |
[("CA:control 7", None), ("CA:control?", "1.45")] |
| 158 |
) as inst: |
| 159 |
inst.ch_A.fake_ctrl = 7 |
| 160 |
assert inst.ch_A.fake_ctrl == 1.45 |
| 161 |
|
| 162 |
|
| 163 |
def test_channel_setting(): |
| 164 |
with expected_protocol( |
| 165 |
ChannelInstrument, |
| 166 |
[("CA:setting 3", None)] |
| 167 |
) as inst: |
| 168 |
inst.ch_A.fake_setting = 3 |
| 169 |
|
| 170 |
|
| 171 |
def test_channel_measurement(): |
| 172 |
with expected_protocol( |
| 173 |
ChannelInstrument, |
| 174 |
[("CA:measurement?", "2")] |
| 175 |
) as inst: |
| 176 |
assert inst.ch_A.fake_measurement == "Y" |
| 177 |
|
| 178 |
|
| 179 |
def test_channel_dynamic_property(): |
| 180 |
with expected_protocol(ChannelInstrument, [("CA:control 100", None)]) as inst: |
| 181 |
inst.ch_A.fake_ctrl_values = (1, 200) |
| 182 |
# original values is (1, 10), therefore 100 should not be allowed. |
| 183 |
inst.ch_A.fake_ctrl = 100 |
| 184 |
|
| 185 |
|
| 186 |
def test_channel_special_control(): |
| 187 |
"""Test different Prefixes for getter and setter.""" |
| 188 |
with expected_protocol(ChannelInstrument, |
| 189 |
[("SOURA:special?", "super"), |
| 190 |
("OUTPB:special test", None)], |
| 191 |
) as inst: |
| 192 |
assert inst.ch_A.special_control == "super" |
| 193 |
inst.ch_B.special_control = "test" |
| 194 |
|
| 195 |
|
| 196 |
def test_channel_check_set_errors(): |
| 197 |
with expected_protocol(ChannelInstrument, |
| 198 |
[("CA:xy 5", None), |
| 199 |
("SYST:ERR?", "Some, error")], |
| 200 |
) as inst: |
| 201 |
inst.ch_A.check_errors_control = 5 |
| 202 |
assert inst.errors == [["Some", " error"]] |
| 203 |
|
| 204 |
|
| 205 |
def test_channel_check_get_errors(): |
| 206 |
with expected_protocol(ChannelInstrument, |
| 207 |
[("CA:xy?", "5"), |
| 208 |
("SYST:ERR?", "Some, error")], |
| 209 |
) as inst: |
| 210 |
assert inst.ch_A.check_errors_control == 5 |
| 211 |
assert inst.errors == [["Some", " error"]] |