stoner_measurement.instruments.protocol.scpi.ScpiProtocol

class stoner_measurement.instruments.protocol.scpi.ScpiProtocol(terminator: bytes = b'\n', *, max_frame_size: int = 4096)[source]

Bases: BaseProtocol

SCPI protocol — newline-terminated ASCII messages.

Variables:

terminator (bytes) – Byte sequence appended to every outgoing message. Defaults to b"\n".

Examples

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

Raise InstrumentError if response signals a SCPI error.

response should be the parsed reply to a SYST:ERR? query. SCPI instruments encode their error queue entries as "<code>,\"<message>\""; a code of +0 means No Error.

Parameters:

response (str) – A response string previously obtained from error_query.

Keyword Parameters:

command (str | None) – The original command that triggered the error check.

Raises:

InstrumentError – If response does not start with "+0".

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().check_error('+0,"No error"')  # no exception
>>> try:
...     ScpiProtocol().check_error('-113,"Undefined header"', command="*IDN")
... except Exception as exc:
...     print(type(exc).__name__, exc)
InstrumentError Undefined header (command: *IDN, code: -113)
property error_query: str

SCPI error-queue query string.

Returns:

"SYST:ERR?" — the standard SCPI command for reading the first entry from the instrument’s error queue.

Return type:

(str)

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().error_query
'SYST:ERR?'
format_command(command: str) → bytes[source]

Format a SCPI command for transmission.

Parameters:

command (str) – SCPI command string (without terminator).

Returns:

UTF-8 encoded command with terminator appended.

Return type:

(bytes)

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().format_command("VOLT 5.0")
b'VOLT 5.0\n'
format_query(query: str) → bytes[source]

Format a SCPI query for transmission.

Parameters:

query (str) – SCPI query string (without terminator).

Returns:

UTF-8 encoded query with terminator appended.

Return type:

(bytes)

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().format_query("*IDN?")
b'*IDN?\n'
property max_frame_size: int

Maximum response-frame size expected for one SCPI reply.

Returns:

Maximum number of bytes to read for one SCPI response frame.

Return type:

(int)

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

Parse a raw SCPI response.

Decodes raw as UTF-8 and strips surrounding whitespace.

Parameters:

raw (bytes) – Raw bytes received from the instrument.

Returns:

Decoded and stripped response string.

Return type:

(str)

Examples

>>> from stoner_measurement.instruments.protocol import ScpiProtocol
>>> ScpiProtocol().parse_response(b'  KEITHLEY,2400,12345,C32\r\n')
'KEITHLEY,2400,12345,C32'