| 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, VISAAdapter |
| 29 |
from pymeasure.instruments import Instrument |
| 30 |
|
| 31 |
|
| 32 |
# As an instrument contributor, I want to define default connection settings for my |
| 33 |
# instrument with a minimum of boiler-plate. These settings should be easily |
| 34 |
# user-overridable if appropriate without having to change the code/implementation |
| 35 |
# of my instrument. |
| 36 |
class MultiprotocolInstrument(Instrument): |
| 37 |
"""Test instrument for testing the configuration and use of multiple connection methods. |
| 38 |
|
| 39 |
The instrument support multiple transports methods(serial, TCP/IP, GPIB). |
| 40 |
In case of serial connections, the default baud rate is low (2400), but configurable in |
| 41 |
the device, so it can use faster communication if desired. |
| 42 |
Two examples in our instruments are AMI430, Keithley2260B (for TCP/IP, there are more |
| 43 |
with GPIB capability) |
| 44 |
|
| 45 |
It also has an attribute to configure a non-serial (gpib) connection. This demonstrates |
| 46 |
that there may be attributes that are not valid for a serial connection, only others. |
| 47 |
""" |
| 48 |
def __init__(self, adapter, name="Instrument with multiple connection methods", |
| 49 |
baud_rate=2400, **kwargs): |
| 50 |
# - baud_rate is placed in the signature if it's expected to be modified by the user |
| 51 |
# - baud_rate is only valid for the asrl protocol |
| 52 |
# - enable_repeat_addressing is only for gpib and always False for this device |
| 53 |
# - timeout is the same for all pyvisa protocols, using setdefault it is user-modifiable |
| 54 |
# without cluttering the signature: |
| 55 |
kwargs.setdefault('timeout', 1500) |
| 56 |
|
| 57 |
super().__init__(adapter, |
| 58 |
name=name, |
| 59 |
gpib=dict(enable_repeat_addressing=False, |
| 60 |
read_termination='\r'), |
| 61 |
asrl={'baud_rate': baud_rate, |
| 62 |
'read_termination': '\r\n'}, |
| 63 |
**kwargs, # all others/generally valid kwargs |
| 64 |
) |
| 65 |
|
| 66 |
# The defined tests use the pyvisa-sim simulated connections to avoid the need for a connected |
| 67 |
# instrument. The argument handling and connection creation happens in pyvisa just like for a |
| 68 |
# real instrument, through. |
| 69 |
|
| 70 |
|
| 71 |
def test_serial_default_settings(): |
| 72 |
"""As a user, I want to simply connect to an instrument using default settings""" |
| 73 |
instr = MultiprotocolInstrument(adapter='ASRL1::INSTR', visa_library='@sim') |
| 74 |
assert instr.adapter.connection.baud_rate == 2400 |
| 75 |
|
| 76 |
|
| 77 |
def test_serial_custom_baud_rate_is_set(): |
| 78 |
"""As a user, I want to easily override default settings to fit my needs""" |
| 79 |
instr = MultiprotocolInstrument(adapter='ASRL1::INSTR', baud_rate=115200, visa_library='@sim') |
| 80 |
assert instr.adapter.connection.baud_rate == 115200 |
| 81 |
|
| 82 |
|
| 83 |
def test_connections_use_tcpip(): |
| 84 |
"""As a user, I want to be free to choose which connection to use (e.g. serial-over RS-232, |
| 85 |
USB, TCP/IP) should an instrument support more than one |
| 86 |
""" |
| 87 |
# here, baud_rate is invalid for pyvisa, but is a default kwarg anyway |
| 88 |
instr = MultiprotocolInstrument(adapter='TCPIP::localhost:1111::INSTR', visa_library='@sim') |
| 89 |
# method specific to pyvisa TCPIPInstrument |
| 90 |
assert hasattr(instr.adapter.connection, 'control_ren') |
| 91 |
|
| 92 |
|
| 93 |
def test_connections_use_gpib(): |
| 94 |
"""As a user, I want to be free to choose which connection to use (e.g. serial-over RS-232, |
| 95 |
USB, TCP/IP) should an instrument support more than one |
| 96 |
""" |
| 97 |
# here, baud_rate is invalid for pyvisa, but is a default kwarg anyway |
| 98 |
instr = MultiprotocolInstrument(adapter='GPIB::8::INSTR', visa_library='@sim') |
| 99 |
# attribute specific to pyvisa GPIBInstrument |
| 100 |
assert instr.adapter.connection.enable_repeat_addressing is False |
| 101 |
|
| 102 |
|
| 103 |
def test_use_separate_SerialAdapter(): |
| 104 |
"""As a user, I want to be able to supply a self-generated Adapter instance |
| 105 |
(e.g. to enable serial connection sharing over RS-485/-422) |
| 106 |
""" |
| 107 |
# note: baudrate, not baud_rate, as this goes to pyserial |
| 108 |
ser = SerialAdapter(serial.serial_for_url("loop://", baudrate=4800)) |
| 109 |
instr = MultiprotocolInstrument(ser) |
| 110 |
# don't need visa_library here as this does not go to pyvisa, kwargs are ignored if provided |
| 111 |
assert instr.adapter.connection.baudrate == 4800 |
| 112 |
|
| 113 |
|
| 114 |
def test_separate_adapter_discards_additional_args(): |
| 115 |
"""When passing in a separate Adapter instance, all connection-specific args and settings |
| 116 |
are ignored. |
| 117 |
""" |
| 118 |
ser = SerialAdapter(serial.serial_for_url("loop://")) |
| 119 |
instr = MultiprotocolInstrument(ser, baud_rate=1234, wrong_kwarg='fizzbuzz') |
| 120 |
assert instr.adapter.connection.baudrate == 9600 # default of serial |
| 121 |
|
| 122 |
|
| 123 |
def test_use_separate_VISAAdapter(): |
| 124 |
"""As a user, I want to be able to supply my own VISAAdpter with my settings |
| 125 |
""" |
| 126 |
# User can use their own VISAAdapter |
| 127 |
ser = VISAAdapter('ASRL1::INSTR', baud_rate=1200, visa_library='@sim') |
| 128 |
instr = MultiprotocolInstrument(ser) |
| 129 |
# don't need visa_library here as this does not go to pyvisa, kwargs are ignored if provided |
| 130 |
assert instr.adapter.connection.baud_rate == 1200 |
| 131 |
|
| 132 |
|
| 133 |
def test_incorrect_arg_is_flagged(): |
| 134 |
"""As a user or instrument contributor that used an incorrect/invalid (kw)arg, |
| 135 |
I want to be alerted to that fact. |
| 136 |
""" |
| 137 |
with pytest.raises(ValueError, match='bitrate'): |
| 138 |
_ = MultiprotocolInstrument(adapter='ASRL1::INSTR', bitrate=1234, visa_library='@sim') |
| 139 |
|
| 140 |
|
| 141 |
def test_incorrect_interface_type_is_flagged(): |
| 142 |
"""As a user or instrument contributor that used an incorrect/invalid interface type, |
| 143 |
I want to be alerted to that fact. |
| 144 |
""" |
| 145 |
class WrongInterfaceInstrument(Instrument): |
| 146 |
def __init__(self, adapter, name="Instrument with incorrect interface name", **kwargs): |
| 147 |
super().__init__(adapter, |
| 148 |
name=name, |
| 149 |
arsl={'read_termination': '\r\n'}, # typo here |
| 150 |
**kwargs, |
| 151 |
) |
| 152 |
with pytest.raises(ValueError, match='arsl'): |
| 153 |
_ = WrongInterfaceInstrument(adapter='ASRL1::INSTR', visa_library='@sim') |
| 154 |
|
| 155 |
|
| 156 |
def test_improper_arg_is_flagged(): |
| 157 |
"""As a user or instrument contributor that used a kwarg that is inappropriate for the present |
| 158 |
connection, I want to be alerted to that fact. |
| 159 |
""" |
| 160 |
with pytest.raises(ValueError, match='enable_repeat_addressing'): |
| 161 |
_ = MultiprotocolInstrument(adapter='ASRL1::INSTR', enable_repeat_addressing=True, |
| 162 |
visa_library='@sim') |
| 163 |
|
| 164 |
|
| 165 |
def test_common_kwargs_are_retained(): |
| 166 |
instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', visa_library='@sim') |
| 167 |
assert instr1.adapter.connection.timeout == 1500 |
| 168 |
instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', visa_library='@sim') |
| 169 |
assert instr2.adapter.connection.timeout == 1500 |
| 170 |
|
| 171 |
|
| 172 |
def test_distinct_interface_specific_defaults(): |
| 173 |
"""As an instrument implementor, I can easily prescribe default settings |
| 174 |
that are different between interfaces. |
| 175 |
""" |
| 176 |
instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', visa_library='@sim') |
| 177 |
assert instr1.adapter.connection.read_termination == '\r\n' |
| 178 |
instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', visa_library='@sim') |
| 179 |
assert instr2.adapter.connection.read_termination == '\r' |
| 180 |
|
| 181 |
|
| 182 |
def test_modified_non_signature_kwargs_are_retained(): |
| 183 |
instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', timeout=3000, visa_library='@sim') |
| 184 |
assert instr1.adapter.connection.timeout == 3000 |
| 185 |
instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', timeout=3000, visa_library='@sim') |
| 186 |
assert instr2.adapter.connection.timeout == 3000 |
| 187 |
|
| 188 |
|
| 189 |
def test_override_interface_specific_defaults(): |
| 190 |
"""As as user, I want to be able to override interface-specific hardcoded defaults. |
| 191 |
""" |
| 192 |
instr1 = MultiprotocolInstrument(adapter='ASRL1::INSTR', read_termination='\r', |
| 193 |
visa_library='@sim') |
| 194 |
assert instr1.adapter.connection.read_termination == '\r' |
| 195 |
instr2 = MultiprotocolInstrument(adapter='GPIB::8::INSTR', enable_repeat_addressing=True, |
| 196 |
visa_library='@sim') |
| 197 |
assert instr2.adapter.connection.enable_repeat_addressing is True |