Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def convert_gpib_status(status):
if status & 0x4000:
return constants.StatusCode.error_timeout
elif status & 0x8000:
return constants.StatusCode.error_system_error
else:
return constants.StatusCode.success
Corresponds to viRead function of the VISA library.
:param session: Unique logical identifier to a session.
:param count: Number of bytes to be read.
:return: data read, return value of the library call.
:rtype: bytes, :class:`pyvisa.constants.StatusCode`
"""
try:
sess = self.sessions[session]
except KeyError:
return b'', constants.StatusCode.error_invalid_object
try:
chunk, status = sess.read(count)
if status == constants.StatusCode.error_timeout:
raise errors.VisaIOError(constants.VI_ERROR_TMO)
return chunk, status
except AttributeError:
return b'', constants.StatusCode.error_nonsupported_operation
def convert_gpib_error(error, status, operation):
"""Convert a GPIB error to a VISA StatusCode.
:param error: Error to use to determine the proper status code.
:type error: gpib.GpibError
:param status: Status byte of the GPIB library.
:type status: int
:param operation: Name of the operation that caused an exception. Used in logging.
:type operation: str
:return: Status code matching the GPIB error.
:rtype: constants.StatusCode
"""
# 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
if not last:
time.sleep(.01)
now = time.time()
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
def wait_on_event(self, in_event_type, timeout, capture_timeout=False):
"""Waits for an occurrence of the specified event in this resource.
:param in_event_type: Logical identifier of the event(s) to wait for.
:param timeout: Absolute time period in time units that the resource shall wait for a specified event to
occur before returning the time elapsed error. The time unit is in milliseconds.
None means waiting forever if necessary.
:param capture_timeout: When True will not produce a VisaIOError(VI_ERROR_TMO) but
instead return a WaitResponse with timed_out=True
:return: A WaitResponse object that contains event_type, context and ret value.
"""
try:
event_type, context, ret = self.visalib.wait_on_event(self.session, in_event_type, timeout)
except errors.VisaIOError as exc:
if capture_timeout and exc.error_code == constants.StatusCode.error_timeout:
return WaitResponse(in_event_type, None, exc.error_code, self.visalib, timed_out=True)
raise
return WaitResponse(event_type, context, ret, self.visalib)
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