-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrument.py
63 lines (50 loc) · 1.79 KB
/
instrument.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import time
import pyvisa
class RequestHandler:
"""A wrapper around pyvisa that limits the rate of requests.
Parameters
----------
resource_name (str)
The name of the resource.
interval_ms (int)
The interval in milliseconds between requests.
**kwargs
Additional keyword arguments to be passed to the resource.
"""
def __init__(self, resource_name: str, interval_ms: int = 50, **kwargs):
self.resource_name = resource_name
self.interval_ms = interval_ms
self._resource_kwargs = kwargs
def open(self):
"""Open the pyvisa resource."""
self.inst = pyvisa.ResourceManager().open_resource(
self.resource_name, **self._resource_kwargs
)
self._last_update = time.perf_counter_ns()
def wait_time(self):
"""Wait until the interval between requests has passed."""
if self.interval_ms == 0:
return
while (time.perf_counter_ns() - self._last_update) <= self.interval_ms * 1e3:
time.sleep(1e-4)
def write(self, *args, **kwargs):
self.wait_time()
res = self.inst.write(*args, **kwargs)
self._last_update = time.perf_counter_ns()
return res
def query(self, *args, **kwargs):
self.wait_time()
res = self.inst.query(*args, **kwargs)
self._last_update = time.perf_counter_ns()
return res
def read(self, *args, **kwargs):
"""Read data from the resource.
This is not very likely to be used. It may cause problems due to the wait time.
Use `query` instead.
"""
self.wait_time()
res = self.inst.read(*args, **kwargs)
self._last_update = time.perf_counter_ns()
return res
def close(self):
self.inst.close()