| 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 |
|
| 26 |
""" |
| 27 |
Test the instrument class for the Velleman F8090. |
| 28 |
|
| 29 |
This is not an integration test! This is software-only. |
| 30 |
""" |
| 31 |
import pytest |
| 32 |
|
| 33 |
from pymeasure.test import expected_protocol |
| 34 |
from pymeasure.instruments.velleman import VellemanK8090, VellemanK8090Switches as Switches |
| 35 |
|
| 36 |
|
| 37 |
def test_version(): |
| 38 |
with expected_protocol( |
| 39 |
VellemanK8090, |
| 40 |
[ |
| 41 |
( |
| 42 |
bytearray.fromhex("04 71 00 00 00 8B 0F"), |
| 43 |
bytearray.fromhex("04 71 FF 0A 01 81 0F"), |
| 44 |
) |
| 45 |
], |
| 46 |
) as inst: |
| 47 |
assert inst.version == (10, 1) |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.parametrize("reply,msg", [ |
| 51 |
(b"0", "Incoming packet was 1 bytes instead of 7"), |
| 52 |
(bytearray.fromhex("04 71 FF 0A 01 82 0F"), "Packet checksum was not correct"), |
| 53 |
(bytearray.fromhex("04 71 FF 0A 01 81 00"), "Received invalid start and stop bytes"), |
| 54 |
]) |
| 55 |
def test_version_bad_reply_short(reply, msg): |
| 56 |
with expected_protocol( |
| 57 |
VellemanK8090, |
| 58 |
[ |
| 59 |
( |
| 60 |
bytearray.fromhex("04 71 00 00 00 8B 0F"), |
| 61 |
reply, |
| 62 |
) |
| 63 |
], |
| 64 |
) as inst: |
| 65 |
with pytest.raises(ConnectionError, match=msg): |
| 66 |
_ = inst.version |
| 67 |
|
| 68 |
|
| 69 |
def test_status(): |
| 70 |
with expected_protocol( |
| 71 |
VellemanK8090, |
| 72 |
[ |
| 73 |
( |
| 74 |
bytearray.fromhex("04 18 00 00 00 E4 0F"), |
| 75 |
bytearray.fromhex("04 51 01 15 80 15 0F"), |
| 76 |
) |
| 77 |
], |
| 78 |
) as inst: |
| 79 |
last_on, curr_on, time_on = inst.status |
| 80 |
|
| 81 |
assert last_on == Switches.CH1 |
| 82 |
assert curr_on == Switches.CH1 | Switches.CH3 | Switches.CH5 |
| 83 |
assert time_on == Switches.CH8 |
| 84 |
|
| 85 |
|
| 86 |
def test_switch_on(): |
| 87 |
with expected_protocol( |
| 88 |
VellemanK8090, |
| 89 |
[ |
| 90 |
( |
| 91 |
bytearray.fromhex("04 11 05 00 00 E6 0F"), |
| 92 |
bytearray.fromhex("04 51 01 05 80 25 0F"), |
| 93 |
) |
| 94 |
] |
| 95 |
* 2, |
| 96 |
) as inst: |
| 97 |
# Test both signatures |
| 98 |
inst.switch_on = Switches.CH1 | Switches.CH3 |
| 99 |
inst.switch_on = [1, 3] |
| 100 |
|
| 101 |
|
| 102 |
def test_switch_off(): |
| 103 |
with expected_protocol( |
| 104 |
VellemanK8090, |
| 105 |
[ |
| 106 |
( |
| 107 |
bytearray.fromhex("04 12 FF 00 00 EB 0F"), |
| 108 |
bytearray.fromhex("04 51 01 00 80 2A 0F"), |
| 109 |
) |
| 110 |
], |
| 111 |
) as inst: |
| 112 |
inst.switch_off = Switches.ALL |