How to use the pyvisa.util function in PyVISA

To help you get started, we’ve selected a few PyVISA examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyvisa / pyvisa / pyvisa / testsuite / test_util.py View on Github external
def test_ieee_integer(self):
        values = list(range(99))
        containers = (list, tuple) #+ ((np.asarray,) if np else ())
        for fmt in 'bBhHiIfd':
            for endi in (True, False):
                for cont in containers:
                    conv = cont(values)
                    msg = 'fmt=%s, endianness=%s, container=%s' % (fmt, endi, cont.__name__)
                    try:
                        block = util.to_ieee_block(conv, fmt, endi)
                        parsed = util.from_ieee_block(block, fmt, endi, cont)
                    except Exception as e:
                        raise Exception(msg + '\n' + repr(e))

                    self.assertEqual(conv, parsed, msg)
github pyvisa / pyvisa / pyvisa / resources / messagebased.py View on Github external
:param converter: function used to convert each element.
                          Defaults to float
        :type converter: callable
        :param separator: a callable that split the str into individual
                          elements. If a str is given, data.split(separator) is
                          used.
        :type: separator: (str) -> collections.Iterable[int] | str
        :param container: container type to use for the output data.
        :returns: the answer from the device.
        :rtype: list

        """
        # Use read rather than _read_raw because we cannot handle a bytearray
        block = self.read()

        return util.from_ascii_block(block, converter, separator, container)
github pyvisa / pyvisa / pyvisa / errors.py View on Github external
def from_wrong_arch(cls, filename):
        s = ''
        details = util.get_system_details(backends=False)
        visalib = util.LibraryPath(filename, 'user' if filename == util.read_user_library_path() else 'auto')
        s += 'No matching architecture.\n'
        s += '    Current Python interpreter is %s bits\n' % details['bits']
        s += '    The library in: %s\n' % visalib.path
        s += '      found by: %s\n' % visalib.found_by
        s += '      bitness: %s\n' % visalib.bitness

        return cls('Error while accessing %s: %s' % (filename, s))
github pyvisa / pyvisa / pyvisa / errors.py View on Github external
def from_wrong_arch(cls, filename):
        s = ''
        details = util.get_system_details(backends=False)
        visalib = util.LibraryPath(filename, 'user' if filename == util.read_user_library_path() else 'auto')
        s += 'No matching architecture.\n'
        s += '    Current Python interpreter is %s bits\n' % details['bits']
        s += '    The library in: %s\n' % visalib.path
        s += '      found by: %s\n' % visalib.found_by
        s += '      bitness: %s\n' % visalib.bitness

        return cls('Error while accessing %s: %s' % (filename, s))
github BBN-Q / Auspex / src / auspex / instruments / agilent.py View on Github external
def _raw_query(self, string, size=16):
        """Some Agilent VNAs are stupid and do not understand VISA query commands for large binary transfers.
            Hack around this. The raw read size seems to be safe with 16 bytes per read but this might need to be changed?
        """
        self.interface.write(string)
        block =  self.interface.read_raw(size=size)
        offset, data_length = util.parse_ieee_block_header(block)
        return util.from_ieee_block(block, 'f', True, np.array)
github pyvisa / pyvisa / pyvisa / resources / messagebased.py View on Github external
if fmt & 0x01 == 0:  # ascii
            return util.from_ascii_block(self.read())

        data = self._read_raw()

        try:
            if fmt & 0x03 == 1:  # single
                is_single = True
            elif fmt & 0x03 == 3:  # double:
                is_single = False
            else:
                raise ValueError("unknown data values fmt requested")

            is_big_endian = fmt & 0x04  # big endian
            return util.parse_binary(data, is_big_endian, is_single)
        except ValueError as e:
            raise errors.InvalidBinaryFormat(e.args)
github pyvisa / pyvisa / pyvisa / resources / messagebased.py View on Github external
:type message: unicode (Py2) or str (Py3)
        :param values: data to be writen to the device.
        :param datatype: the format string for a single element. See struct
                         module.
        :param is_big_endian: boolean indicating endianess.
        :return: number of bytes written.
        :rtype: int
        """
        term = self._write_termination if termination is None else termination
        enco = self._encoding if encoding is None else encoding

        if term and message.endswith(term):
                warnings.warn("write message already ends with "
                              "termination characters", stacklevel=2)

        block = util.to_ieee_block(values, datatype, is_big_endian)

        message = message.encode(enco) + block

        if term:
            message += term.encode(enco)

        count = self.write_raw(message)

        return count
github pyvisa / pyvisa / pyvisa / resources / messagebased.py View on Github external
:type converter: callable | str
        :param separator: a callable that join the values in a single str.
                          If a str is given, separator.join(values) is used.
        :type: separator: (collections.Iterable[T]) -> str | str
        :return: number of bytes written.
        :rtype: int
        """

        term = self._write_termination if termination is None else termination
        enco = self._encoding if encoding is None else encoding

        if term and message.endswith(term):
                warnings.warn("write message already ends with "
                              "termination characters", stacklevel=2)

        block = util.to_ascii_block(values, converter, separator)

        message = message.encode(enco) + block.encode(enco)

        if term:
            message += term.encode(enco)

        count = self.write_raw(message)

        return count
github pyvisa / pyvisa / pyvisa / resources / messagebased.py View on Github external
data.
        :returns: the answer from the device.
        :rtype: type(container)

        """
        block = self._read_raw(chunk_size)

        if header_fmt == 'ieee':
            offset, data_length = util.parse_ieee_block_header(block)

            # Allow to support instrument such as the Keithley 2000 that do not
            # report the length of the block
            data_length = data_length or data_points*struct.calcsize(datatype)

        elif header_fmt == 'hp':
            offset, data_length = util.parse_hp_block_header(block,
                                                             is_big_endian)
        elif header == 'empty':
            offset = 0
        else:
            raise ValueError("Invalid header format. Valid options are 'ieee',"
                             " 'empty', 'hp'")

        expected_length = offset + data_length

        if expect_termination and self._read_termination is not None:
            expected_length += len(self._read_termination)

        # Read all the data if we know what to expect.
        if data_length != 0:
            block.extend(self.read_bytes(expected_length - len(block),
                                         chunk_size=chunk_size))