hvl_ccb.comm.visa

Inheritance diagram of hvl_ccb.comm.visa

Communication protocol for VISA. Makes use of the pyvisa library. The backend can be NI-Visa or pyvisa-py.

Information on how to install a VISA backend can be found here: https://pyvisa.readthedocs.io/en/master/getting_nivisa.html

So far only TCPIP SOCKET and TCPIP INSTR interfaces are supported.

class VisaCommunication(configuration)[source]

Bases: SyncCommunicationProtocol

Implements the Communication Protocol for VISA / SCPI.

MULTI_COMMANDS_MAX = 5

The maximum of commands that can be sent in one round is 5 according to the VISA standard.

MULTI_COMMANDS_SEPARATOR = ';'

The character to separate two commands is ; according to the VISA standard.

WAIT_AFTER_WRITE = 0.08

Small pause in seconds to wait after write operations, allowing devices to really do what we tell them before continuing with further tasks.

close() None[source]

Close the VISA connection and invalidates the handle.

static config_cls() type[VisaCommunicationConfig][source]

Return the default configdataclass class.

Returns:

a reference to the default configdataclass class

open() None[source]

Open the VISA connection and create the resource.

query(**kwargs)[source]

Send a command to the interface and handle the status message. Possibly raises an exception.

Parameters:
  • command – Command to send

  • n_attempts_max – Amount of attempts how often a non-empty text is tried to be read as answer

  • attempt_interval_sec – time between the reading attempts

Returns:

Answer from the interface, which can be None instead of an empty reply

query_multiple(**kwargs)[source]
read(**kwargs)[source]

Read a single line of text as str from the communication.

Returns:

text as str including the terminator, which can also be empty “”

read_all(_n_attempts_max: int | None = None, _attempt_interval_sec: int | float | None = None) str | None[source]

Read all lines of text from the connection till nothing is left to read.

Parameters:
  • n_attempts_max – Amount of attempts how often a non-empty text is tried to be read

  • attempt_interval_sec – time between the reading attempts

Returns:

A multi-line str including the terminator internally

read_bytes(**kwargs)[source]

Read a single line as bytes from the communication.

This method uses self.access_lock to ensure thread-safety.

Returns:

a single line as bytes containing the terminator, which can also be empty b””

spoll(**kwargs)[source]
write(**kwargs)[source]

Write text as str to the communication.

Parameters:

text – test as a str to be written

write_bytes(**kwargs)[source]

Write data as bytes to the communication.

This method uses self.access_lock to ensure thread-safety.

Parameters:

data – data as bytes-string to be written

Returns:

number of bytes written

write_multiple(**kwargs)[source]
class VisaCommunicationConfig(terminator: bytes = b'\n', encoding: str = 'utf-8', encoding_error_handling: str = 'strict', wait_sec_read_text_nonempty: int | float = 0.5, default_n_attempts_read_text_nonempty: int = 10, host: str | IPv4Address | IPv6Address = '', interface_type: str | InterfaceType = '', board: int = 0, port: int = 5025, timeout: int = 5000, chunk_size: int = 204800, open_timeout: int = 1000, visa_backend: str = '')[source]

Bases: SyncCommunicationProtocolConfig

VisaCommunication configuration dataclass.

class InterfaceType(value=<no_arg>, names=None, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: AutoNumberNameEnum

Supported VISA Interface types.

TCPIP_INSTR = 2

VXI-11 protocol

TCPIP_SOCKET = 1

VISA-RAW protocol

address(host: str, port: int | None = None, board: int | None = None) str[source]

Address string specific to the VISA interface type.

Parameters:
  • host – host IP address

  • port – optional TCP port

  • board – optional board number

Returns:

address string

property address: str

Address string depending on the VISA protocol’s configuration.

Returns:

address string corresponding to current configuration

board: int = 0

Board number is typically 0 and comes from old bus systems.

chunk_size: int = 204800

Chunk size is the allocated memory for read operations. The standard is 20kB, and is increased per default here to 200kB. It is specified in bytes.

clean_values() None[source]
force_value(fieldname, value)

Forces a value to a dataclass field despite the class being frozen.

NOTE: you can define post_force_value method with same signature as this method to do extra processing after value has been forced on fieldname.

Parameters:
  • fieldname – name of the field

  • value – value to assign

host: str | IPv4Address | IPv6Address = ''
interface_type: str | InterfaceType = ''

Interface type of the VISA connection, being one of InterfaceType.

classmethod keys() Sequence[str]

Returns a list of all configdataclass fields key-names.

Returns:

a list of strings containing all keys.

open_timeout: int = 1000

Timeout for opening the connection, in milli seconds.

classmethod optional_defaults() dict[str, object]

Returns a list of all configdataclass fields, that have a default value assigned and may be optionally specified on instantiation.

Returns:

a list of strings containing all optional keys.

port: int = 5025

TCP port, standard is 5025.

classmethod required_keys() Sequence[str]

Returns a list of all configdataclass fields, that have no default value assigned and need to be specified on instantiation.

Returns:

a list of strings containing all required keys.

terminator: bytes = b'\n'

The terminator will be used to set write_termination and read_termination

timeout: int = 5000

Timeout for commands in milli seconds.

visa_backend: str = ''

Specifies the path to the library to be used with PyVISA as a backend. Defaults to None, which is NI-VISA (if installed), or pyvisa-py (if NI-VISA is not found). To force the use of pyvisa-py, specify @py’ here.

exception VisaCommunicationError[source]

Bases: OSError, CommunicationError

Base class for VisaCommunication errors.

has_instrument(func: Callable) Callable[source]

Decorator to check if self._instrument is not None.