Skip to content

Commit d8939b0

Browse files
authored
Merge pull request #201 from sandialabs/list-resources-feat
List resources feat
2 parents f10c074 + d835b6f commit d8939b0

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

pyscan/general/get_resources.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from pyvisa import ResourceManager, VisaIOError
2+
3+
4+
def get_resources(print_resources=True):
5+
"""
6+
Returns a list of connected VISA GPIB addresses and optionally (by default)
7+
tries to print them paired with their corresponding ID strings.
8+
9+
This function uses the PyVISA library to return a list of all resources (instruments) available to the system.
10+
It tries to connect to each instrument and prints the error message if unable to.
11+
If `list_resources` is set to True, it attempts to query and print the IDs of each instrument.
12+
13+
Parameters
14+
----------
15+
list_resources : bool
16+
If True (default), queries and prints instrument connection addresses paired with their ID strings if available.
17+
If False, only returns the available resources without querying their IDs.
18+
19+
Returns
20+
-------
21+
resources_listed : tuple
22+
A list of resource connections (strings) representing the instruments detected by the system.
23+
"""
24+
rm = ResourceManager()
25+
resources_listed = rm.list_resources()
26+
resource_dict = {}
27+
28+
for r in resources_listed:
29+
try:
30+
# Connect to the instrument
31+
res = rm.open_resource(r)
32+
except VisaIOError as e:
33+
print(f"{e}\n Could not connect to instrument {r}, may already be connected elsewhere.")
34+
continue
35+
36+
try:
37+
# Try to query the I.D. of the instrument
38+
name = res.query('*IDN?')
39+
resource_dict[r] = name
40+
41+
# If found and print resources is true, print the resource as well as the I.D.
42+
if print_resources is True:
43+
print(r, name)
44+
except VisaIOError:
45+
resource_dict[r] = "Instrument I.D. could not be queried."
46+
if print_resources is True:
47+
print(r, "Instrument I.D. could not be queried.")
48+
49+
# We are no longer closing the connection by defualt since this could close connections that were open before
50+
# and there is no way of telling if a connection was already open automatically.
51+
# res.close()
52+
53+
return resource_dict

0 commit comments

Comments
 (0)