Debian Codegraph @

tests/instruments/test_connection_configuration.py

raw ◀ ▶
1 Debian patch touch this file
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 importlib
26
27 import pytest
28 import serial
29
30 from pymeasure.adapters import SerialAdapter, VISAAdapter
31 from pymeasure.instruments import Instrument
32
•33 is_pyvisa_sim_not_installed = not bool(importlib.util.find_spec("pyvisa_sim"))
34
35 # As an instrument contributor, I want to define default connection settings for my
36 # instrument with a minimum of boiler-plate. These settings should be easily
37 # user-overridable if appropriate without having to change the code/implementation
38 # of my instrument.
39 class MultiprotocolInstrument(Instrument):
40 """Test instrument for testing the configuration and use of multiple connection methods.
41
42 The instrument support multiple transports methods(serial, TCP/IP, GPIB).
43 In case of serial connections, the default baud rate is low (2400), but configurable in
44 the device, so it can use faster communication if desired.
45 Two examples in our instruments are AMI430, Keithley2260B (for TCP/IP, there are more
46 with GPIB capability)
47
48 It also has an attribute to configure a non-serial (gpib) connection. This demonstrates
49 that there may be attributes that are not valid for a serial connection, only others.
50 """
51 def __init__(self, adapter, name="Instrument with multiple connection methods",
52 baud_rate=2400, **kwargs):
53 # - baud_rate is placed in the signature if it's expected to be modified by the user
54 # - baud_rate is only valid for the asrl protocol
55 # - enable_repeat_addressing is only for gpib and always False for this device
56 # - timeout is the same for all pyvisa protocols, using setdefault it is user-modifiable
57 # without cluttering the signature:
58 kwargs.setdefault('timeout', 1500)
59
60 super().__init__(adapter,
61 name=name,
62 gpib=dict(enable_repeat_addressing=False,
63 read_termination='\r'),
64 asrl={'baud_rate': baud_rate,
65 'read_termination': '\r\n'},
66 **kwargs, # all others/generally valid kwargs
67 )
68
69 # The defined tests use the pyvisa-sim simulated connections to avoid the need for a connected
70 # instrument. The argument handling and connection creation happens in pyvisa just like for a
71 # real instrument, through.
72
73
•74 @pytest.mark.skipif(
75 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
76 )
77 def test_serial_default_settings():
78 """As a user, I want to simply connect to an instrument using default settings"""
79 instr = MultiprotocolInstrument(adapter='ASRL1::INSTR', visa_library='@sim')
80 assert instr.adapter.connection.baud_rate == 2400
81
82
•83 @pytest.mark.skipif(
84 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
85 )
86 def test_serial_custom_baud_rate_is_set():
87 """As a user, I want to easily override default settings to fit my needs"""
88 instr = MultiprotocolInstrument(adapter='ASRL1::INSTR', baud_rate=115200, visa_library='@sim')
89 assert instr.adapter.connection.baud_rate == 115200
90
91
•92 @pytest.mark.skipif(
93 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
94 )
95 def test_connections_use_tcpip():
96 """As a user, I want to be free to choose which connection to use (e.g. serial-over RS-232,
97 USB, TCP/IP) should an instrument support more than one
98 """
99 # here, baud_rate is invalid for pyvisa, but is a default kwarg anyway
100 instr = MultiprotocolInstrument(adapter='TCPIP::localhost:1111::INSTR', visa_library='@sim')
101 # method specific to pyvisa TCPIPInstrument
102 assert hasattr(instr.adapter.connection, 'control_ren')
103
104
•105 @pytest.mark.skipif(
106 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
107 )
108 def test_connections_use_gpib():
109 """As a user, I want to be free to choose which connection to use (e.g. serial-over RS-232,
110 USB, TCP/IP) should an instrument support more than one
111 """
112 # here, baud_rate is invalid for pyvisa, but is a default kwarg anyway
113 instr = MultiprotocolInstrument(adapter='GPIB::8::INSTR', visa_library='@sim')
114 # attribute specific to pyvisa GPIBInstrument
115 assert instr.adapter.connection.enable_repeat_addressing is False
116
117
118 def test_use_separate_SerialAdapter():
119 """As a user, I want to be able to supply a self-generated Adapter instance
120 (e.g. to enable serial connection sharing over RS-485/-422)
121 """
122 # note: baudrate, not baud_rate, as this goes to pyserial
123 ser = SerialAdapter(serial.serial_for_url("loop://", baudrate=4800))
124 instr = MultiprotocolInstrument(ser)
125 # don't need visa_library here as this does not go to pyvisa, kwargs are ignored if provided
126 assert instr.adapter.connection.baudrate == 4800
127
128
129 def test_separate_adapter_discards_additional_args():
130 """When passing in a separate Adapter instance, all connection-specific args and settings
131 are ignored.
132 """
133 ser = SerialAdapter(serial.serial_for_url("loop://"))
134 instr = MultiprotocolInstrument(ser, baud_rate=1234, wrong_kwarg='fizzbuzz')
135 assert instr.adapter.connection.baudrate == 9600 # default of serial
136
137
•138 @pytest.mark.skipif(
139 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
140 )
141 def test_use_separate_VISAAdapter():
142 """As a user, I want to be able to supply my own VISAAdpter with my settings
143 """
144 # User can use their own VISAAdapter
145 ser = VISAAdapter('ASRL1::INSTR', baud_rate=1200, visa_library='@sim')
146 instr = MultiprotocolInstrument(ser)
147 # don't need visa_library here as this does not go to pyvisa, kwargs are ignored if provided
148 assert instr.adapter.connection.baud_rate == 1200
149
150
•151 @pytest.mark.skipif(
152 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
153 )
154 def test_incorrect_arg_is_flagged():
155 """As a user or instrument contributor that used an incorrect/invalid (kw)arg,
156 I want to be alerted to that fact.
157 """
158 with pytest.raises(ValueError, match='bitrate'):
159 _ = MultiprotocolInstrument(adapter='ASRL1::INSTR', bitrate=1234, visa_library='@sim')
160
161
•162 @pytest.mark.skipif(
163 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
164 )
165 def test_incorrect_interface_type_is_flagged():
166 """As a user or instrument contributor that used an incorrect/invalid interface type,
167 I want to be alerted to that fact.
168 """
169 class WrongInterfaceInstrument(Instrument):
170 def __init__(self, adapter, name="Instrument with incorrect interface name", **kwargs):
171 super().__init__(adapter,
172 name=name,
173 arsl={'read_termination': '\r\n'}, # typo here
174 **kwargs,
175 )
176 with pytest.raises(ValueError, match='arsl'):
177 _ = WrongInterfaceInstrument(adapter='ASRL1::INSTR', visa_library='@sim')
178
179
•180 @pytest.mark.skipif(
181 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
182 )
183 def test_improper_arg_is_flagged():
184 """As a user or instrument contributor that used a kwarg that is inappropriate for the present
185 connection, I want to be alerted to that fact.
186 """
187 with pytest.raises(ValueError, match='enable_repeat_addressing'):
188 _ = MultiprotocolInstrument(adapter='ASRL1::INSTR', enable_repeat_addressing=True,
189 visa_library='@sim')
190
191
•192 @pytest.mark.skipif(
193 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
194 )
195 def test_common_kwargs_are_retained():
196 instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', visa_library='@sim')
197 assert instr1.adapter.connection.timeout == 1500
198 instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', visa_library='@sim')
199 assert instr2.adapter.connection.timeout == 1500
200
201
•202 @pytest.mark.skipif(
203 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
204 )
205 def test_distinct_interface_specific_defaults():
206 """As an instrument implementor, I can easily prescribe default settings
207 that are different between interfaces.
208 """
209 instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', visa_library='@sim')
210 assert instr1.adapter.connection.read_termination == '\r\n'
211 instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', visa_library='@sim')
212 assert instr2.adapter.connection.read_termination == '\r'
213
214
•215 @pytest.mark.skipif(
216 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
217 )
218 def test_modified_non_signature_kwargs_are_retained():
219 instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', timeout=3000, visa_library='@sim')
220 assert instr1.adapter.connection.timeout == 3000
221 instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', timeout=3000, visa_library='@sim')
222 assert instr2.adapter.connection.timeout == 3000
223
224
•225 @pytest.mark.skipif(
226 is_pyvisa_sim_not_installed, reason="PyVISA tests require the pyvisa-sim library"
227 )
228 def test_override_interface_specific_defaults():
229 """As as user, I want to be able to override interface-specific hardcoded defaults.
230 """
231 instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', read_termination='\r',
232 visa_library='@sim')
233 assert instr1.adapter.connection.read_termination == '\r'
234 instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', enable_repeat_addressing=True,
235 visa_library='@sim')
236 assert instr2.adapter.connection.enable_repeat_addressing is True