-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruments.py
More file actions
57 lines (45 loc) · 1.67 KB
/
instruments.py
File metadata and controls
57 lines (45 loc) · 1.67 KB
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
# Imports
import sys
import pyvisa
import pyvisa.constants
class Instruments():
def __init__(self):
self.rm = pyvisa.ResourceManager() # Open a VISA resource manager
print("Using resource manager:", self.rm)
self.instruments = self._find()
def _find(self) -> dict:
resources = self.rm.list_resources()
found_devices = {}
for instrument in resources:
device = self.rm.open_resource(instrument)
name = device.get_visa_attribute(
pyvisa.constants.ResourceAttribute.model_name)
device.close()
found_devices[name] = instrument
print("Devices found:", len(found_devices))
return found_devices
def open(self, model_name: str):
device = self.instruments.get(model_name)
try:
resource = self.rm.open_resource(device)
except pyvisa.constants.VI_ERROR_INV_RSRC_NAME:
print("ERROR: Could not open instrument", model_name, file=sys.stderr)
return
else:
print("Success! Opened:", device)
return resource
def close(self, resource):
try:
resource.close()
except pyvisa.constants.VI_ERROR_CLOSING_FAILED:
print("ERROR: Could not close instrument", resource, file=sys.stderr)
else:
print("Closed:", resource)
def close_all(self):
if not self.open_resources:
print("No devices to close")
return
for resource in self.rm.list_opened_resources:
self.close(resource)
if self.rm.list_opened_resources:
print("Devices still open:", self.open_resources)