Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visa allow setting custom resource args. #5485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/qcodes/instrument/visa.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
``qcodes.instruments.sims:AimTTi_PL601P.yaml`` in which case it is loaded
from the supplied module. Note that it is an error to pass both
``pyvisa_sim_file`` and ``visalib``.
visa_kwargs: Keyword arguments passed to PyVisas `ResourceManager().open_resource`

Check notice on line 67 in src/qcodes/instrument/visa.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/qcodes/instrument/visa.py#L67

Line too long (90/88)
This allows setting custom properties for a Visa connection such as serial
baud rate. See PyVisa docs for additional options.

See help for :class:`.Instrument` for additional information on writing
instrument subclasses.
Expand All @@ -79,6 +82,7 @@
device_clear: bool = True,
visalib: str | None = None,
pyvisa_sim_file: str | None = None,
visa_kwargs: dict[str, Any] | None = None,
**kwargs: Any,
):

Expand All @@ -98,6 +102,8 @@
"It's an error to supply both visalib and pyvisa_sim_file as "
"arguments to a VISA instrument"
)
if visa_kwargs is None:
visa_kwargs = {}
if pyvisa_sim_file is not None:
if ":" in pyvisa_sim_file:
module, pyvisa_sim_file = pyvisa_sim_file.split(":")
Expand All @@ -116,10 +122,12 @@
visa_handle,
visabackend,
resource_manager,
) = self._connect_and_handle_error(address, visalib)
) = self._connect_and_handle_error(
address, visalib, visa_kwargs=visa_kwargs
)
else:
visa_handle, visabackend, resource_manager = self._connect_and_handle_error(
address, visalib
address, visalib, visa_kwargs=visa_kwargs
)
finalize(self, _close_visa_handle, visa_handle, str(self.name))

Expand All @@ -142,11 +150,13 @@
self.timeout.set(timeout)

def _connect_and_handle_error(
self, address: str, visalib: str | None
self, address: str, visalib: str | None, visa_kwargs: dict[str, Any]
) -> tuple[pyvisa.resources.MessageBasedResource, str, pyvisa.ResourceManager]:
try:
visa_handle, visabackend, resource_manager = self._open_resource(
address, visalib
address,
visalib,
visa_kwargs=visa_kwargs,
)
except Exception as e:
self.visa_log.exception(f"Could not connect at {address}")
Expand All @@ -155,7 +165,7 @@
return visa_handle, visabackend, resource_manager

def _open_resource(
self, address: str, visalib: str | None
self, address: str, visalib: str | None, visa_kwargs: dict[str, Any]
) -> tuple[pyvisa.resources.MessageBasedResource, str, pyvisa.ResourceManager]:

# in case we're changing the address - close the old handle first
Expand All @@ -174,7 +184,7 @@
visabackend = "ivi"

self.visa_log.info(f"Opening PyVISA resource at address: {address}")
resource = resource_manager.open_resource(address)
resource = resource_manager.open_resource(address, **visa_kwargs)
if not isinstance(resource, pyvisa.resources.MessageBasedResource):
resource.close()
raise TypeError("QCoDeS only support MessageBasedResource Visa resources")
Expand All @@ -192,7 +202,7 @@
(and then call this function).
"""
resource, visabackend, resource_manager = self._open_resource(
address, self.visalib
address, self.visalib, {}
)
self.visa_handle = resource
self._address = address
Expand Down
Loading