| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2022 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 |
import logging |
| 25 |
import time |
| 26 |
|
| 27 |
from pymeasure.instruments import Instrument |
| 28 |
|
| 29 |
log = logging.getLogger(__name__) # https://docs.python.org/3/howto/logging.html#library-config |
| 30 |
log.addHandler(logging.NullHandler()) |
| 31 |
|
| 32 |
|
| 33 |
class ReturnValueError(Exception): |
| 34 |
pass |
| 35 |
|
| 36 |
|
| 37 |
class Racal1992(Instrument): |
| 38 |
"""Represents the Racal-Dana 1992 Universal counter |
| 39 |
|
| 40 |
.. code-block:: python |
| 41 |
|
| 42 |
from pymeasure.instruments.racal import Racal1992 |
| 43 |
counter = Racal1992("GPIB0::10") |
| 44 |
|
| 45 |
This class should also work for Racal-Dana 1991, it has the same |
| 46 |
product manual, as long as you don't use functionality that requires |
| 47 |
channel B. |
| 48 |
""" |
| 49 |
|
| 50 |
def __init__(self, adapter, name="Racal-Dana 1992", **kwargs): |
| 51 |
kwargs.setdefault('write_termination', '\r\n') |
| 52 |
|
| 53 |
super().__init__( |
| 54 |
adapter, |
| 55 |
name, |
| 56 |
includeSCPI=False, |
| 57 |
**kwargs |
| 58 |
) |
| 59 |
|
| 60 |
int_types = ['SF', 'RS', 'UT', 'MS', 'TA'] |
| 61 |
float_types = ['CK', 'FA', 'PA', 'TI', 'PH', 'RA', |
| 62 |
'MX', 'MZ', 'LA', 'LB', 'FC', 'RC', 'DT', 'GS'] |
| 63 |
|
| 64 |
channel_params = { |
| 65 |
'A' : { # noqa |
| 66 |
'coupling' : { 'AC' : 'AAC', 'DC' : 'ADC' }, # noqa |
| 67 |
'attenuation' : { 'X1' : 'AAD', 'X10' : 'AAE' }, # noqa |
| 68 |
'trigger' : { 'auto' : 'AAU', 'manual' : 'AMN' }, # noqa |
| 69 |
'impedance' : { '50' : 'ALI', '1M' : 'AHI' }, # noqa |
| 70 |
'slope' : { 'pos' : 'APS', 'neg' : 'ANS' }, # noqa |
| 71 |
'filtering' : { True : 'AFE', False : 'AFD' }, # noqa |
| 72 |
'trigger_level' : None, # noqa |
| 73 |
}, |
| 74 |
'B' : { # noqa |
| 75 |
'coupling' : { 'AC' : 'BAC', 'DC' : 'BDC' }, # noqa |
| 76 |
'attenuation' : { 'X1' : 'BAD', 'X10' : 'BAE' }, # noqa |
| 77 |
'trigger' : { 'auto' : 'BAU', 'manual' : 'BMN' }, # noqa |
| 78 |
'impedance' : { '50' : 'BLI', '1M' : 'BHI' }, # noqa |
| 79 |
'slope' : { 'pos' : 'BPS', 'neg' : 'BNS' }, # noqa |
| 80 |
'input_select' : { 'separate' : 'BCS', 'common' : 'BCC' }, # noqa |
| 81 |
'trigger_level' : None, # noqa |
| 82 |
}, |
| 83 |
} |
| 84 |
|
| 85 |
operating_modes = { |
| 86 |
'self_check' : 'CK', # noqa |
| 87 |
'frequency_a' : 'FA', # noqa |
| 88 |
'period_a' : 'PA', # noqa |
| 89 |
'phase_a_rel_b' : 'PH', # noqa |
| 90 |
'ratio_a_to_b' : 'RA', # noqa |
| 91 |
'ratio_c_to_b' : 'RC', # noqa |
| 92 |
'interval_a_to_b' : 'TI', # noqa |
| 93 |
'total_a_by_b' : 'TA', # noqa |
| 94 |
'frequency_c' : 'FC', # noqa |
| 95 |
} |
| 96 |
|
| 97 |
@staticmethod |
| 98 |
def decode(v, allowed_types=None): |
| 99 |
"""Decode received message. |
| 100 |
|
| 101 |
All values returned follow the same format: 2 letters to indicate |
| 102 |
the type of the value returned, followed by a floating point number |
| 103 |
(which could be an integer, of course.) |
| 104 |
This here, for example, is math constant Z: MZ+001.00000000E+00 |
| 105 |
""" |
| 106 |
if len(v) != 19: |
| 107 |
raise ReturnValueError("Length of instrument response must always be 19 characters") |
| 108 |
|
| 109 |
val_type = v[0:2] |
| 110 |
val = float(v[2:19]) |
| 111 |
|
| 112 |
if allowed_types and val_type not in allowed_types: |
| 113 |
raise ValueError(f"Unexpected value type returned: '{val_type}'") |
| 114 |
|
| 115 |
if val_type in Racal1992.int_types: |
| 116 |
return int(val) |
| 117 |
elif val_type in Racal1992.float_types: |
| 118 |
return val |
| 119 |
else: |
| 120 |
raise ValueError("Unsupported return type") |
| 121 |
|
| 122 |
operating_mode = Instrument.setting( |
| 123 |
"%s", |
| 124 |
"""Set operating mode. |
| 125 |
|
| 126 |
Permitted modes are: |
| 127 |
'self_check', |
| 128 |
'frequency_a', |
| 129 |
'period_a', |
| 130 |
'phase_a_rel_b', |
| 131 |
'ratio_a_to_b', |
| 132 |
'ratio_c_to_b', |
| 133 |
'interval_a_to_b', |
| 134 |
'total_a_by_b', |
| 135 |
'frequency_c' |
| 136 |
""", |
| 137 |
set_process=(lambda mode: Racal1992.operating_modes[mode]), |
| 138 |
) |
| 139 |
|
| 140 |
resolution = Instrument.control( |
| 141 |
"RRS", |
| 142 |
"SRS %d", |
| 143 |
"""Control the resolution of the counter with an integer from 3 to 10 that |
| 144 |
specifies the number of significant digits. """, |
| 145 |
get_process=(lambda v: Racal1992.decode(v, "RS")) |
| 146 |
) |
| 147 |
|
| 148 |
delay_enable = Instrument.setting( |
| 149 |
"D%s", |
| 150 |
"""Control delay. True=enable, False=disable""", |
| 151 |
values={True: "E", False: "D"}, |
| 152 |
map_values=True |
| 153 |
) |
| 154 |
|
| 155 |
delay_time = Instrument.control( |
| 156 |
"RDT", |
| 157 |
"SDT %f", |
| 158 |
"""Control delay time.""", |
| 159 |
get_process=(lambda v: Racal1992.decode(v, "DT")) |
| 160 |
) |
| 161 |
|
| 162 |
special_function_enable = Instrument.setting( |
| 163 |
"SF%s", |
| 164 |
"""Control special function. True=enable, False=disable""", |
| 165 |
values={True: "E", False: "D"}, |
| 166 |
map_values=True |
| 167 |
) |
| 168 |
|
| 169 |
# FIXME: not tested on real instrument! |
| 170 |
special_function_number = Instrument.control( |
| 171 |
"RSF", |
| 172 |
"S%d", |
| 173 |
"""Control special function.""", |
| 174 |
get_process=(lambda v: Racal1992.decode(v, "SF")) |
| 175 |
) |
| 176 |
|
| 177 |
# FIXME: not tested on real instrument! |
| 178 |
total_so_far = Instrument.measurement( |
| 179 |
"RF", |
| 180 |
"""Get total number of events so far.""", |
| 181 |
get_process=(lambda v: Racal1992.decode(v, "RF")) |
| 182 |
) |
| 183 |
|
| 184 |
software_version = Instrument.measurement( |
| 185 |
"RMS", |
| 186 |
"Get instrument software version", |
| 187 |
get_process=(lambda v: Racal1992.decode(v, "MS")) |
| 188 |
) |
| 189 |
|
| 190 |
gpib_software_version = Instrument.measurement( |
| 191 |
"RGS", |
| 192 |
"Get GPIB software version", |
| 193 |
get_process=(lambda v: Racal1992.decode(v, "GS")) |
| 194 |
) |
| 195 |
|
| 196 |
device_type = Instrument.measurement( |
| 197 |
"RUT", |
| 198 |
"""Get unit device type. Should return 1992 for a Racal-Dana 1992 |
| 199 |
or 1991 for a Racal-Dana 1991.""", |
| 200 |
get_process=(lambda v: Racal1992.decode(v, "UT")) |
| 201 |
) |
| 202 |
|
| 203 |
math_mode = Instrument.setting( |
| 204 |
"M%s", |
| 205 |
"""Set math mode. True=enable, False=disable""", |
| 206 |
values={True: "E", False: "D"}, |
| 207 |
map_values=True |
| 208 |
) |
| 209 |
|
| 210 |
math_x = Instrument.control( |
| 211 |
"RMX", |
| 212 |
"SMX %f", |
| 213 |
"""Control math constant X.""", |
| 214 |
get_process=(lambda v: Racal1992.decode(v, "MX")) |
| 215 |
) |
| 216 |
|
| 217 |
math_z = Instrument.control( |
| 218 |
"RMZ", |
| 219 |
"SMZ %f", |
| 220 |
"""Control math constant Z.""", |
| 221 |
get_process=(lambda v: Racal1992.decode(v, "MZ")) |
| 222 |
) |
| 223 |
|
| 224 |
trigger_level_a = Instrument.control( |
| 225 |
"RLA", |
| 226 |
"SLA %f", |
| 227 |
"""Control trigger level for channel A""", |
| 228 |
get_process=(lambda v: Racal1992.decode(v, "LA")) |
| 229 |
) |
| 230 |
|
| 231 |
trigger_level_b = Instrument.control( |
| 232 |
"RLB", |
| 233 |
"SLB %f", |
| 234 |
"""Control trigger level for channel B""", |
| 235 |
get_process=(lambda v: Racal1992.decode(v, "LB")) |
| 236 |
) |
| 237 |
|
| 238 |
def read(self): |
| 239 |
return self.read_bytes(21).decode('utf-8') |
| 240 |
|
| 241 |
def write(self, s): |
| 242 |
"""Add a space in front of all commands that are sent to the |
| 243 |
instrument to work around weird model issue. |
| 244 |
|
| 245 |
It shouldn't be needed on almost all devices, but it also doesn't hurt. |
| 246 |
And it fixes a real issue that's seen on a few devices.""" |
| 247 |
super().write(' ' + s) |
| 248 |
|
| 249 |
def read_and_decode(self, allowed_types=None): |
| 250 |
v = self.read_bytes(21).decode('utf-8') |
| 251 |
return Racal1992.decode(v, allowed_types) |
| 252 |
|
| 253 |
# ============================================================ |
| 254 |
# Channel-specific settings |
| 255 |
# ============================================================ |
| 256 |
def channel_settings(self, channel_name, **settings): |
| 257 |
"""Set channel configuration paramters. |
| 258 |
|
| 259 |
:param channel_name: 'A' or 'B' |
| 260 |
:param settings: one or multiple of the following: |
| 261 |
|
| 262 |
'coupling' : 'AC' or 'DC' |
| 263 |
'attenuation' : 'X1' or 'X10' |
| 264 |
'trigger' : 'auto' or 'manual' |
| 265 |
'impedance' : '50' or '1M' |
| 266 |
'slope' : 'pos' or 'neg' |
| 267 |
'filtering' : True or False (only allowed for channel A) |
| 268 |
'input_select' : 'separate' or 'common' (only allowed for channel B) |
| 269 |
'trigger_level' : <floating point number> |
| 270 |
|
| 271 |
""" |
| 272 |
if channel_name not in Racal1992.channel_params: |
| 273 |
raise ValueError("Channel name must by 'A' or 'B'") |
| 274 |
|
| 275 |
commands = [] |
| 276 |
trigger_str = "" |
| 277 |
|
| 278 |
for setting, value in settings.items(): |
| 279 |
if setting not in Racal1992.channel_params[channel_name]: |
| 280 |
raise ValueError(f"Channel {channel_name} does not support a {setting} setting") |
| 281 |
|
| 282 |
accepted_values = Racal1992.channel_params[channel_name][setting] |
| 283 |
if accepted_values is None: |
| 284 |
# Trigger level has a float parameter... |
| 285 |
# Use special string for that because it's used the |
| 286 |
# last setting of all. |
| 287 |
if value < -51 or value > 51: |
| 288 |
raise ValueError(f"{value} is out of range for {setting}") |
| 289 |
trigger_str = f"SL{channel_name} {value}" |
| 290 |
continue |
| 291 |
|
| 292 |
if value not in accepted_values: |
| 293 |
raise ValueError(f"{value} is not an acceptable value for {setting}") |
| 294 |
|
| 295 |
command = accepted_values[value] |
| 296 |
commands.append(command) |
| 297 |
|
| 298 |
if trigger_str != "": |
| 299 |
commands.append(trigger_str) |
| 300 |
|
| 301 |
self.write(" ".join(c for c in commands)) |
| 302 |
|
| 303 |
# ============================================================ |
| 304 |
# IP - Instrument Preset |
| 305 |
# ============================================================ |
| 306 |
def preset(self): |
| 307 |
"""Configure instrument with default presets.""" |
| 308 |
self.write('IP') |
| 309 |
|
| 310 |
# ============================================================ |
| 311 |
# RE - Reset measurement |
| 312 |
# ============================================================ |
| 313 |
def reset_measurement(self): |
| 314 |
"""Reset ongoing measurement.""" |
| 315 |
self.write('RE') |
| 316 |
|
| 317 |
# ============================================================ |
| 318 |
# Wait for measurement value |
| 319 |
# ============================================================ |
| 320 |
def wait_for_measurement(self, timeout=None, progressDots=False): |
| 321 |
"""Wait until a new measurement is available. |
| 322 |
|
| 323 |
:param timeout: number of seconds to wait before timeout exception. |
| 324 |
:param progressDots: when true, print '.' after each ready-check |
| 325 |
|
| 326 |
""" |
| 327 |
if timeout is not None: |
| 328 |
end_time = time.time() + timeout |
| 329 |
|
| 330 |
while True: |
| 331 |
if progressDots: |
| 332 |
log.info(".") |
| 333 |
|
| 334 |
stb = self.adapter.connection.read_stb() |
| 335 |
if stb & 0x10: |
| 336 |
break |
| 337 |
|
| 338 |
if timeout is not None and time.time() > end_time: |
| 339 |
raise Exception("Timeout while waiting for measurement") |
| 340 |
|
| 341 |
return stb |
| 342 |
|
| 343 |
# ============================================================ |
| 344 |
# Measured value |
| 345 |
# ============================================================ |
| 346 |
@property |
| 347 |
def measured_value(self): |
| 348 |
"""Get measured value. |
| 349 |
|
| 350 |
A Racal-Dana 1992 doesn't return measurement data after a request for |
| 351 |
measurement data. Instead, it fills a FIFO with data whenever it completes |
| 352 |
a measurement. When the FIFO is full, the oldest measurement is removed. |
| 353 |
|
| 354 |
The FIFO buffer gets cleared when a command is received that requires |
| 355 |
an immediate reply, such reading a setting. It also gets cleared when |
| 356 |
an operating mode is cleared. |
| 357 |
|
| 358 |
When there is no measurement data, this property will stall until data |
| 359 |
is available. It will also timeout after a time that can be set with the standard |
| 360 |
pyvisa API. |
| 361 |
|
| 362 |
One can make sure that measurement data is available by first calling |
| 363 |
`wait_for_measurement()`. |
| 364 |
""" |
| 365 |
return self.read_and_decode(allowed_types=Racal1992.operating_modes.values()) |