stoner_measurement.instruments.protocol.lakeshore.LakeshoreProtocol

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

Bases: BaseProtocol

Lakeshore simple ASCII CRLF-terminated protocol.

Lakeshore instruments signal an unrecognised command by returning "?" or a "?"-prefixed string in place of the expected value. Because the error indicator is embedded in the normal query response, this protocol sets errors_in_response to True.

Variables:

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

Examples

>>> from stoner_measurement.instruments.protocol import LakeshoreProtocol
>>> p = LakeshoreProtocol()
>>> p.format_command("SETP 1,10.0")
b'SETP 1,10.0\r\n'
>>> p.format_query("KRDG? A")
b'KRDG? A\r\n'
>>> p.parse_response(b'+273.150\r\n')
'+273.150'
>>> p.errors_in_response
True
>>> p.error_query is None
True
check_error(response: str, *, command: str | None = None) → None[source]

Raise InstrumentError if response indicates an error.

Some Lakeshore models return "?" or a "?"-prefixed string for unrecognised commands.

Parameters:

response (str) – Parsed response string.

Keyword Parameters:

command (str | None) – The original command that was sent.

Raises:

InstrumentError – If the response is "?" or starts with "?".

Examples

>>> from stoner_measurement.instruments.protocol import LakeshoreProtocol
>>> LakeshoreProtocol().check_error("+77.350")  # no exception
>>> try:
...     LakeshoreProtocol().check_error("?", command="KRDG? Z")
... except Exception as exc:
...     print(type(exc).__name__, exc)
InstrumentError Lakeshore: unrecognised command (command: KRDG? Z)
property errors_in_response: bool

True — Lakeshore errors are embedded in the response payload.

Returns:

Always True for LakeshoreProtocol.

Return type:

(bool)

Examples

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

Format a Lakeshore command for transmission.

Parameters:

command (str) – Command string without terminator.

Returns:

ASCII-encoded command with CRLF appended.

Return type:

(bytes)

Examples

>>> from stoner_measurement.instruments.protocol import LakeshoreProtocol
>>> LakeshoreProtocol().format_command("RAMP 1,1,0.5")
b'RAMP 1,1,0.5\r\n'
format_query(query: str) → bytes[source]

Format a Lakeshore query for transmission.

Parameters:

query (str) – Query string without terminator.

Returns:

ASCII-encoded query with CRLF appended.

Return type:

(bytes)

Examples

>>> from stoner_measurement.instruments.protocol import LakeshoreProtocol
>>> LakeshoreProtocol().format_query("SRDG? B")
b'SRDG? B\r\n'
property max_frame_size: int

Maximum response-frame size expected for one Lakeshore reply.

Returns:

Maximum number of bytes to read for one Lakeshore response.

Return type:

(int)

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

Parse a raw Lakeshore response.

Decodes the bytes as ASCII and strips surrounding whitespace, including the CRLF terminator.

Parameters:

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

Returns:

Decoded response string with whitespace stripped.

Return type:

(str)

Examples

>>> from stoner_measurement.instruments.protocol import LakeshoreProtocol
>>> LakeshoreProtocol().parse_response(b'+77.350\r\n')
'+77.350'
>>> LakeshoreProtocol().parse_response(b'1,CONTROL,1\r\n')
'1,CONTROL,1'