qtics.transport package#

Transports: channels used to exchange messages with instruments.

Submodules#

qtics.transport.base module#

Base transport: the channel used to exchange messages with an instrument.

class qtics.transport.base.Transport(address: str, timeout: float = 10, terminator: str = '\n', delay: float = 0.1)[source]#

Bases: ABC

Message channel to an instrument.

Opening the channel is deferred to connect(), creating a transport never touches the hardware.

abstract close()[source]#

Close the channel.

abstract connect()[source]#

Open the channel.

abstract property is_connected: bool#

Whether the channel is open.

query(cmd: str) → str[source]#

Send a command, wait delay and read the answer.

abstract read() → str[source]#

Read an answer, without the terminator.

abstract read_raw(n_bytes: int) → bytes[source]#

Read exactly n_bytes bytes.

write(cmd: str)[source]#

Send a command, appending the terminator.

qtics.transport.fake module#

Fake transport for tests: records commands and replies with scripted answers.

class qtics.transport.fake.FakeTransport(answers: Dict[str, str | bytes | Callable[[str], str | bytes]] | None = None, default: str | bytes | Callable[[str], str | bytes] | None = None, address: str = 'fake', terminator: str = '\n')[source]#

Bases: Transport

In-memory transport.

Every written command is appended to written. If the command is a key of answers its answer is queued for reading: strings are returned by read(), bytes by read_raw(), callables are called with the command. Queries (commands containing “?”) without a scripted answer get default (if not None); other commands get no answer.

close()[source]#

Close the fake channel.

connect()[source]#

Open the fake channel.

property is_connected: bool#

Whether the fake channel is open.

read() → str[source]#

Return the oldest pending answer, empty string if none.

read_raw(n_bytes: int) → bytes[source]#

Return the next n_bytes of queued raw data.

qtics.transport.gpib module#

GPIB (IEEE-488) transport.

Calls the IEEE 488.2 C library of the GPIB driver through ctypes: linux-gpib (libgpib.so) on Linux, NI-488.2 (ni4882.dll) on Windows. Only the driver is needed, no Python package.

class qtics.transport.gpib.GPIBTransport(address: int | str, board: int = 0, timeout: float = 10, terminator: str = '\n', delay: float = 0.1, library: str = '')[source]#

Bases: Transport

GPIB transport.

address is the primary address (e.g. 20, combined with board) or a VISA-like resource string (e.g. “GPIB0::20::INSTR”). Reads end when the instrument asserts EOI, as IEEE 488.2 requires, so binary data is never cut at the terminator.

clear()[source]#

Send a GPIB device clear, which empties the instrument buffers.

close()[source]#

Close the device descriptor.

connect()[source]#

Open the device descriptor.

property is_connected: bool#

Whether the device descriptor is open.

property lib: Any#

The loaded driver library.

read() → str[source]#

Read until the end of message (EOI), strip the terminator.

read_raw(n_bytes: int) → bytes[source]#

Read n_bytes bytes, or fewer if the message ends before.

property timeout: float#

Read timeout in seconds, also applied to an open session.

qtics.transport.gpib.load_library(path: str = '') → Any[source]#

Load the GPIB driver library, found automatically if path is empty.

qtics.transport.gpib.timeout_code(seconds: float) → int[source]#

Return the ibtmo code of the shortest timeout of at least seconds.

qtics.transport.serial module#

Serial port transport.

class qtics.transport.serial.SerialTransport(address: str, baudrate: int = 9600, bytesize: int = 8, parity: Literal['N', 'E', 'O', 'M', 'S'] = 'N', stopbits: float = 1, timeout: float = 10, terminator: str = '\n', delay: float = 0.1, flush_before_write: bool = True)[source]#

Bases: Transport

Serial port transport.

Answers are read from what is waiting in the input buffer, so a query relies on delay to give the instrument time to answer.

close()[source]#

Close the port.

connect()[source]#

Open the port.

property is_connected: bool#

Whether the port is open.

read() → str[source]#

Read what is waiting in the input buffer.

read_raw(n_bytes: int) → bytes[source]#

Read exactly n_bytes bytes, or fewer on timeout.

qtics.transport.socket module#

TCP socket transport, e.g. for raw SCPI over LAN.

The code was partially taken from https://github.com/morgan-at-keysight/socketscpi

class qtics.transport.socket.SocketTransport(address: str, port: int = 5025, timeout: float = 10, terminator: str = '\n', delay: float = 0.1, no_delay: bool = True)[source]#

Bases: Transport

Raw TCP socket transport.

close()[source]#

Close the socket.

connect()[source]#

Open the socket.

property is_connected: bool#

Whether the socket is open.

read() → str[source]#

Read until the terminator.

read_raw(n_bytes: int) → bytes[source]#

Read exactly n_bytes bytes.