Debian Codegraph @

tests/instruments/keysight/test_keysightDSOX1102G_with_device.py

raw
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 time import sleep
26 import logging
27
28 import pytest
29 import numpy as np
30 from pymeasure.instruments.keysight.keysightDSOX1102G import KeysightDSOX1102G
31 from pyvisa.errors import VisaIOError
32
33 pytest.skip('Only work with connected hardware', allow_module_level=True)
34
35
36 class TestKeysightDSOX1102G:
37 """
38 Unit tests for KeysightDSOX1102G class.
39
40 This test suite, needs the following setup to work properly:
41 - A KeysightDSOX1102G device should be connected to the computer;
42 - The device's address must be set in the RESOURCE constant;
43 - A probe on Channel 1 must be connected to the Demo output of the oscilloscope.
44 """
45
46 ##################################################
47 # KeysightDSOX1102G device address goes here:
48 RESOURCE = "USB0::10893::6039::CN57266430::INSTR"
49 ##################################################
50
51 #########################
52 # PARAMETRIZATION CASES #
53 #########################
54
55 BOOLEANS = [False, True]
56 CHANNEL_COUPLINGS = ["ac", "dc"]
57 CHANNEL_LABELS = [["label", "LABEL"], ["quite long label", "QUITE LONG"],
58 [12345, "12345"]]
59 TIMEBASE_MODES = ["main", "window", "xy", "roll"]
60 ACQUISITION_TYPES = ["normal", "average", "hresolution", "peak"]
61 ACQUISITION_MODES = ["realtime", "segmented"]
62 DIGITIZE_SOURCES = ["channel1", "channel2", "function", "math", "fft",
63 "abus", "ext"]
64 WAVEFORM_POINTS_MODES = ["normal", "maximum", "raw"]
65 WAVEFORM_POINTS = [100, 250, 500, 1000, 2000, 5000, 10000, 20000, 50000,
66 62500]
67 WAVEFORM_SOURCES = ["channel1", "channel2", "function", "fft", "wmemory1",
68 "wmemory2", "ext"]
69 WAVEFORM_FORMATS = ["ascii", "word", "byte"]
70 DOWNLOAD_SOURCES = ["channel1", "channel2", "function", "fft", "ext"]
71 CHANNELS = [1, 2]
72
73 SCOPE = KeysightDSOX1102G(RESOURCE)
74
75 ############
76 # FIXTURES #
77 ############
78
79 @pytest.fixture
80 def make_reseted_cleared_scope(self):
81 self.SCOPE.reset()
82 self.SCOPE.clear_status()
83 return self.SCOPE
84
85 #########
86 # TESTS #
87 #########
88
89 def test_scope_connection(self, make_reseted_cleared_scope):
90 bad_resource = "USB0::10893::45848::MY12345678::0::INSTR"
91 # The pure python VISA library (pyvisa-py) raises a ValueError while the
92 # PyVISA library raises a VisaIOError.
93 with pytest.raises((ValueError, VisaIOError)):
94 KeysightDSOX1102G(bad_resource)
95
96 def test_autoscale(self, make_reseted_cleared_scope):
97 scope = make_reseted_cleared_scope
98 scope.write(":timebase:position 1")
99 # Autoscale should turn off the zoomed (delayed) time mode
100 assert scope.ask(":timebase:position?") == "+1.000000000000E+00\n"
101 scope.autoscale()
102 assert scope.ask(":timebase:position?") != "+1.000000000000E+00\n"
103
104 # Channel
105 def test_ch_current_configuration(self, make_reseted_cleared_scope):
106 scope = make_reseted_cleared_scope
107 expected = {"OFFS": 0.0, "COUP": "DC", "IMP": "ONEM", "DISP": True,
108 "BWL": False, "INV": False, "UNIT": "VOLT", "PROB": 10.0,
109 "PROB:SKEW": 0.0, "STYP": "SING", "CHAN": 1, "RANG": 40.0}
110 actual = scope.ch(1).current_configuration
111 assert actual == expected
112
113 @pytest.mark.parametrize("ch_number", CHANNELS)
114 @pytest.mark.parametrize("case", BOOLEANS)
115 def test_ch_bwlimit(self, make_reseted_cleared_scope, ch_number, case):
116 scope = make_reseted_cleared_scope
117 scope.ch(ch_number).bwlimit = case
118 assert scope.ch(ch_number).bwlimit == case
119
120 @pytest.mark.parametrize("ch_number", CHANNELS)
121 @pytest.mark.parametrize("case", CHANNEL_COUPLINGS)
122 def test_ch_coupling(self, make_reseted_cleared_scope, ch_number, case):
123 scope = make_reseted_cleared_scope
124 scope.ch(ch_number).coupling = case
125 assert scope.ch(ch_number).coupling == case
126
127 @pytest.mark.parametrize("ch_number", CHANNELS)
128 @pytest.mark.parametrize("case", BOOLEANS)
129 def test_ch_display(self, make_reseted_cleared_scope, ch_number, case):
130 scope = make_reseted_cleared_scope
131 scope.ch(ch_number).display = case
132 assert scope.ch(ch_number).display == case
133
134 @pytest.mark.parametrize("ch_number", CHANNELS)
135 @pytest.mark.parametrize("case", BOOLEANS)
136 def test_ch_invert(self, make_reseted_cleared_scope, ch_number, case):
137 scope = make_reseted_cleared_scope
138 scope.ch(ch_number).invert = case
139 assert scope.ch(ch_number).invert == case
140
141 @pytest.mark.parametrize("ch_number", CHANNELS)
142 @pytest.mark.parametrize("case, expected", CHANNEL_LABELS)
143 def test_ch_label(self, make_reseted_cleared_scope, ch_number, case, expected):
144 scope = make_reseted_cleared_scope
145 scope.ch(ch_number).label = case
146 assert scope.ch(ch_number).label == expected
147
148 @pytest.mark.parametrize("ch_number", CHANNELS)
149 def test_ch_offset(self, make_reseted_cleared_scope, ch_number):
150 scope = make_reseted_cleared_scope
151 scope.ch(ch_number).offset = 1
152 assert scope.ch(ch_number).offset == 1
153
154 @pytest.mark.parametrize("ch_number", CHANNELS)
155 def test_ch_probe_attenuation(self, make_reseted_cleared_scope, ch_number):
156 scope = make_reseted_cleared_scope
157 scope.ch(ch_number).probe_attenuation = 10
158 assert scope.ch(ch_number).probe_attenuation == 10
159
160 @pytest.mark.parametrize("ch_number", CHANNELS)
161 def test_ch_range(self, make_reseted_cleared_scope, ch_number):
162 scope = make_reseted_cleared_scope
163 scope.ch(ch_number).range = 10
164 assert scope.ch(ch_number).range == 10
165
166 @pytest.mark.parametrize("ch_number", CHANNELS)
167 def test_ch_scale(self, make_reseted_cleared_scope, ch_number):
168 scope = make_reseted_cleared_scope
169 scope.ch(ch_number).scale = 0.1
170 assert scope.ch(ch_number).scale == 0.1
171
172 # Timebase
173 def test_timebase(self, make_reseted_cleared_scope):
174 scope = make_reseted_cleared_scope
175 expected = {"REF": "CENT", "MAIN:RANG": +1.000E-03, "POS": 0.0,
176 "MODE": "MAIN"}
177 actual = scope.timebase
178 assert actual == expected
179
180 @pytest.mark.parametrize("case", TIMEBASE_MODES)
181 def test_timebase_mode(self, make_reseted_cleared_scope, case):
182 scope = make_reseted_cleared_scope
183 scope.timebase_mode = case
184 assert scope.timebase_mode == case
185
186 def test_timebase_offset(self, make_reseted_cleared_scope):
187 scope = make_reseted_cleared_scope
188 scope.timebase_offset = 1
189 assert scope.timebase_offset == 1
190
191 def test_timebase_range(self, make_reseted_cleared_scope):
192 scope = make_reseted_cleared_scope
193 scope.timebase_range = 10
194 assert scope.timebase_range == 10
195
196 def test_timebase_scale(self, make_reseted_cleared_scope):
197 scope = make_reseted_cleared_scope
198 scope.timebase_scale = 0.1
199 assert scope.timebase_scale == 0.1
200
201 # Acquisition
202 @pytest.mark.parametrize("case", ACQUISITION_TYPES)
203 def test_acquisition_type(self, make_reseted_cleared_scope, case):
204 scope = make_reseted_cleared_scope
205 scope.acquisition_type = case
206 assert scope.acquisition_type == case
207
208 @pytest.mark.parametrize("case", ACQUISITION_MODES)
209 def test_acquisition_mode(self, make_reseted_cleared_scope, case):
210 scope = make_reseted_cleared_scope
211 scope.acquisition_mode = case
212 assert scope.acquisition_mode == case
213
214 @pytest.mark.parametrize("case", WAVEFORM_POINTS_MODES)
215 def test_waveform_points_mode(self, make_reseted_cleared_scope, case):
216 scope = make_reseted_cleared_scope
217 scope.waveform_points_mode = case
218 assert scope.waveform_points_mode == case
219
220 @pytest.mark.parametrize("case", WAVEFORM_POINTS)
221 def test_waveform_points(self, make_reseted_cleared_scope, case):
222 scope = make_reseted_cleared_scope
223 scope.waveform_points_mode = "raw"
224 scope.waveform_points = case
225 assert scope.waveform_points == case
226
227 @pytest.mark.parametrize("case", WAVEFORM_SOURCES)
228 def test_waveform_source(self, make_reseted_cleared_scope, case):
229 scope = make_reseted_cleared_scope
230 scope.waveform_source = case
231 assert scope.waveform_source == case
232
233 @pytest.mark.parametrize("case", WAVEFORM_FORMATS)
234 def test_waveform_format(self, make_reseted_cleared_scope, case):
235 scope = make_reseted_cleared_scope
236 scope.waveform_format = case
237 assert scope.waveform_format == case
238
239 def test_waveform_preamble(self, make_reseted_cleared_scope):
240 scope = make_reseted_cleared_scope
241 scope.waveform_format = "ascii"
242 scope.waveform_source = "channel1"
243 expected_preamble = {"count": 1, "format": "ASCII", "points": 62500,
244 "type": "NORMAL", "xincrement": 1.6e-08, "xorigin": -0.0005,
245 "xreference": 0, "yincrement": 0.0007851759,
246 "yorigin": 0, "yreference": 32768.0}
247 preamble = scope.waveform_preamble
248 assert preamble == expected_preamble
249
250 @pytest.mark.parametrize("case", DIGITIZE_SOURCES)
251 def test_digitize(self, make_reseted_cleared_scope, case):
252 scope = make_reseted_cleared_scope
253 # Here, we only assert that no error arrises when using the expected parameters.
254 # Success of digitize operation is evaluated through test_waveform_data
255 scope.digitize(case)
256 sleep(2) # Account for Digitize operation duration
257
258 def test_waveform_data(self, make_reseted_cleared_scope):
259 scope = make_reseted_cleared_scope
260 scope.digitize("channel1")
261 sleep(2) # Account for Digitize operation duration
262 scope.waveform_format = "ascii"
263 value = scope.waveform_data
264 assert type(value) is list
265 assert len(value) > 0
266 assert all(isinstance(n, float) for n in value)
267
268 def test_system_setup(self, make_reseted_cleared_scope):
269 scope = make_reseted_cleared_scope
270 initial_setup = scope.system_setup
271 scope.ch(1).display = not scope.ch(1).display
272 scope.ch(2).display = not scope.ch(2).display
273 # Assert that setup block is different
274 assert scope.system_setup != initial_setup
275 # Assert that the setup was successful
276 scope.system_setup = initial_setup
277 assert scope.system_setup == initial_setup
278
279 # Setup methods
280 @pytest.mark.parametrize("ch_number", CHANNELS)
281 def test_channel_setup(self, make_reseted_cleared_scope, ch_number, caplog):
282 # Using caplog to check content of log.
283 caplog.set_level(logging.WARNING)
284 scope = make_reseted_cleared_scope
285
286 # Not testing the actual values assignment since different combinations of
287 # parameters can play off each other.
288 expected = scope.ch(ch_number).current_configuration
289 scope.ch(ch_number).setup()
290 assert scope.ch(ch_number).current_configuration == expected
291 with pytest.raises(ValueError):
292 scope.ch(3)
293 scope.ch(ch_number).setup(1, vertical_range=1, scale=1)
294 assert 'Both "vertical_range" and "scale" are specified. Specified "scale" has priority.' in caplog.text # noqa
295
296 def test_timebase_setup(self, make_reseted_cleared_scope):
297 scope = make_reseted_cleared_scope
298 expected = scope.timebase
299 scope.timebase_setup()
300 assert scope.timebase == expected
301
302 # Download methods
303 def test_download_image_default_arguments(self, make_reseted_cleared_scope):
304 scope = make_reseted_cleared_scope
305 img = scope.download_image()
306 assert type(img) is bytearray
307
308 @pytest.mark.parametrize("format", ["bmp", "bmp8bit", "png"])
309 @pytest.mark.parametrize("color_palette", ["color", "grayscale"])
310 def test_download_image(self, make_reseted_cleared_scope, format, color_palette):
311 scope = make_reseted_cleared_scope
312 img = scope.download_image(format_=format, color_palette=color_palette)
313 assert type(img) is bytearray
314
315 def test_download_data_missingArgument(self, make_reseted_cleared_scope):
316 scope = make_reseted_cleared_scope
317 with pytest.raises(TypeError):
318 scope.download_data()
319
320 @pytest.mark.parametrize("case1", DOWNLOAD_SOURCES)
321 @pytest.mark.parametrize("case2", WAVEFORM_POINTS)
322 def test_download_data(self, make_reseted_cleared_scope, case1, case2):
323 scope = make_reseted_cleared_scope
324 data, preamble = scope.download_data(source=case1, points=case2)
325 assert type(data) is np.ndarray
326 # Returned length is not always as specified. Problem seems to be from scope itself.
327 # assert len(data) == case2
328 assert type(preamble) is dict