VisaDriver#

class VisaDriver(visa_resource: str | VisaConfig)[source]#

Bases: TransportBase

Transport for VISA-attached instruments. Composed by concrete drivers, not extended.

Supports shared ownership: a device serving several categories holds one connection for all of them, passing each category view as the holder to open()/close(), and it closes only when the last owner closes it. Thread-safe at the I/O level via an internal lock; use lock() to keep a multi-step VISA sequence atomic.

Construct from a VISA resource string (uses defaults) or a full VisaConfig.

Attributes

is_open

Whether the underlying VISA resource is currently open.

Methods

clear

VISA device clear — aborts any pending operation.

close

Close and teardown; with a holder, remove it and tear down only when the last owner leaves.

lock

Return the reentrant resource lock for atomic multi-step sequences; the holder may write/query/read inside it.

open

Open the connection (idempotent); with a holder, admit it and return True only for the first owner.

query

Write command and read the response.

query_binary_values

Send command and decode the IEEE-488.2 definite-length binary block reply.

query_raw

Write command (with terminator) and read raw bytes — reply is not decoded or stripped.

read

Read a response, stripping the configured read terminator.

read_raw

Read raw bytes from the instrument.

temporary_timeout

Hold the lock and override the operation timeout to timeout_ms ms; restored on exit (even if raises).

write

Write command to the instrument; the configured write terminator is appended.

write_raw

Write raw bytes verbatim.

property is_open: bool#

Whether the underlying VISA resource is currently open.

write(command: str) → None[source]#

Write command to the instrument; the configured write terminator is appended.

read() → str[source]#

Read a response, stripping the configured read terminator.

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

Write command and read the response.

write_raw(data: bytes) → None[source]#

Write raw bytes verbatim. Caller owns framing for binary payloads.

read_raw() → bytes[source]#

Read raw bytes from the instrument.

query_raw(command: str) → bytes[source]#

Write command (with terminator) and read raw bytes — reply is not decoded or stripped.

query_binary_values(
command: str,
datatype: str = 'B',
is_big_endian: bool = False,
container: type = list,
) → Any[source]#

Send command and decode the IEEE-488.2 definite-length binary block reply.

Use for waveforms, screenshots, settings dumps.

Parameters:
  • command

    SCPI query that returns a binary block (e.g. "CURV?").

  • datatype

    struct-style format char ("B" u8, "h" i16, "H" u16, "f" f32).

  • is_big_endian

    Byte order of multi-byte elements.

  • container

    Container for decoded values (default list).

clear() → None[source]#

VISA device clear — aborts any pending operation. Use after a timed-out blocking read.

temporary_timeout(timeout_ms: int) → Iterator[None][source]#

Hold the lock and override the operation timeout to timeout_ms ms; restored on exit (even if raises).

close(holder: object | None = None) → None#

Close and teardown; with a holder, remove it and tear down only when the last owner leaves.

A holder that needs to talk to the instrument before the connection goes (releasing a remote lock, say) does that in its own close before calling this, while the session is still up.

Raises UnknownHolderError when an unrecognized holder closes while others still own the connection, because that is the case where ignoring it silently strands the real owners and leaves the connection open forever. Once the last owner has left there is nothing to strand, so a repeat close is a no-op and close stays idempotent.

lock() → RLock#

Return the reentrant resource lock for atomic multi-step sequences; the holder may write/query/read inside it.

open(holder: object | None = None) → bool#

Open the connection (idempotent); with a holder, admit it and return True only for the first owner.