test_dev_pfeiffer_tpg.py 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
"""
Tests for the .dev.pfeiffer_tpg sub-package.
"""

import logging

import pytest

alisec's avatar
alisec committed
9
from hvl_ccb import comm, dev
10
from tests.masked_comm.serial import PfeifferTPGLoopSerialCommunication
11
12
13
14
15
16
17
18
19

logging.basicConfig(level=logging.ERROR)


@pytest.fixture(scope="module")
def com_config():
    return {
        "port": "loop://?logging=debug",
        "baudrate": 9600,
20
21
22
        "parity": dev.PfeifferTPGSerialCommunicationConfig.Parity.NONE,
        "stopbits": dev.PfeifferTPGSerialCommunicationConfig.Stopbits.ONE,
        "bytesize": dev.PfeifferTPGSerialCommunicationConfig.Bytesize.EIGHTBITS,
23
24
25
26
27
28
29
        "terminator": b'\r\n',
        "timeout": 3,
    }


@pytest.fixture(scope='module')
def dev_config():
alisec's avatar
alisec committed
30
    return {'model': dev.PfeifferTPGConfig.Model.TPG25xA}
31
32
33
34


@pytest.fixture
def started_pfeiffer_tpg(com_config, dev_config):
35
    com = PfeifferTPGLoopSerialCommunication(com_config)
alisec's avatar
alisec committed
36
37
38
39
40
41
    com.open()
    com.put_text(chr(6))
    com.put_text(','.join([dev.PfeifferTPG.SensorTypes.CMR.name,
                           dev.PfeifferTPG.SensorTypes.noSENSOR.name]))
    with dev.PfeifferTPG(com, dev_config) as pg:
        while com.get_written() is not None:
42
            pass
alisec's avatar
alisec committed
43
        yield com, pg
44
45
46
47
48


def test_pfeiffer_tpg_instantiation(com_config, dev_config):
    pg = dev.PfeifferTPG(com_config, dev_config)
    assert pg is not None
49
50
51
52
53
54
55
56
57
58
59
    assert pg.unit == "mbar"

    # another valid config
    config_dict = dict(dev_config)
    config_dict['model'] = 'TPGx6x'
    dev.PfeifferTPG(com_config, config_dict)

    # wrong config
    config_dict['model'] = 'wrong_name'
    with pytest.raises(ValueError):
        dev.PfeifferTPG(com_config, config_dict)
Chachereau Alise's avatar
Chachereau Alise committed
60
61


62
63
64
def test_com_send_command(com_config):
    com = PfeifferTPGLoopSerialCommunication(com_config)
    com.open()
65
    com.put_text(chr(6))
66
67
    com.send_command('this is the command')
    assert com.get_written() == 'this is the command'
68
    com.put_text('not an acknowledgement')
69
70
71
    with pytest.raises(dev.PfeifferTPGError):
        com.send_command('this command is not acknowledged')
    com.close()
72
73


74
75
76
77
def test_com_query(com_config):
    com = PfeifferTPGLoopSerialCommunication(com_config)
    com.open()
    com.put_text(chr(6))
78
    com.put_text('this is the answer')
79
80
    assert com.query('this is the query') == 'this is the answer'
    assert com.get_written() == 'this is the query'
81
    assert com.get_written() == chr(5)
82
    com.put_text('not an acknowledgement')
83
84
85
86
87
88
89
    with pytest.raises(dev.PfeifferTPGError):
        com.query('this query is not acknowledged')
    com.put_text(chr(6))
    com.put_text('')
    with pytest.raises(dev.PfeifferTPGError):
        com.query('the answer to this query is empty')
    com.close()
90
91


Chachereau Alise's avatar
Chachereau Alise committed
92
93
94
def test_pfeiffer_tpg_start(started_pfeiffer_tpg):
    com, pg = started_pfeiffer_tpg
    # starting again should work
alisec's avatar
alisec committed
95
    com.put_text(chr(6))
alisec's avatar
alisec committed
96
    com.put_text(','.join(['Unknown', dev.PfeifferTPG.SensorTypes.CMR.name]))
Chachereau Alise's avatar
Chachereau Alise committed
97
    pg.start()
98
    assert pg.number_of_sensors == 2
Chachereau Alise's avatar
Chachereau Alise committed
99
100
    assert pg.sensors[0] == 'Unknown'
    assert pg.sensors[1] == 'APR/CMR Linear Gauge'
101
102


