1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# -*- coding: utf-8 -*-
import logging
import pytest
from pyvisa.errors import VisaIOError
def assert_instrument_response(device, query, data):
response = device.query(query)
assert response == data, "%s, %r == %r" % (device.resource_name, query, data)
def test_list(resource_manager):
assert set(resource_manager.list_resources("?*")) == {
"ASRL1::INSTR",
"ASRL2::INSTR",
"ASRL3::INSTR",
"ASRL4::INSTR",
"TCPIP0::localhost::inst0::INSTR",
"TCPIP0::localhost::10001::SOCKET",
"TCPIP0::localhost:2222::inst0::INSTR",
"TCPIP0::localhost:3333::inst0::INSTR",
"TCPIP0::localhost:4444::inst0::INSTR",
"USB0::0x1111::0x2222::0x1234::0::INSTR",
"USB0::0x1111::0x2222::0x2468::0::INSTR",
"USB0::0x1111::0x2222::0x3692::0::INSTR",
"USB0::0x1111::0x2222::0x4444::0::INSTR",
"USB0::0x1111::0x2222::0x4445::0::RAW",
"GPIB0::4::INSTR",
"GPIB0::8::INSTR",
"GPIB0::9::INSTR",
"GPIB0::10::INSTR",
}
@pytest.mark.parametrize(
"resource",
[
"ASRL1::INSTR",
"GPIB0::8::INSTR",
"TCPIP0::localhost::inst0::INSTR",
"TCPIP0::localhost::10001::SOCKET",
"USB0::0x1111::0x2222::0x1234::0::INSTR",
"USB0::0x1111::0x2222::0x4445::0::RAW",
],
)
def test_instruments(resource, resource_manager):
inst = resource_manager.open_resource(
resource,
read_termination="\n",
write_termination="\r\n" if resource.startswith("ASRL") else "\n",
)
assert_instrument_response(inst, "?IDN", "LSG Serial #1234")
assert_instrument_response(inst, "?FREQ", "100.00")
assert_instrument_response(inst, "!FREQ 10.3", "OK")
assert_instrument_response(inst, "?FREQ", "10.30")
assert_instrument_response(inst, "?AMP", "1.00")
assert_instrument_response(inst, "!AMP 3.8", "OK")
assert_instrument_response(inst, "?AMP", "3.80")
assert_instrument_response(inst, "?OFF", "0.00")
assert_instrument_response(inst, "!OFF 1.2", "OK")
assert_instrument_response(inst, "?OFF", "1.20")
assert_instrument_response(inst, "?OUT", "0")
assert_instrument_response(inst, "!OUT 1", "OK")
assert_instrument_response(inst, "?OUT", "1")
assert_instrument_response(inst, "?WVF", "0")
assert_instrument_response(inst, "!WVF 1", "OK")
assert_instrument_response(inst, "?WVF", "1")
assert_instrument_response(inst, "!CAL", "OK")
# Errors
assert_instrument_response(inst, "!WVF 23", "ERROR")
assert_instrument_response(inst, "!AMP -1.0", "ERROR")
assert_instrument_response(inst, "!AMP 11.0", "ERROR")
assert_instrument_response(inst, "!FREQ 0.0", "FREQ_ERROR")
assert_instrument_response(inst, "BOGUS_COMMAND", "ERROR")
inst.close()
@pytest.mark.parametrize(
"resource",
[
"ASRL3::INSTR",
"GPIB0::10::INSTR",
"TCPIP0::localhost:3333::inst0::INSTR",
"USB0::0x1111::0x2222::0x3692::0::INSTR",
],
)
def test_instruments_on_invalid_command(resource, resource_manager):
inst = resource_manager.open_resource(
resource,
read_termination="\n",
write_termination="\r\n" if resource.startswith("ASRL") else "\n",
)
response = inst.query("FAKE_COMMAND")
assert response == "INVALID_COMMAND", "invalid command test - response"
status_reg = inst.query("*ESR?")
assert int(status_reg) == 32, "invalid command test - status"
@pytest.mark.parametrize(
"resource",
[
"ASRL2::INSTR",
"GPIB0::9::INSTR",
"TCPIP0::localhost:2222::inst0::INSTR",
"USB0::0x1111::0x2222::0x2468::0::INSTR",
],
)
def test_instrument_on_invalid_values(resource, resource_manager):
inst = resource_manager.open_resource(
resource,
read_termination="\n",
write_termination="\r\n" if resource.startswith("ASRL") else "\n",
)
inst.write("FAKE_COMMAND")
status_reg = inst.query("*ESR?")
assert int(status_reg) == 32, "invalid test command"
inst.write(":VOLT:IMM:AMPL 2.00")
status_reg = inst.query("*ESR?")
assert int(status_reg) == 0
inst.write(":VOLT:IMM:AMPL 0.5")
status_reg = inst.query("*ESR?")
assert int(status_reg) == 32, "invalid range test - <min"
inst.write(":VOLT:IMM:AMPL 6.5")
status_reg = inst.query("*ESR?")
assert int(status_reg) == 32, "invalid range test - >max"
@pytest.mark.parametrize(
"resource",
[
"ASRL3::INSTR",
"GPIB0::10::INSTR",
"TCPIP0::localhost:3333::inst0::INSTR",
"USB0::0x1111::0x2222::0x3692::0::INSTR",
],
)
def test_instruments_with_timeouts(resource, resource_manager):
inst = resource_manager.open_resource(resource, timeout=0.1)
with pytest.raises(VisaIOError):
inst.read()
@pytest.mark.parametrize(
"resource",
[
"ASRL4::INSTR",
"GPIB0::4::INSTR",
"TCPIP0::localhost:4444::inst0::INSTR",
"USB0::0x1111::0x2222::0x4444::0::INSTR",
],
)
def test_instrument_for_error_state(resource, resource_manager):
inst = resource_manager.open_resource(
resource,
read_termination="\n",
write_termination="\r\n" if resource.startswith("ASRL") else "\n",
)
assert_instrument_response(inst, ":SYST:ERR?", "0, No Error")
inst.write("FAKE COMMAND")
assert_instrument_response(inst, ":SYST:ERR?", "1, Command error")
inst.write(":VOLT:IMM:AMPL 0")
assert_instrument_response(inst, ":SYST:ERR?", "1, Command error")
def test_device_write_logging(caplog, resource_manager) -> None:
instr = resource_manager.open_resource(
"USB0::0x1111::0x2222::0x4444::0::INSTR",
read_termination="\n",
write_termination="\n",
)
with caplog.at_level(logging.DEBUG):
instr.write("*IDN?")
instr.read()
assert "input buffer: b'D'" not in caplog.text
assert r"input buffer: b'*IDN?\n'" in caplog.text
|