| 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 logging |
| 26 |
from warnings import warn |
| 27 |
|
| 28 |
from pymeasure.instruments import Instrument, SCPIMixin |
| 29 |
|
| 30 |
from .buffer import KeithleyBuffer |
| 31 |
|
| 32 |
import numpy as np |
| 33 |
import time |
| 34 |
|
| 35 |
log = logging.getLogger(__name__) |
| 36 |
log.addHandler(logging.NullHandler()) |
| 37 |
|
| 38 |
|
| 39 |
def clist_validator(value, values): |
| 40 |
""" Provides a validator function that returns a valid clist string |
| 41 |
for channel commands of the Keithley 2700. Otherwise it raises a |
| 42 |
ValueError. |
| 43 |
|
| 44 |
:param value: A value to test |
| 45 |
:param values: A range of values (range, list, etc.) |
| 46 |
:raises: ValueError if the value is out of the range |
| 47 |
""" |
| 48 |
# Convert value to list of strings |
| 49 |
if isinstance(value, str): |
| 50 |
clist = [value.strip(" @(),")] |
| 51 |
elif isinstance(value, (int, float)): |
| 52 |
clist = [f"{value:d}"] |
| 53 |
elif isinstance(value, (list, tuple, np.ndarray, range)): |
| 54 |
clist = [f"{x:d}" for x in value] |
| 55 |
else: |
| 56 |
raise ValueError(f"Type of value ({type(value)}) not valid") |
| 57 |
|
| 58 |
# Pad numbers to length (if required) |
| 59 |
clist = [c.rjust(2, "0") for c in clist] |
| 60 |
clist = [c.rjust(3, "1") for c in clist] |
| 61 |
|
| 62 |
# Check channels against valid channels |
| 63 |
for c in clist: |
| 64 |
if int(c) not in values: |
| 65 |
raise ValueError( |
| 66 |
f"Channel number {value:g} not valid." |
| 67 |
) |
| 68 |
|
| 69 |
# Convert list of strings to clist format |
| 70 |
clist = "(@{:s})".format(", ".join(clist)) |
| 71 |
|
| 72 |
return clist |
| 73 |
|
| 74 |
|
| 75 |
def text_length_validator(value, values): |
| 76 |
""" Provides a validator function that a valid string for the display |
| 77 |
commands of the Keithley. Raises a TypeError if value is not a string. |
| 78 |
If the string is too long, it is truncated to the correct length. |
| 79 |
|
| 80 |
:param value: A value to test |
| 81 |
:param values: The allowed length of the text |
| 82 |
""" |
| 83 |
|
| 84 |
if not isinstance(value, str): |
| 85 |
raise TypeError("Value is not a string.") |
| 86 |
|
| 87 |
return value[:values] |
| 88 |
|
| 89 |
|
| 90 |
class Keithley2700(KeithleyBuffer, SCPIMixin, Instrument): |
| 91 |
""" Represents the Keithley 2700 Multimeter/Switch System and provides a |
| 92 |
high-level interface for interacting with the instrument. |
| 93 |
|
| 94 |
.. code-block:: python |
| 95 |
|
| 96 |
keithley = Keithley2700("GPIB::1") |
| 97 |
|
| 98 |
""" |
| 99 |
|
| 100 |
CLIST_VALUES = list(range(101, 300)) |
| 101 |
|
| 102 |
def __init__(self, adapter, name="Keithley 2700 MultiMeter/Switch System", **kwargs): |
| 103 |
super().__init__( |
| 104 |
adapter, |
| 105 |
name, |
| 106 |
**kwargs |
| 107 |
) |
| 108 |
|
| 109 |
self.check_errors() |
| 110 |
self.determine_valid_channels() |
| 111 |
|
| 112 |
# Routing commands |
| 113 |
closed_channels = Instrument.control( |
| 114 |
"ROUTe:MULTiple:CLOSe?", "ROUTe:MULTiple:CLOSe %s", |
| 115 |
""" Parameter that controls the opened and closed channels. |
| 116 |
All mentioned channels are closed, other channels will be opened. |
| 117 |
""", |
| 118 |
validator=clist_validator, |
| 119 |
values=CLIST_VALUES, |
| 120 |
check_get_errors=True, |
| 121 |
check_set_errors=True, |
| 122 |
separator=None, |
| 123 |
get_process=lambda v: [ |
| 124 |
int(vv) for vv in (v.strip(" ()@,").split(",")) if not vv == "" |
| 125 |
], |
| 126 |
) |
| 127 |
|
| 128 |
open_channels = Instrument.setting( |
| 129 |
"ROUTe:MULTiple:OPEN %s", |
| 130 |
""" A parameter that opens the specified list of channels. Can only |
| 131 |
be set. |
| 132 |
""", |
| 133 |
validator=clist_validator, |
| 134 |
values=CLIST_VALUES, |
| 135 |
check_set_errors=True |
| 136 |
) |
| 137 |
|
| 138 |
def get_state_of_channels(self, channels): |
| 139 |
""" Get the open or closed state of the specified channels |
| 140 |
|
| 141 |
:param channels: a list of channel numbers, or single channel number |
| 142 |
""" |
| 143 |
clist = clist_validator(channels, self.CLIST_VALUES) |
| 144 |
state = self.ask("ROUTe:MULTiple:STATe? %s" % clist) |
| 145 |
|
| 146 |
return state |
| 147 |
|
| 148 |
def open_all_channels(self): |
| 149 |
""" Open all channels of the Keithley 2700. |
| 150 |
""" |
| 151 |
self.write(":ROUTe:OPEN:ALL") |
| 152 |
|
| 153 |
def determine_valid_channels(self): |
| 154 |
""" Determine what cards are installed into the Keithley 2700 |
| 155 |
and from that determine what channels are valid. |
| 156 |
""" |
| 157 |
self.CLIST_VALUES.clear() |
| 158 |
|
| 159 |
self.cards = {slot: card for slot, card in enumerate(self.options, 1)} |
| 160 |
|
| 161 |
for slot, card in self.cards.items(): |
| 162 |
|
| 163 |
if card == "none": |
| 164 |
continue |
| 165 |
elif card == "7709": |
| 166 |
"""The 7709 is a 6(rows) x 8(columns) matrix card, with two |
| 167 |
additional switches (49 & 50) that allow row 1 and 2 to be |
| 168 |
connected to the DMM backplane (input and sense respectively). |
| 169 |
""" |
| 170 |
channels = range(1, 51) |
| 171 |
else: |
| 172 |
log.warning( |
| 173 |
f"Card type {card} at slot {slot} is not yet implemented." |
| 174 |
) |
| 175 |
continue |
| 176 |
|
| 177 |
channels = [100 * slot + ch for ch in channels] |
| 178 |
|
| 179 |
self.CLIST_VALUES.extend(channels) |
| 180 |
|
| 181 |
def close_rows_to_columns(self, rows, columns, slot=None): |
| 182 |
""" Closes (connects) the channels between column(s) and row(s) |
| 183 |
of the 7709 connection matrix. |
| 184 |
Only one of the parameters 'rows' or 'columns' can be "all" |
| 185 |
|
| 186 |
:param rows: row number or list of numbers; can also be "all" |
| 187 |
:param columns: column number or list of numbers; can also be "all" |
| 188 |
:param slot: slot number (1 or 2) of the 7709 card to be used |
| 189 |
""" |
| 190 |
|
| 191 |
channels = self.channels_from_rows_columns(rows, columns, slot) |
| 192 |
self.closed_channels = channels |
| 193 |
|
| 194 |
def open_rows_to_columns(self, rows, columns, slot=None): |
| 195 |
""" Opens (disconnects) the channels between column(s) and row(s) |
| 196 |
of the 7709 connection matrix. |
| 197 |
Only one of the parameters 'rows' or 'columns' can be "all" |
| 198 |
|
| 199 |
:param rows: row number or list of numbers; can also be "all" |
| 200 |
:param columns: column number or list of numbers; can also be "all" |
| 201 |
:param slot: slot number (1 or 2) of the 7709 card to be used |
| 202 |
""" |
| 203 |
|
| 204 |
channels = self.channels_from_rows_columns(rows, columns, slot) |
| 205 |
self.open_channels = channels |
| 206 |
|
| 207 |
def channels_from_rows_columns(self, rows, columns, slot=None): |
| 208 |
""" Determine the channel numbers between column(s) and row(s) of the |
| 209 |
7709 connection matrix. Returns a list of channel numbers. |
| 210 |
Only one of the parameters 'rows' or 'columns' can be "all" |
| 211 |
|
| 212 |
:param rows: row number or list of numbers; can also be "all" |
| 213 |
:param columns: column number or list of numbers; can also be "all" |
| 214 |
:param slot: slot number (1 or 2) of the 7709 card to be used |
| 215 |
|
| 216 |
""" |
| 217 |
|
| 218 |
if slot is not None and self.cards[slot] != "7709": |
| 219 |
raise ValueError("No 7709 card installed in slot %g" % slot) |
| 220 |
|
| 221 |
if isinstance(rows, str) and isinstance(columns, str): |
| 222 |
raise ValueError("Only one parameter can be 'all'") |
| 223 |
elif isinstance(rows, str) and rows == "all": |
| 224 |
rows = list(range(1, 7)) |
| 225 |
elif isinstance(columns, str) and columns == "all": |
| 226 |
columns = list(range(1, 9)) |
| 227 |
|
| 228 |
if isinstance(rows, (list, tuple, np.ndarray)) and \ |
| 229 |
isinstance(columns, (list, tuple, np.ndarray)): |
| 230 |
|
| 231 |
if len(rows) != len(columns): |
| 232 |
raise ValueError("The length of the rows and columns do not match") |
| 233 |
|
| 234 |
# Flatten (were necessary) the arrays |
| 235 |
new_rows = [] |
| 236 |
new_columns = [] |
| 237 |
for row, column in zip(rows, columns): |
| 238 |
if isinstance(row, int) and isinstance(column, int): |
| 239 |
new_rows.append(row) |
| 240 |
new_columns.append(column) |
| 241 |
elif isinstance(row, (list, tuple, np.ndarray)) and isinstance(column, int): |
| 242 |
new_columns.extend(len(row) * [column]) |
| 243 |
new_rows.extend(list(row)) |
| 244 |
elif isinstance(column, (list, tuple, np.ndarray)) and isinstance(row, int): |
| 245 |
new_columns.extend(list(column)) |
| 246 |
new_rows.extend(len(column) * [row]) |
| 247 |
|
| 248 |
rows = new_rows |
| 249 |
columns = new_columns |
| 250 |
|
| 251 |
# Determine channel number from rows and columns number. |
| 252 |
rows = np.array(rows, ndmin=1) |
| 253 |
columns = np.array(columns, ndmin=1) |
| 254 |
|
| 255 |
channels = (rows - 1) * 8 + columns |
| 256 |
|
| 257 |
if slot is not None: |
| 258 |
channels += 100 * slot |
| 259 |
|
| 260 |
return channels |
| 261 |
|
| 262 |
# system, some taken from Keithley 2400 |
| 263 |
def beep(self, frequency, duration): |
| 264 |
""" Sounds a system beep. |
| 265 |
|
| 266 |
:param frequency: A frequency in Hz between 65 Hz and 2 MHz |
| 267 |
:param duration: A time in seconds between 0 and 7.9 seconds |
| 268 |
""" |
| 269 |
self.write(f":SYST:BEEP {frequency:g}, {duration:g}") |
| 270 |
|
| 271 |
def triad(self, base_frequency, duration): |
| 272 |
""" Sounds a musical triad using the system beep. |
| 273 |
|
| 274 |
:param base_frequency: A frequency in Hz between 65 Hz and 1.3 MHz |
| 275 |
:param duration: A time in seconds between 0 and 7.9 seconds |
| 276 |
""" |
| 277 |
self.beep(base_frequency, duration) |
| 278 |
time.sleep(duration) |
| 279 |
self.beep(base_frequency * 5.0 / 4.0, duration) |
| 280 |
time.sleep(duration) |
| 281 |
self.beep(base_frequency * 6.0 / 4.0, duration) |
| 282 |
|
| 283 |
@property |
| 284 |
def error(self): |
| 285 |
warn("Deprecated to use `error`, use `next_error` instead.", FutureWarning) |
| 286 |
return self.next_error |
| 287 |
|
| 288 |
def reset(self): |
| 289 |
""" Resets the instrument and clears the queue. """ |
| 290 |
self.write("status:queue:clear;*RST;:stat:pres;:*CLS;") |
| 291 |
|
| 292 |
options = Instrument.measurement( |
| 293 |
"*OPT?", |
| 294 |
"""Property that lists the installed cards in the Keithley 2700. |
| 295 |
Returns a dict with the integer card numbers on the position.""", |
| 296 |
cast=False |
| 297 |
) |
| 298 |
|
| 299 |
########### |
| 300 |
# DISPLAY # |
| 301 |
########### |
| 302 |
|
| 303 |
text_enabled = Instrument.control( |
| 304 |
"DISP:TEXT:STAT?", "DISP:TEXT:STAT %d", |
| 305 |
""" A boolean property that controls whether a text message can be |
| 306 |
shown on the display of the Keithley 2700. |
| 307 |
""", |
| 308 |
values={True: 1, False: 0}, |
| 309 |
map_values=True, |
| 310 |
) |
| 311 |
display_text = Instrument.control( |
| 312 |
"DISP:TEXT:DATA?", "DISP:TEXT:DATA '%s'", |
| 313 |
""" A string property that controls the text shown on the display of |
| 314 |
the Keithley 2700. Text can be up to 12 ASCII characters and must be |
| 315 |
enabled to show. |
| 316 |
""", |
| 317 |
validator=text_length_validator, |
| 318 |
values=12, |
| 319 |
cast=str, |
| 320 |
separator="NO_SEPARATOR", |
| 321 |
get_process=lambda v: v.strip("'\""), |
| 322 |
) |
| 323 |
|
| 324 |
def display_closed_channels(self): |
| 325 |
""" Show the presently closed channels on the display of the Keithley |
| 326 |
2700. |
| 327 |
""" |
| 328 |
|
| 329 |
# Get the closed channels and make a string of the list |
| 330 |
channels = self.closed_channels |
| 331 |
channel_string = " ".join([ |
| 332 |
str(channel % 100) for channel in channels |
| 333 |
]) |
| 334 |
|
| 335 |
# Prepend "Closed: " or "C: " to the string, depending on the length |
| 336 |
str_length = 12 |
| 337 |
if len(channel_string) < str_length - 8: |
| 338 |
channel_string = "Closed: " + channel_string |
| 339 |
elif len(channel_string) < str_length - 3: |
| 340 |
channel_string = "C: " + channel_string |
| 341 |
|
| 342 |
# enable displaying text-messages |
| 343 |
self.text_enabled = True |
| 344 |
|
| 345 |
# write the string to the display |
| 346 |
self.display_text = channel_string |