stoner_measurement.instruments.protocol.oxford.OxfordProtocol

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

Bases: BaseProtocol

Oxford Instruments carriage-return-terminated ASCII protocol.

Oxford Instruments instruments echo the command letter as the first character of each response. An error is indicated when the response payload (after stripping the echo character) begins with "?". Because the error is carried in the normal response, this protocol sets errors_in_response to True; callers should invoke check_error() on every parsed query response. Actions require a single command-letter acknowledgement, except for Q communications-mode commands and explicitly dollar-prefixed commands that suppress replies.

Variables:

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

Examples

>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> p = OxfordProtocol()
>>> p.format_command("S5")
b'S5\r'
>>> p.format_query("R1")
b'R1\r'
>>> p.parse_response(b'R1.234\r')
'1.234'
>>> 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 Oxford error.

Oxford instruments respond with a payload starting with "?" when the instrument did not understand the command.

Parameters:

response (str) – Parsed response string (echo character already stripped by parse_response()).

Keyword Parameters:

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

Raises:

InstrumentError – If the response starts with "?".

Examples

>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> OxfordProtocol().check_error("1.234")  # no exception
>>> try:
...     OxfordProtocol().check_error("?", command="R99")
... except Exception as exc:
...     print(type(exc).__name__, exc)
InstrumentError Oxford Instruments: unrecognised command (command: R99)
command_expects_acknowledgement(command: str) → bool[source]

Return whether an Oxford action returns a command-letter reply.

Parameters:

command (str) – Action command without its terminator.

Returns:

False for communications-mode Q commands and explicit

dollar-prefixed reply suppression; True otherwise.

Return type:

(bool)

property errors_in_response: bool

True — Oxford errors are embedded in the response payload.

Returns:

Always True for OxfordProtocol.

Return type:

(bool)

Examples

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

Format an Oxford Instruments command for transmission.

Parameters:

command (str) – Command string without terminator.

Returns:

ASCII-encoded command with carriage-return appended.

Return type:

(bytes)

Examples

>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> OxfordProtocol().format_command("H1")
b'H1\r'
format_query(query: str) → bytes[source]

Format an Oxford Instruments query for transmission.

Oxford Instruments does not formally distinguish commands from queries; both are formatted identically.

Parameters:

query (str) – Query string without terminator.

Returns:

ASCII-encoded query with carriage-return appended.

Return type:

(bytes)

Examples

>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> OxfordProtocol().format_query("R1")
b'R1\r'
property max_frame_size: int

Maximum response-frame size expected for one Oxford reply.

Returns:

Maximum number of bytes to read for one Oxford response.

Return type:

(int)

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

Parse the first comma-separated numeric Oxford payload.

Parameters:
  • response (str) – Payload after command-echo removal.

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

Returns:

Numeric value in the first response field.

Return type:

(float)

Raises:

ValueError – The first field is not numeric.

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

Parse a raw Oxford Instruments response.

Oxford responses often echo the command letter as their first character. This method strips that leading echo character only when it matches the first character of command (case-insensitively), then strips the trailing carriage return and surrounding whitespace.

Parameters:

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

Keyword Parameters:

command (str | None) – Command/query string that produced raw. When provided, a leading echoed command character is removed only if it matches the first character of this command, ignoring case. Defaults to None.

Returns:

Response payload with any matching echo character and the terminator removed.

Return type:

(str)

Notes

When command is not provided, this method uses legacy behaviour and strips the first character for multi-character responses.

Examples

>>> from stoner_measurement.instruments.protocol import OxfordProtocol
>>> OxfordProtocol().parse_response(b'R1.234\r', command="R1")
'1.234'
>>> OxfordProtocol().parse_response(b'ITC503 Version 1.11\r', command="V")
'ITC503 Version 1.11'
>>> OxfordProtocol().parse_response(b'S$A0C0H1L0R0B0\r', command="S")
'$A0C0H1L0R0B0'
validate_acknowledgement(raw: bytes, *, command: str) → None[source]

Require an accepted action’s single command-letter acknowledgement.

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

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

Raises:

InstrumentError – The action was rejected or its reply is malformed.