| 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 time import sleep, time |
| 27 |
|
| 28 |
import numpy as np |
| 29 |
|
| 30 |
from pymeasure.instruments import Instrument |
| 31 |
from pymeasure.instruments.validators import truncated_range |
| 32 |
from pymeasure.adapters import PrologixAdapter |
| 33 |
|
| 34 |
log = logging.getLogger(__name__) |
| 35 |
log.addHandler(logging.NullHandler()) |
| 36 |
|
| 37 |
|
| 38 |
class KeithleyBuffer: |
| 39 |
""" Implements the basic buffering capability found in |
| 40 |
many Keithley instruments. """ |
| 41 |
|
| 42 |
buffer_points = Instrument.control( |
| 43 |
":TRAC:POIN?", ":TRAC:POIN %d", |
| 44 |
""" Control the number of buffer points. This does not represent actual points |
| 45 |
in the buffer, but the configuration value instead. """, |
| 46 |
validator=truncated_range, |
| 47 |
values=[2, 1024], |
| 48 |
cast=int |
| 49 |
) |
| 50 |
|
| 51 |
def config_buffer(self, points=64, delay=0): |
| 52 |
""" Configures the measurement buffer for a number of points, to be |
| 53 |
taken with a specified delay. |
| 54 |
|
| 55 |
:param points: The number of points in the buffer. |
| 56 |
:param delay: The delay time in seconds. |
| 57 |
""" |
| 58 |
# Enable measurement status bit |
| 59 |
# Enable buffer full measurement bit |
| 60 |
self.write(":STAT:PRES;*CLS;*SRE 1;:STAT:MEAS:ENAB 512;") |
| 61 |
self.write(":TRAC:CLEAR;") |
| 62 |
self.buffer_points = points |
| 63 |
self.trigger_count = points |
| 64 |
self.trigger_delay = delay |
| 65 |
self.write(":TRAC:FEED SENSE;:TRAC:FEED:CONT NEXT;") |
| 66 |
self.check_errors() |
| 67 |
|
| 68 |
def is_buffer_full(self): |
| 69 |
""" Returns True if the buffer is full of measurements. """ |
| 70 |
status_byte = int(self.ask("*STB?")) |
| 71 |
return (status_byte & 65) == 65 |
| 72 |
|
| 73 |
def wait_for_buffer(self, should_stop=lambda: False, |
| 74 |
timeout=60, interval=0.1): |
| 75 |
""" Blocks the program, waiting for a full buffer. This function |
| 76 |
returns early if the :code:`should_stop` function returns True or |
| 77 |
the timeout is reached before the buffer is full. |
| 78 |
|
| 79 |
:param should_stop: A function that returns True when this function should return early |
| 80 |
:param timeout: A time in seconds after which this function should return early |
| 81 |
:param interval: A time in seconds for how often to check if the buffer is full |
| 82 |
""" |
| 83 |
# TODO: Use SRQ initially instead of constant polling |
| 84 |
# self.adapter.wait_for_srq() |
| 85 |
t = time() |
| 86 |
while not self.is_buffer_full(): |
| 87 |
sleep(interval) |
| 88 |
if should_stop(): |
| 89 |
return |
| 90 |
if (time() - t) > timeout: |
| 91 |
raise Exception("Timed out waiting for Keithley buffer to fill.") |
| 92 |
|
| 93 |
@property |
| 94 |
def buffer_data(self): |
| 95 |
""" Get a numpy array of values from the buffer. """ |
| 96 |
self.write(":FORM:DATA ASCII") |
| 97 |
return np.array(self.values(":TRAC:DATA?"), dtype=np.float64) |
| 98 |
|
| 99 |
def start_buffer(self): |
| 100 |
""" Starts the buffer. """ |
| 101 |
self.write(":INIT") |
| 102 |
|
| 103 |
def reset_buffer(self): |
| 104 |
""" Resets the buffer. """ |
| 105 |
self.write(":STAT:PRES;*CLS;:TRAC:CLEAR;:TRAC:FEED:CONT NEXT;") |
| 106 |
|
| 107 |
def stop_buffer(self): |
| 108 |
""" Aborts the buffering measurement, by stopping the measurement |
| 109 |
arming and triggering sequence. If possible, a Selected Device |
| 110 |
Clear (SDC) is used. """ |
| 111 |
if type(self.adapter) is PrologixAdapter: |
| 112 |
self.write("++clr") |
| 113 |
else: |
| 114 |
self.write(":ABOR") |
| 115 |
|
| 116 |
def disable_buffer(self): |
| 117 |
""" Disables the connection between measurements and the |
| 118 |
buffer, but does not abort the measurement process. |
| 119 |
""" |
| 120 |
self.write(":TRAC:FEED:CONT NEV") |