| 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 |
import serial |
| 27 |
|
| 28 |
from pymeasure.adapters import SerialAdapter |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def adapter(): |
| 33 |
return SerialAdapter(serial.serial_for_url("loop://", timeout=0.2)) |
| 34 |
|
| 35 |
|
| 36 |
@pytest.mark.parametrize("term", (("", "\n", "123\r"))) |
| 37 |
def test_write_termination(adapter, term): |
| 38 |
adapter.write_termination = term |
| 39 |
adapter.write("abc") |
| 40 |
assert adapter.read() == "abc" + term |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize("term", (("", "\n", "\r\n", "4.1123"))) |
| 44 |
def test_read_termination(adapter, term): |
| 45 |
adapter.read_termination = term |
| 46 |
adapter.write("abc" + term) |
| 47 |
assert adapter.read() == "abc" |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.parametrize("msg", ["OUTP\n", "POWER 22 dBm\n"]) |
| 51 |
def test_adapter_write_read(adapter, msg): |
| 52 |
adapter.write(msg) |
| 53 |
assert adapter.read() == msg |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.parametrize("msg", [b"OUTP\n", b"POWER 22 dBm\n"]) |
| 57 |
def test_write_bytes(adapter, msg): |
| 58 |
adapter.write_bytes(msg) |
| 59 |
assert adapter.read() == msg.decode() |
| 60 |
|
| 61 |
|
| 62 |
def test_read_bytes(adapter): |
| 63 |
"""Test whether `count` bytes are returned, even though a term char is defined.""" |
| 64 |
adapter.read_termination = "\n" |
| 65 |
adapter.write_bytes(b"basd\x02\nfasdf\n") |
| 66 |
assert adapter.read_bytes(9) == b"basd\x02\nfas" |
| 67 |
|
| 68 |
|
| 69 |
def test_read_bytes_unlimited(adapter): |
| 70 |
"""Test whether all bytes are returned, even though a term char is defined.""" |
| 71 |
adapter.read_termination = "\n" |
| 72 |
adapter.write_bytes(b"basd\x02\nfasdf\n") |
| 73 |
assert adapter.read_bytes(-1) == b"basd\x02\nfasdf\n" |
| 74 |
|
| 75 |
|
| 76 |
def test_read_bytes_unlimited_long(adapter): |
| 77 |
"""Test whether all bytes are returned when a lot of data is sent.""" |
| 78 |
adapter.write_bytes(b"abcde" * 50) |
| 79 |
assert adapter.read_bytes(-1) == b"abcde" * 50 |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.parametrize("count", (-1, 8)) |
| 83 |
def test_read_bytes_break_on_termchar(adapter, count): |
| 84 |
adapter.read_termination = "\n" |
| 85 |
adapter.write_bytes(b"basd\x02\nfasdf\n") |
| 86 |
assert adapter.read_bytes(count, break_on_termchar=True) == b"basd\x02\n" |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize("test_input,expected", [([1, 2, 3], b'OUTP#13\x01\x02\x03'), |
| 90 |
(range(100), b'OUTP#3100' + bytes(range(100)))]) |
| 91 |
def test_adapter_write_binary_values(adapter, test_input, expected): |
| 92 |
adapter.write_binary_values("OUTP", test_input, datatype='B') |
| 93 |
# Add 10 bytes more, just to check that no extra bytes are present |
| 94 |
assert adapter.connection.read(len(expected) + 10) == expected |