Skip to content

Commit

Permalink
Added optional resource_pyclass to open_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
hgrecco committed Jul 27, 2015
1 parent df888a3 commit def2d0a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ You do not create this objects directly but they are returned by the
`open_resource` method of a `ResourceManager`. In general terms, there
are two main groups derived from `Resource`: `MessageBased` and `RegisterBased`.

.. note:: The resource Python class to use is selected automatically from the
resource name. However, you can force

>>> from pyvisa.resources import MessageBasedResource
>>> inst = rm.open('ASRL1::INSTR', resource_pyclass=MessageBasedResource)


The following sections explore the most common attributes of `Resource` and
`MessageBased` (Serial, GPIB, etc) which are the ones you will encounte more
often. For more information, refer to the :ref:`api`.
Expand Down
20 changes: 13 additions & 7 deletions pyvisa/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,26 +1602,32 @@ def open_bare_resource(self, resource_name,

def open_resource(self, resource_name,
access_mode=constants.AccessModes.no_lock,
open_timeout=constants.VI_TMO_IMMEDIATE, **kwargs):
open_timeout=constants.VI_TMO_IMMEDIATE,
resource_pyclass=None,
**kwargs):
"""Return an instrument for the resource name.
:param resource_name: name or alias of the resource to open.
:param access_mode: access mode.
:type access_mode: :class:`pyvisa.constants.AccessModes`
:param open_timeout: time out to open.
:param resource_pyclass: resource python class to use to instantiate the Resource.
Defaults to None: select based on the resource name.
:param kwargs: keyword arguments to be used to change instrument attributes
after construction.
:rtype: :class:`pyvisa.resources.Resource`
"""
info = self.resource_info(resource_name, extended=False)

try:
cls = self._resource_classes[(info.interface_type, info.resource_class)]
except KeyError:
raise ValueError('There is no class defined for %r' % ((info.interface_type, info.resource_class),))
if resource_pyclass is None:
info = self.resource_info(resource_name, extended=False)

try:
resource_pyclass = self._resource_classes[(info.interface_type, info.resource_class)]
except KeyError:
raise ValueError('There is no class defined for %r' % ((info.interface_type, info.resource_class),))

res = cls(self, resource_name)
res = resource_pyclass(self, resource_name)
for key in kwargs.keys():
try:
getattr(res, key)
Expand Down

0 comments on commit def2d0a

Please sign in to comment.