alisec's avatar
alisec committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def test_pfeiffer_com_error(com_config, dev_config):
    wrong_config = dict(com_config)
    wrong_config['port'] = 'NOT A PORT'
    tpg = dev.PfeifferTPG(wrong_config, dev_config)
    assert not tpg.com.is_open

    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.start()

    tpg = dev.PfeifferTPG(com_config, dev_config)
    assert not tpg.com.is_open

    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.identify_sensors()
    tpg.sensors = [0]
    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.measure(1)
    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.measure_all()
    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.set_full_scale_unitless([1])
    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.get_full_scale_unitless()
    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.set_full_scale_mbar([100])
    with pytest.raises(comm.SerialCommunicationIOError):
        tpg.get_full_scale_mbar()


132
133
def test_identify_sensors(started_pfeiffer_tpg):
    com, pg = started_pfeiffer_tpg
134
    # example with 3 sensors
135
    com.put_text(chr(6))
alisec's avatar
alisec committed
136
137
138
    com.put_text(','.join([dev.PfeifferTPG.SensorTypes.PKR.name,
                           dev.PfeifferTPG.SensorTypes.CMR.name,
                           dev.PfeifferTPG.SensorTypes.IKR.name]))
139
    pg.identify_sensors()
alisec's avatar
alisec committed
140
141
    assert com.get_written() == 'TID'
    assert com.get_written() == chr(5)
142
    assert pg.number_of_sensors == 3
alisec's avatar
alisec committed
143
144
145
    assert pg.sensors[0] == dev.PfeifferTPG.SensorTypes.PKR.name
    assert pg.sensors[1] == dev.PfeifferTPG.SensorTypes.CMR.name
    assert pg.sensors[2] == dev.PfeifferTPG.SensorTypes.IKR.name
146
147
148
149
    # wrong answer from device
    com.put_text('this will make the command fail')
    with pytest.raises(dev.PfeifferTPGError):
        pg.identify_sensors()
Chachereau Alise's avatar
Chachereau Alise committed
150
151


152
153
154
155
156
157
158
159
def test_set_display_unit(started_pfeiffer_tpg):
    com, pg = started_pfeiffer_tpg
    # valid str argument
    com.put_text(chr(6))
    pg.set_display_unit("mbar")
    # valid PressureUnits argument
    com.put_text(chr(6))
    pg.set_display_unit(pg.PressureUnits.Pascal)
160
161
162
163
    # valid PressureUnits argument but no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.set_display_unit(pg.PressureUnits.Pascal)
164
    # invalid str argument
165
    with pytest.raises(ValueError):
166
167
168
        pg.set_display_unit("Not an accepted unit")


169
def test_measure(started_pfeiffer_tpg):
Chachereau Alise's avatar
Chachereau Alise committed
170
    com, pg = started_pfeiffer_tpg
171
    # normal case
Chachereau Alise's avatar
Chachereau Alise committed
172
    com.put_text(chr(6))
alisec's avatar
alisec committed
173
174
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Ok.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.Ok.name, 1.234e-2)
175
176
177
    assert com.get_written() == 'PR2'
    # underrange
    com.put_text(chr(6))
alisec's avatar
alisec committed
178
179
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Underrange.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.Underrange.name, 1.234e-2)
180
181
    # overrange
    com.put_text(chr(6))
alisec's avatar
alisec committed
182
183
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Overrange.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.Overrange.name, 1.234e-2)
184
185
    # error
    com.put_text(chr(6))
alisec's avatar
alisec committed
186
187
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Sensor_error.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.Sensor_error.name, 1.234e-2)
188
189
    # off
    com.put_text(chr(6))
alisec's avatar
alisec committed
190
191
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Sensor_off.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.Sensor_off.name, 1.234e-2)
192
193
    # none
    com.put_text(chr(6))
alisec's avatar
alisec committed
194
195
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.No_sensor.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.No_sensor.name, 1.234e-2)
196
197
    # identification error
    com.put_text(chr(6))
alisec's avatar
alisec committed
198
199
200
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Identification_error.value},1.234E-02")
    assert pg.measure(2) == (dev.PfeifferTPG.SensorStatus.Identification_error.name,
                             1.234e-2)
201
202
203
204
205
206
207
    # wrong channel
    with pytest.raises(ValueError):
        pg.measure(12)
    # no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.measure(1)
alisec's avatar
alisec committed
208
209


