stoner_measurement.instruments.protocol.base.BaseProtocol

class stoner_measurement.instruments.protocol.base.BaseProtocol[source]

Bases: ABC

Abstract base for all instrument communication protocols.

A protocol defines the command syntax and response parsing rules for a particular family of instruments. Concrete subclasses implement the format_command(), format_query(), and parse_response() methods according to the protocol conventions.

Two complementary error-detection strategies are supported:

Response-embedded errors (Oxford Instruments, Lakeshore):

The instrument encodes an error indicator directly inside its normal response string (e.g. a leading "?"). errors_in_response returns True for these protocols and check_error() should be called on every parsed response.

Error-queue errors (SCPI):

The instrument maintains an internal error queue that must be polled separately, typically via a SYST:ERR? query. error_query returns the query string and check_error() is called on the result of that query rather than on normal responses.

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> p = ScpiProtocol()
>>> p.format_command("VOLT 1.0")
b'VOLT 1.0\n'
>>> p.format_query("VOLT?")
b'VOLT?\n'
>>> p.parse_response(b' +1.234E+00\n')
'+1.234E+00'
>>> p.errors_in_response
False
>>> p.error_query
'SYST:ERR?'
check_error(response: str, *, command: str | None = None) → None[source]

Raise InstrumentError if response indicates an error.

The default implementation is a no-op. Override in subclasses that support error checking.

For protocols where errors_in_response is True (Oxford, Lakeshore), response is the parsed reply to any query and the method must inspect it for error indicators.

For error-queue protocols (SCPI), response is the parsed reply to the error_query command.

Parameters:

response (str) – The parsed response string to examine.

Keyword Parameters:

command (str | None) – The original command that was sent (used to populate the command field). Defaults to None.

Raises:

InstrumentError – If an instrument-level error is detected in the response.

command_expects_acknowledgement(command: str) → bool[source]

Return whether a write must read and validate a reply.

Parameters:

command (str) – Action command without its terminator.

Returns:

False by default; response-producing protocols override this.

Return type:

(bool)

property error_query: str | None

Query string used to retrieve the first error from the error queue.

Returns None (default) for protocols that embed errors in responses. Override in subclasses that maintain a separate error queue (e.g. SCPI returns "SYST:ERR?").

Returns:

Query command string, or None if not applicable.

Return type:

(str | None)

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().error_query
'SYST:ERR?'
>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> OxfordProtocol().error_query is None
True
property errors_in_response: bool

True if errors are embedded in normal query responses.

When this property returns True (Oxford, Lakeshore), the instrument signals command errors directly inside its reply to a query. check_error() should be called on every parsed response.

When False (SCPI), errors are held in a separate error queue and must be retrieved via error_query.

Returns:

True for response-embedded error protocols; False (default) for error-queue protocols.

Return type:

(bool)

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().errors_in_response
False
>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> OxfordProtocol().errors_in_response
True
abstractmethod format_command(command: str) → bytes[source]

Format a command string for transmission.

Parameters:

command (str) – The human-readable command string, without any protocol- specific terminators.

Returns:

Bytes ready to be passed to BaseTransport.write().

Return type:

(bytes)

abstractmethod format_query(query: str) → bytes[source]

Format a query string for transmission.

Parameters:

query (str) – The human-readable query string. Many protocols distinguish between commands (which do not elicit a response) and queries (which do); subclasses may add or modify a suffix accordingly.

Returns:

Bytes ready to be passed to BaseTransport.write().

Return type:

(bytes)

property max_frame_size: int

Maximum number of response bytes expected for one protocol frame.

Returns:

Default frame-size limit used by transports when reading a single response frame.

Return type:

(int)

parse_float(response: str, *, command: str) → float[source]

Parse a numeric query payload.

Parameters:
  • response (str) – Payload after response framing has been removed.

  • command (str) – Query command used for error context.

Returns:

Numeric value in the reply.

Return type:

(float)

Raises:

ValueError – The payload is not a floating-point number.

abstractmethod parse_response(raw: bytes, *, command: str | None = None) → str[source]

Parse a raw response from the instrument.

Parameters:

raw (bytes) – Raw bytes as returned by the transport layer.

Keyword Parameters:

command (str | None) – Command/query string that produced raw, when available. Defaults to None.

Returns:

The cleaned-up response string, stripped of any protocol- specific framing, terminators, or whitespace.

Return type:

(str)

validate_acknowledgement(raw: bytes, *, command: str) → None[source]

Validate an action reply, independently of optional error polling.

Parameters:
  • raw (bytes) – Acknowledgement received from the transport.

  • command (str) – Action command that produced the reply.

Raises:

InstrumentError – The protocol reports an error in the reply.