Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
# First check the imeout condition in the status byte
if status & 0x4000:
return constants.StatusCode.error_timeout
# All other cases are hard errors.
# In particular linux-gpib simply gives a string we could parse but that
# feels brittle. As a consequence we only try to be smart when using
# gpib-ctypes. However in both cases we log the exception at debug level.
else:
logger.debug("Failed to %s.", exc_info=error)
if not GPIB_CTYPES:
return constants.StatusCode.error_system_error
if error.code == 1:
return constants.StatusCode.error_not_cic
elif error.code == 2:
return constants.StatusCode.error_no_listeners
elif error.code == 4:
return constants.StatusCode.error_invalid_mode
elif error.code == 11:
return constants.StatusCode.error_nonsupported_operation
elif error.code == 1:
return constants.StatusCode.error_not_cic
elif error.code == 21:
return constants.StatusCode.error_resource_locked
else:
return constants.StatusCode.error_system_error
value_mask = 0x200
elif line == constants.VI_ATTR_GPIB_SRQ_STATE:
# SRQ bit valid = 0x20, SRQ bit value = 0x2000
validity_mask = 0x20
value_mask = 0x2000
if not value & validity_mask:
return constants.VI_STATE_UNKNOWN, StatusCode.success
else:
if value & value_mask:
return constants.VI_STATE_ASSERTED, StatusCode.success
else:
return constants.VI_STATE_UNASSERTED, StatusCode.success
StatusCode = constants.StatusCode
# linux-gpib timeout constants, in seconds. See GPIBSession._set_timeout.
TIMETABLE = (
0,
10e-6,
30e-6,
100e-6,
300e-6,
1e-3,
3e-3,
10e-3,
30e-3,
100e-3,
300e-3,
1.0,
3.0,
:param session: Resource Manager session (should always be the Default Resource Manager for VISA
returned from open_default_resource_manager()).
:param resource_name: Unique symbolic name of a resource.
:return: Resource information, return value of the library call.
:rtype: :class:`pyvisa.highlevel.ResourceInfo`, :class:`pyvisa.constants.StatusCode`
"""
try:
parsed = rname.parse_resource_name(resource_name)
return (ResourceInfo(parsed.interface_type_const,
parsed.board,
parsed.resource_class,
str(parsed), None),
constants.StatusCode.success)
except ValueError:
return 0, constants.StatusCode.error_invalid_resource_name
time.sleep(.01)
continue
out += last
if end_in == constants.SerialTermination.termination_char:
if out[-1:] == end_char:
return out, constants.StatusCode.success_termination_character_read
elif end_in == constants.SerialTermination.last_bit:
if common.last_int(out) & mask:
return out, constants.StatusCode.success
if enabled and out[-1:] == end_char:
return out, constants.StatusCode.success_termination_character_read
elif end_in == constants.SerialTermination.none:
if out[-1:] == end_char:
return out, constants.StatusCode.success_termination_character_read
else:
raise ValueError('Unknown value for VI_ATTR_ASRL_END_IN')
if len(out) == count:
return out, constants.StatusCode.success_max_count_read
else:
return out, constants.StatusCode.error_timeout
raise ValueError('open_timeout (%r) must be an integer (or compatible type)' % open_timeout)
try:
parsed = rname.parse_resource_name(resource_name)
except rname.InvalidResourceName:
return 0, constants.StatusCode.error_invalid_resource_name
# Loops through all session types, tries to parse the resource name and if ok, open it.
cls = sessions.Session.get_session_class(parsed.interface_type_const, parsed.resource_class)
sess = cls(session, resource_name, parsed)
try:
sess.device = self.devices[sess.attrs[constants.VI_ATTR_RSRC_NAME]]
except KeyError:
return 0, constants.StatusCode.error_resource_not_found
return self._register(sess), constants.StatusCode.success
session : Union[VISASession, VISAEventContext, VISARMSession]
Unique logical identifier to a session, event, resource manager.
Returns
-------
StatusCode
Return value of the library call.
"""
try:
sess = self.sessions[session]
# The RM session directly references the library.
if sess is not self:
return self.handle_return_value(session, sess.close())
else:
return self.handle_return_value(session, StatusCode.success)
except KeyError:
return self.handle_return_value(session, StatusCode.error_invalid_object)
# Check if reading the attribute is allowed.
if not attr.read:
raise Exception("Do not now how to handle write only attributes.")
# First try to answer those attributes that are registered in
# self.attrs, see Session.after_parsing
if attribute in self.attrs:
value = self.attrs[attribute]
status = StatusCode.success
if isinstance(value, tuple):
getter = value[0]
value, status = (
getter(attribute)
if getter
else (0, StatusCode.error_nonsupported_attribute)
)
return value, status
# Dispatch to `_get_attribute`, which must be implemented by subclasses
try:
return self._get_attribute(attribute)
except UnknownAttribute as e:
logger.exception(str(e))
return 0, StatusCode.error_nonsupported_attribute
it. For example, VXI|GPIB means (VXI)|(GPIB), not VX(I|G)PIB.
(exp) Grouping characters or expressions.
Thus the default query, '?*::INSTR', matches any sequences of characters ending
ending with '::INSTR'.
:param query: a VISA Resource Regular Expression used to match devices.
"""
resources = []
try:
find_list, return_counter, instrument_description, err = self.find_resources(session, query)
except errors.VisaIOError as e:
if e.error_code == constants.StatusCode.error_resource_not_found:
return tuple()
raise e
resources.append(instrument_description)
for i in range(return_counter - 1):
resources.append(self.find_next(find_list)[0])
self.close(find_list)
return tuple(resource for resource in resources)
start = time.time()
out = b''
while time.time() - start <= timeout:
last = self.device.read()
if not last:
time.sleep(.01)
continue
out += last
if enabled:
if len(out) > 0 and out[-1] == end_char:
return out, constants.StatusCode.success_termination_character_read
if len(out) == count:
return out, constants.StatusCode.success_max_count_read
else:
return out, constants.StatusCode.error_timeout
try:
_ = usb.core.find()
except Exception as e:
msg = (
"PyUSB does not seem to be properly installed.\n"
"Please refer to PyUSB documentation and \n"
"install a suitable backend like \n"
"libusb 0.1, libusb 1.0, libusbx, \n"
"libusb-win32 or OpenUSB.\n%s" % e
)
Session.register_unavailable(constants.InterfaceType.usb, "INSTR", msg)
Session.register_unavailable(constants.InterfaceType.usb, "RAW", msg)
raise
StatusCode = constants.StatusCode
class USBTimeoutException(Exception):
"""Exception used internally to indicate USB timeout."""
pass
class USBSession(Session):
"""Base class for drivers that communicate with usb devices
via usb port using pyUSB
"""
@staticmethod
def list_resources():
"""Return list of resources for this type of USB device"""