210
def test_measure_all(started_pfeiffer_tpg):
alisec's avatar
alisec committed
211
    com, pg = started_pfeiffer_tpg
212
213
    # normal case
    com.put_text(chr(6))
alisec's avatar
alisec committed
214
215
216
217
218
    com.put_text(f"{dev.PfeifferTPG.SensorStatus.Identification_error.value},1.234E-02,"
                 f"{dev.PfeifferTPG.SensorStatus.Ok.value},1.234E-02")
    assert pg.measure_all() == [(dev.PfeifferTPG.SensorStatus.Identification_error.name,
                                 1.234e-2),
                                (dev.PfeifferTPG.SensorStatus.Ok.name, 1.234e-2)]
219
    assert com.get_written() == 'PRX'
220
221
222
223
    # no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.measure_all()
Chachereau Alise's avatar
Chachereau Alise committed
224
225


Chachereau Alise's avatar
Chachereau Alise committed
226
def test_set_full_scale_unitless(started_pfeiffer_tpg):
Chachereau Alise's avatar
Chachereau Alise committed
227
228
    com, pg = started_pfeiffer_tpg
    com.put_text(chr(6))
229
    pg.set_full_scale_unitless([1, 2])
Chachereau Alise's avatar
Chachereau Alise committed
230
    assert com.get_written() == 'FSR,1,2'
231
232
233
234
235
236
237
238
239
240
241
242
    # no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.set_full_scale_unitless([1, 2])
    # wrong values
    com.put_text(chr(6))
    with pytest.raises(ValueError):
        pg.set_full_scale_unitless([12, 24])
    # wrong number of values
    com.put_text(chr(6))
    with pytest.raises(ValueError):
        pg.set_full_scale_unitless([12, 24, 32])
Chachereau Alise's avatar
Chachereau Alise committed
243
244


Chachereau Alise's avatar
Chachereau Alise committed
245
def test_get_full_scale_unitless(started_pfeiffer_tpg):
Chachereau Alise's avatar
Chachereau Alise committed
246
247
248
    com, pg = started_pfeiffer_tpg
    com.put_text(chr(6))
    com.put_text('2,0')
Chachereau Alise's avatar
Chachereau Alise committed
249
250
    assert pg.get_full_scale_unitless() == [2, 0]
    assert com.get_written() == 'FSR'
251
252
253
254
255
256
257
258
259
    # no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.get_full_scale_unitless()
    # wrong answer from device
    com.put_text(chr(6))
    com.put_text('12,24')
    with pytest.raises(dev.PfeifferTPGError):
        pg.get_full_scale_unitless()
Chachereau Alise's avatar
Chachereau Alise committed
260
261
262
263
264


def test_set_full_scale_mbar(started_pfeiffer_tpg):
    com, pg = started_pfeiffer_tpg
    com.put_text(chr(6))
265
    pg.set_full_scale_mbar([100, 1])
alisec's avatar
alisec committed
266
267
    fsr = pg.config.model.full_scale_ranges
    assert com.get_written() == f'FSR,{fsr[100]},{fsr[1]}'
268
269
270
271
272
273
274
275
276
277
278
279
    # no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.set_full_scale_mbar([100, 1])
    # wrong values
    com.put_text(chr(6))
    with pytest.raises(ValueError):
        pg.set_full_scale_mbar([12, 24])
    # wrong number of values
    com.put_text(chr(6))
    with pytest.raises(ValueError):
        pg.set_full_scale_mbar([12, 24, 32])
Chachereau Alise's avatar
Chachereau Alise committed
280
281
282
283
284
285


def test_get_full_scale_mbar(started_pfeiffer_tpg):
    com, pg = started_pfeiffer_tpg
    com.put_text(chr(6))
    com.put_text('2,0')
alisec's avatar
alisec committed
286
287
    fsr_rev = pg.config.model.full_scale_ranges_reversed
    assert pg.get_full_scale_mbar() == [fsr_rev[2], fsr_rev[0]]
Chachereau Alise's avatar
Chachereau Alise committed
288
    assert com.get_written() == 'FSR'
289
290
291
292
293
294
295
296
297
    # no acknowledgment from device
    com.put_text('not an acknowledgment')
    with pytest.raises(dev.PfeifferTPGError):
        pg.get_full_scale_mbar()
    # wrong answer from device
    com.put_text(chr(6))
    com.put_text('12,24')
    with pytest.raises(dev.PfeifferTPGError):
        pg.get_full_scale_mbar()