| 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 |
from warnings import warn |
| 26 |
from pymeasure.instruments import Instrument, SCPIUnknownMixin |
| 27 |
from pymeasure.instruments.validators import strict_discrete_set |
| 28 |
|
| 29 |
|
| 30 |
deprecated_text = """ |
| 31 |
|
| 32 |
.. deprecated:: 0.12 Use the :code:`function_` and :code:`reading` properties instead. |
| 33 |
""" |
| 34 |
|
| 35 |
|
| 36 |
def _deprecation_warning(property_name): |
| 37 |
def func(x): |
| 38 |
warn(f'Deprecated property name "{property_name}", use the "function_" ' |
| 39 |
'and "reading" properties instead.', FutureWarning) |
| 40 |
return x |
| 41 |
return func |
| 42 |
|
| 43 |
|
| 44 |
class HP34401A(SCPIUnknownMixin, Instrument): |
| 45 |
""" Represents the HP / Agilent / Keysight 34401A Multimeter and |
| 46 |
provides a high-level interface for interacting with the instrument. |
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
dmm = HP34401A("GPIB::1") |
| 51 |
dmm.function_ = "DCV" |
| 52 |
print(dmm.reading) # -> Single float reading |
| 53 |
|
| 54 |
dmm.nplc = 0.02 |
| 55 |
dmm.autozero_enabled = False |
| 56 |
dmm.trigger_count = 100 |
| 57 |
dmm.trigger_delay = "MIN" |
| 58 |
print(dmm.reading) # -> Array of 100 very fast readings |
| 59 |
|
| 60 |
""" |
| 61 |
FUNCTIONS = {"DCV": "VOLT", "DCV_RATIO": "VOLT:RAT", |
| 62 |
"ACV": "VOLT:AC", "DCI": "CURR", "ACI": "CURR:AC", |
| 63 |
"R2W": "RES", "R4W": "FRES", "FREQ": "FREQ", |
| 64 |
"PERIOD": "PER", "CONTINUITY": "CONT", "DIODE": "DIOD"} |
| 65 |
|
| 66 |
FUNCTIONS_WITH_RANGE = { |
| 67 |
"DCV": "VOLT", "ACV": "VOLT:AC", "DCI": "CURR", |
| 68 |
"ACI": "CURR:AC", "R2W": "RES", "R4W": "FRES", |
| 69 |
"FREQ": "FREQ", "PERIOD": "PER"} |
| 70 |
|
| 71 |
BOOL_MAPPINGS = {True: 1, False: 0} |
| 72 |
|
| 73 |
# Below: stop_bits: 20 comes from |
| 74 |
# https://pyvisa.readthedocs.io/en/latest/api/constants.html#pyvisa.constants.StopBits |
| 75 |
def __init__(self, adapter, name="HP 34401A", **kwargs): |
| 76 |
super().__init__( |
| 77 |
adapter, |
| 78 |
name, |
| 79 |
asrl={'baud_rate': 9600, 'data_bits': 8, 'parity': 0, 'stop_bits': 20}, |
| 80 |
**kwargs |
| 81 |
) |
| 82 |
|
| 83 |
# Log a deprecated warning for the old function property |
| 84 |
voltage_ac = Instrument.measurement("MEAS:VOLT:AC? DEF,DEF", |
| 85 |
"AC voltage, in Volts" + deprecated_text, |
| 86 |
get_process=_deprecation_warning('voltage_ac')) |
| 87 |
|
| 88 |
current_dc = Instrument.measurement("MEAS:CURR:DC? DEF,DEF", |
| 89 |
"DC current, in Amps" + deprecated_text, |
| 90 |
get_process=_deprecation_warning('current_dc')) |
| 91 |
|
| 92 |
current_ac = Instrument.measurement("MEAS:CURR:AC? DEF,DEF", |
| 93 |
"AC current, in Amps" + deprecated_text, |
| 94 |
get_process=_deprecation_warning('current_ac')) |
| 95 |
|
| 96 |
resistance = Instrument.measurement("MEAS:RES? DEF,DEF", |
| 97 |
"Resistance, in Ohms" + deprecated_text, |
| 98 |
get_process=_deprecation_warning('resistance')) |
| 99 |
|
| 100 |
resistance_4w = Instrument.measurement( |
| 101 |
"MEAS:FRES? DEF,DEF", |
| 102 |
"Four-wires (remote sensing) resistance, in Ohms" + deprecated_text, |
| 103 |
get_process=_deprecation_warning('resistance_4w')) |
| 104 |
|
| 105 |
function_ = Instrument.control( |
| 106 |
"FUNC?", "FUNC \"%s\"", |
| 107 |
"""Control the measurement function. |
| 108 |
|
| 109 |
Allowed values: "DCV", "DCV_RATIO", "ACV", "DCI", "ACI", |
| 110 |
"R2W", "R4W", "FREQ", "PERIOD", "CONTINUITY", "DIODE".""", |
| 111 |
validator=strict_discrete_set, |
| 112 |
values=FUNCTIONS, |
| 113 |
map_values=True, |
| 114 |
get_process=lambda v: v.strip('"'), |
| 115 |
) |
| 116 |
|
| 117 |
range_ = Instrument.control( |
| 118 |
"{function_prefix_for_range}:RANG?", "{function_prefix_for_range}:RANG %s", |
| 119 |
"""Control the range for the currently active function. |
| 120 |
|
| 121 |
For frequency and period measurements, ranging applies to |
| 122 |
the signal's input voltage, not its frequency""", |
| 123 |
) |
| 124 |
|
| 125 |
autorange = Instrument.control( |
| 126 |
"{function_prefix_for_range}:RANG:AUTO?", |
| 127 |
"{function_prefix_for_range}:RANG:AUTO %d", |
| 128 |
"""Control the autorange state for the currently active function.""", |
| 129 |
validator=strict_discrete_set, |
| 130 |
values=BOOL_MAPPINGS, |
| 131 |
map_values=True, |
| 132 |
) |
| 133 |
|
| 134 |
resolution = Instrument.control( |
| 135 |
"{function}:RES?", "{function}:RES %g", |
| 136 |
"""Control the resolution of the measurements. |
| 137 |
|
| 138 |
Not valid for frequency, period, or ratio. |
| 139 |
Specify the resolution in the same units as the |
| 140 |
measurement function, not in number of digits. |
| 141 |
Results in a "Settings Conflict" error if autorange is enabled. |
| 142 |
MIN selects the smallest value accepted, which gives the most resolution. |
| 143 |
MAX selects the largest value accepted which gives the least resolution.""", |
| 144 |
) |
| 145 |
|
| 146 |
nplc = Instrument.control( |
| 147 |
"{function}:NPLC?", "{function}:NPLC %s", |
| 148 |
"""Control the integration time in number of power line cycles (NPLC). |
| 149 |
|
| 150 |
Valid values: 0.02, 0.2, 1, 10, 100, "MIN", "MAX". |
| 151 |
This command is valid only for dc volts, ratio, dc current, |
| 152 |
2-wire ohms, and 4-wire ohms.""", |
| 153 |
validator=strict_discrete_set, |
| 154 |
values=[0.02, 0.2, 1, 10, 100, "MIN", "MAX"], |
| 155 |
) |
| 156 |
|
| 157 |
gate_time = Instrument.control( |
| 158 |
"{function}:APER?", "{function}:APER %s", |
| 159 |
"""Control the gate time (or aperture time) for frequency or period measurements. |
| 160 |
|
| 161 |
Valid values: 0.01, 0.1, 1, "MIN", "MAX". |
| 162 |
Specifically: 10 ms (4.5 digits), 100 ms (default; 5.5 digits), |
| 163 |
or 1 second (6.5 digits).""", |
| 164 |
validator=strict_discrete_set, |
| 165 |
values=[0.01, 0.1, 1, "MIN", "MAX"], |
| 166 |
) |
| 167 |
|
| 168 |
detector_bandwidth = Instrument.control( |
| 169 |
"DET:BAND?", "DET:BAND %s", |
| 170 |
"""Control the lowest frequency expected in the input signal in Hertz. |
| 171 |
|
| 172 |
Valid values: 3, 20, 200, "MIN", "MAX".""", |
| 173 |
validator=strict_discrete_set, |
| 174 |
values=[3, 20, 200, "MIN", "MAX"], |
| 175 |
) |
| 176 |
|
| 177 |
autozero_enabled = Instrument.control( |
| 178 |
"ZERO:AUTO?", "ZERO:AUTO %s", |
| 179 |
"""Control the autozero state.""", |
| 180 |
validator=strict_discrete_set, |
| 181 |
values=BOOL_MAPPINGS, |
| 182 |
map_values=True, |
| 183 |
) |
| 184 |
|
| 185 |
def trigger_single_autozero(self): |
| 186 |
"""Trigger an autozero measurement. |
| 187 |
|
| 188 |
Consequent autozero measurements are disabled.""" |
| 189 |
self.write("ZERO:AUTO ONCE") |
| 190 |
|
| 191 |
auto_input_impedance_enabled = Instrument.control( |
| 192 |
"INP:IMP:AUTO?", "INP:IMP:AUTO %s", |
| 193 |
"""Control if automatic input resistance mode is enabled. |
| 194 |
|
| 195 |
Only valid for dc voltage measurements. |
| 196 |
When disabled (default), the input resistance is fixed |
| 197 |
at 10 MOhms for all ranges. With AUTO ON, the input resistance is set to |
| 198 |
>10 GOhms for the 100 mV, 1 V, and 10 V ranges.""", |
| 199 |
validator=strict_discrete_set, |
| 200 |
values=BOOL_MAPPINGS, |
| 201 |
map_values=True, |
| 202 |
) |
| 203 |
|
| 204 |
terminals_used = Instrument.measurement( |
| 205 |
"ROUT:TERM?", |
| 206 |
"""Query the multimeter to determine if the front or rear input terminals |
| 207 |
are selected. |
| 208 |
|
| 209 |
Returns "FRONT" or "REAR".""", |
| 210 |
values={"FRONT": "FRON", "REAR": "REAR"}, |
| 211 |
map_values=True, |
| 212 |
) |
| 213 |
|
| 214 |
# Trigger related commands |
| 215 |
def init_trigger(self): |
| 216 |
"""Set the state of the triggering system to "wait-for-trigger". |
| 217 |
|
| 218 |
Measurements will begin when the specified trigger conditions |
| 219 |
are satisfied after this command is received.""" |
| 220 |
self.write("INIT"), |
| 221 |
|
| 222 |
reading = Instrument.measurement( |
| 223 |
"READ?", |
| 224 |
"""Take a measurement of the currently selected function. |
| 225 |
|
| 226 |
Reading this property is equivalent to calling `init_trigger()`, |
| 227 |
waiting for completion and fetching the reading(s).""", |
| 228 |
) |
| 229 |
|
| 230 |
trigger_source = Instrument.control( |
| 231 |
"TRIG:SOUR?", "TRIG:SOUR %s", |
| 232 |
"""Control the trigger source. |
| 233 |
|
| 234 |
Valid values: "IMM", "BUS", "EXT" |
| 235 |
The multimeter will accept a software (bus) trigger, |
| 236 |
an immediate internal trigger (this is the default source), |
| 237 |
or a hardware trigger from the rear-panel |
| 238 |
Ext Trig (external trigger) terminal.""", |
| 239 |
validator=strict_discrete_set, |
| 240 |
values=["IMM", "BUS", "EXT"], |
| 241 |
) |
| 242 |
|
| 243 |
trigger_delay = Instrument.control( |
| 244 |
"TRIG:DEL?", "TRIG:DEL %s", |
| 245 |
"""Control the trigger delay in seconds. |
| 246 |
|
| 247 |
Valid values (incl. floats): 0 to 3600 seconds, "MIN", "MAX".""", |
| 248 |
) |
| 249 |
|
| 250 |
trigger_auto_delay_enabled = Instrument.control( |
| 251 |
"TRIG:DEL:AUTO?", "TRIG:DEL:AUTO %s", |
| 252 |
"""Control the automatic trigger delay state. |
| 253 |
|
| 254 |
If enabled, the delay is determined by function, range, integration time, |
| 255 |
and ac filter setting. Selecting a specific trigger delay value |
| 256 |
automatically turns off the automatic trigger delay.""", |
| 257 |
validator=strict_discrete_set, |
| 258 |
values=BOOL_MAPPINGS, |
| 259 |
map_values=True, |
| 260 |
) |
| 261 |
|
| 262 |
sample_count = Instrument.control( |
| 263 |
"SAMP:COUN?", "SAMP:COUN %s", |
| 264 |
"""Controls the number of samples per trigger event. |
| 265 |
|
| 266 |
Valid values: 1 to 50000, "MIN", "MAX".""", |
| 267 |
) |
| 268 |
|
| 269 |
trigger_count = Instrument.control( |
| 270 |
"TRIG:COUN?", "TRIG:COUN %s", |
| 271 |
"""Control the number of triggers accepted before returning to the "idle" state. |
| 272 |
|
| 273 |
Valid values: 1 to 50000, "MIN", "MAX", "INF". |
| 274 |
The INFinite parameter instructs the multimeter to continuously accept triggers |
| 275 |
(you must send a device clear to return to the "idle" state).""", |
| 276 |
) |
| 277 |
|
| 278 |
stored_reading = Instrument.measurement( |
| 279 |
"FETC?", |
| 280 |
"""Measure the reading(s) currently stored in the multimeter's internal memory. |
| 281 |
|
| 282 |
Reading this property will NOT initialize a trigger. |
| 283 |
If you need that, use the `reading` property instead.""", |
| 284 |
) |
| 285 |
|
| 286 |
# Display related commands |
| 287 |
display_enabled = Instrument.control( |
| 288 |
"DISP?", "DISP %s", |
| 289 |
"""Control the display state.""", |
| 290 |
validator=strict_discrete_set, |
| 291 |
values=BOOL_MAPPINGS, |
| 292 |
map_values=True, |
| 293 |
) |
| 294 |
|
| 295 |
displayed_text = Instrument.control( |
| 296 |
"DISP:TEXT?", "DISP:TEXT \"%s\"", |
| 297 |
"""Control the text displayed on the multimeter's display. |
| 298 |
|
| 299 |
The text can be up to 12 characters long; |
| 300 |
any additional characters are truncated my the multimeter.""", |
| 301 |
get_process=lambda x: x.strip('"'), |
| 302 |
) |
| 303 |
|
| 304 |
# System related commands |
| 305 |
remote_control_enabled = Instrument.control( |
| 306 |
"SYST: ", "SYST:%s", |
| 307 |
"""Control whether remote control is enabled.""", |
| 308 |
validator=strict_discrete_set, |
| 309 |
values={True: "REM", False: "LOC"}, |
| 310 |
map_values=True, |
| 311 |
) |
| 312 |
|
| 313 |
remote_lock_enabled = Instrument.control( |
| 314 |
"SYST: ", "SYST:%s", |
| 315 |
"""Control whether the beeper is enabled.""", |
| 316 |
validator=strict_discrete_set, |
| 317 |
values={True: "RWL", False: "LOC"}, |
| 318 |
map_values=True, |
| 319 |
) |
| 320 |
|
| 321 |
def beep(self): |
| 322 |
"""This command causes the multimeter to beep once.""" |
| 323 |
self.write("SYST:BEEP") |
| 324 |
|
| 325 |
beeper_enabled = Instrument.control( |
| 326 |
"SYST:BEEP:STAT?", "SYST:BEEP:STAT %s", |
| 327 |
"""Control whether the beeper is enabled.""", |
| 328 |
validator=strict_discrete_set, |
| 329 |
values=BOOL_MAPPINGS, |
| 330 |
map_values=True, |
| 331 |
) |
| 332 |
|
| 333 |
scpi_version = Instrument.measurement( |
| 334 |
"SYST:VERS?", |
| 335 |
"""The SCPI version of the multimeter.""", |
| 336 |
) |
| 337 |
|
| 338 |
stored_readings_count = Instrument.measurement( |
| 339 |
"DATA:POIN?", |
| 340 |
"""The number of readings currently stored in the internal memory.""", |
| 341 |
) |
| 342 |
|
| 343 |
self_test_result = Instrument.measurement( |
| 344 |
"*TST?", |
| 345 |
"""Initiate a self-test of the multimeter and return the result. |
| 346 |
|
| 347 |
Be sure to set an appropriate connection timeout, |
| 348 |
otherwise the command will fail.""", |
| 349 |
) |
| 350 |
|
| 351 |
def write(self, command): |
| 352 |
"""Write a command to the instrument.""" |
| 353 |
if "{function_prefix_for_range}" in command: |
| 354 |
command = command.replace("{function_prefix_for_range}", |
| 355 |
self._get_function_prefix_for_range()) |
| 356 |
elif "{function}" in command: |
| 357 |
command = command.replace("{function}", HP34401A.FUNCTIONS[self.function_]) |
| 358 |
super().write(command) |
| 359 |
|
| 360 |
def _get_function_prefix_for_range(self): |
| 361 |
function_prefix = HP34401A.FUNCTIONS_WITH_RANGE[self.function_] |
| 362 |
if function_prefix in ["FREQ", "PER"]: |
| 363 |
function_prefix += ":VOLT" |
| 364 |
return function_prefix |