stoner_measurement.instruments.protocol.base.BaseProtocol¶
- class stoner_measurement.instruments.protocol.base.BaseProtocol[source]¶
Bases:
ABCAbstract 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(), andparse_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_responsereturnsTruefor these protocols andcheck_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_queryreturns the query string andcheck_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
InstrumentErrorif response indicates an error.The default implementation is a no-op. Override in subclasses that support error checking.
For protocols where
errors_in_responseisTrue(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_querycommand.- Parameters:
response (str) – The parsed response string to examine.
- Keyword Parameters:
command (str | None) – The original command that was sent (used to populate the
commandfield). Defaults toNone.- 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.
- 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
Noneif 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¶
Trueif 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 viaerror_query.- Returns:
Truefor 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.
- 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:
- 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)