| 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 pytest |
| 26 |
|
| 27 |
from pymeasure.test import expected_protocol |
| 28 |
|
| 29 |
|
| 30 |
from pymeasure.instruments.hcp import TC038D |
| 31 |
|
| 32 |
|
| 33 |
# Testing the 'write multiple values' method of the device. |
| 34 |
def test_write_multiple_values(): |
| 35 |
# Communication from manual. |
| 36 |
with expected_protocol( |
| 37 |
TC038D, |
| 38 |
[(b"\x01\x10\x01\x0A\x00\x04\x08\x00\x00\x03\xE8\xFF\xFF\xFC\x18\x8D\xE9", |
| 39 |
b"\x01\x10\x01\x0A\x00\x04\xE0\x34")] |
| 40 |
) as inst: |
| 41 |
inst.write("W,0x010A,1000,-1000") |
| 42 |
inst.read() |
| 43 |
|
| 44 |
|
| 45 |
def test_write_multiple_values_decimal_address(): |
| 46 |
# Communication from manual. |
| 47 |
with expected_protocol( |
| 48 |
TC038D, |
| 49 |
[(b"\x01\x10\x01\x0A\x00\x04\x08\x00\x00\x03\xE8\xFF\xFF\xFC\x18\x8D\xE9", |
| 50 |
b"\x01\x10\x01\x0A\x00\x04\xE0\x34")] |
| 51 |
) as inst: |
| 52 |
inst.write("W,266,1000,-1000") |
| 53 |
inst.read() |
| 54 |
|
| 55 |
|
| 56 |
def test_write_values_CRC_error(): |
| 57 |
"""Test whether an invalid response CRC code raises an Exception.""" |
| 58 |
with expected_protocol( |
| 59 |
TC038D, |
| 60 |
[(b"\x01\x10\x01\x06\x00\x02\x04\x00\x00\x01A\xbf\xb5", |
| 61 |
b"\x01\x10\x01\x06\x00\x02\x01\x02")], |
| 62 |
) as inst: |
| 63 |
with pytest.raises(ConnectionError): |
| 64 |
inst.setpoint = 32.1 |
| 65 |
|
| 66 |
|
| 67 |
def test_write_multiple_handle_wrong_start_address(): |
| 68 |
"""Test whether the error code (byte 2) of 2 raises the right error.""" |
| 69 |
with expected_protocol( |
| 70 |
TC038D, |
| 71 |
[(b"\x01\x10\x01\x06\x00\x02\x04\x00\x00\x01A\xbf\xb5", |
| 72 |
b"\x01\x90\x02\xcd\xc1")], |
| 73 |
) as inst: |
| 74 |
with pytest.raises(ValueError, match="Wrong start address"): |
| 75 |
inst.setpoint = 32.1 |
| 76 |
|
| 77 |
|
| 78 |
# Test the 'read register' method of the device |
| 79 |
def test_read_CRC_error(): |
| 80 |
"""Test whether an invalid response CRC code raises an Exception.""" |
| 81 |
with expected_protocol( |
| 82 |
TC038D, |
| 83 |
[(b"\x01\x03\x00\x00\x00\x02\xC4\x0B", |
| 84 |
b"\x01\x03\x04\x00\x00\x03\xE8\x01\x02")], |
| 85 |
) as inst: |
| 86 |
with pytest.raises(ConnectionError): |
| 87 |
inst.temperature |
| 88 |
|
| 89 |
|
| 90 |
def test_read_address_error(): |
| 91 |
"""Test whether the error code (byte 2) of 2 raises the right error.""" |
| 92 |
with expected_protocol( |
| 93 |
TC038D, |
| 94 |
[(b"\x01\x03\x00\x00\x00\x02\xC4\x0B", |
| 95 |
b"\x01\x83\x02\xc0\xf1")], |
| 96 |
) as inst: |
| 97 |
with pytest.raises(ValueError, match="start address"): |
| 98 |
inst.temperature |
| 99 |
|
| 100 |
|
| 101 |
def test_read_elements_error(): |
| 102 |
"""Test whether the error code (byte 2) of 3 raises the right error.""" |
| 103 |
with expected_protocol( |
| 104 |
TC038D, |
| 105 |
[(b"\x01\x03\x00\x00\x00\x02\xC4\x0B", |
| 106 |
b"\x01\x83\x03\x011")], |
| 107 |
) as inst: |
| 108 |
with pytest.raises(ValueError, match="Variable data"): |
| 109 |
inst.temperature |
| 110 |
|
| 111 |
|
| 112 |
def test_read_any_error(): |
| 113 |
"""Test whether any wrong message (byte 1 is not 3) raises an error.""" |
| 114 |
with expected_protocol( |
| 115 |
TC038D, |
| 116 |
[(b"\x01\x03\x00\x00\x00\x02\xC4\x0B", |
| 117 |
b"\x01\x43\x05\xd13")], |
| 118 |
) as inst: |
| 119 |
with pytest.raises(ConnectionError): |
| 120 |
inst.temperature |
| 121 |
|
| 122 |
|
| 123 |
# Test properties |
| 124 |
def test_setpoint(): |
| 125 |
with expected_protocol( |
| 126 |
TC038D, |
| 127 |
[(b"\x01\x03\x01\x06\x00\x02\x25\xf6", |
| 128 |
b"\x01\x03\x04\x00\x00\x00\x99:Y")], |
| 129 |
) as inst: |
| 130 |
assert inst.setpoint == 15.3 |
| 131 |
|
| 132 |
|
| 133 |
def test_setpoint_setter(): |
| 134 |
with expected_protocol( |
| 135 |
TC038D, |
| 136 |
[(b"\x01\x10\x01\x06\x00\x02\x04\x00\x00\x01A\xbf\xb5", |
| 137 |
b"\x01\x10\x01\x06\x00\x02\xa0\x35")], |
| 138 |
) as inst: |
| 139 |
inst.setpoint = 32.1 |
| 140 |
|
| 141 |
|
| 142 |
def test_temperature(): |
| 143 |
# Communication from manual. |
| 144 |
# Tests readRegister as well. |
| 145 |
with expected_protocol( |
| 146 |
TC038D, |
| 147 |
[(b"\x01\x03\x00\x00\x00\x02\xC4\x0B", |
| 148 |
b"\x01\x03\x04\x00\x00\x03\xE8\xFA\x8D")], |
| 149 |
) as inst: |
| 150 |
assert inst.temperature == 100 |
| 151 |
|
| 152 |
|
| 153 |
def test_ping(): |
| 154 |
# Communication from manual. |
| 155 |
with expected_protocol( |
| 156 |
TC038D, |
| 157 |
[(b"\x01\x08\x00\x00\x12\x34\xed\x7c", b"\x01\x08\x00\x00\x12\x34\xed\x7c")], |
| 158 |
) as inst: |
| 159 |
inst.ping(4660) |