Implement SCPI parser
Adds SCPIParser class with parse() method that handles: - IEEE 488.2 common commands (*IDN?, *RST, etc.) - Query commands (ending with '?') - Commands with comma-separated arguments - Whitespace stripping
This commit is contained in:
@@ -30,3 +30,57 @@ class SCPICommand:
|
||||
For regular commands like "VOLT", returns "VOLT".
|
||||
"""
|
||||
return self.header.rstrip("?")
|
||||
|
||||
|
||||
class SCPIParser:
|
||||
"""Parse SCPI command strings.
|
||||
|
||||
Handles both IEEE 488.2 common commands (e.g., *IDN?, *RST) and
|
||||
instrument-specific commands (e.g., VOLT 3.3, TEMP:SETPOINT?).
|
||||
|
||||
Examples:
|
||||
>>> parser = SCPIParser()
|
||||
>>> cmd = parser.parse("*IDN?")
|
||||
>>> cmd.header, cmd.is_query
|
||||
('*IDN?', True)
|
||||
>>> cmd = parser.parse("VOLT 3.3")
|
||||
>>> cmd.header, cmd.arguments
|
||||
('VOLT', ['3.3'])
|
||||
"""
|
||||
|
||||
def parse(self, command_string: str) -> SCPICommand:
|
||||
"""Parse a SCPI command string.
|
||||
|
||||
Args:
|
||||
command_string: The raw SCPI command string to parse.
|
||||
|
||||
Returns:
|
||||
SCPICommand with parsed header, arguments, and query flag.
|
||||
|
||||
Examples:
|
||||
"*IDN?" -> SCPICommand("*IDN?", [], True)
|
||||
"VOLT 3.3" -> SCPICommand("VOLT", ["3.3"], False)
|
||||
"TEMP:SETPOINT?" -> SCPICommand("TEMP:SETPOINT?", [], True)
|
||||
"CONF:VOLT:DC 10,0.001" -> SCPICommand("CONF:VOLT:DC", ["10", "0.001"], False)
|
||||
"""
|
||||
command_string = command_string.strip()
|
||||
if not command_string:
|
||||
return SCPICommand(header="", arguments=[], is_query=False)
|
||||
|
||||
is_query = command_string.endswith("?")
|
||||
|
||||
# Split into header and arguments on first whitespace
|
||||
parts = command_string.split(None, 1)
|
||||
header = parts[0]
|
||||
arguments: list[str] = []
|
||||
|
||||
if len(parts) > 1:
|
||||
# Parse comma-separated arguments
|
||||
arg_string = parts[1]
|
||||
arguments = [arg.strip() for arg in arg_string.split(",")]
|
||||
|
||||
return SCPICommand(
|
||||
header=header,
|
||||
arguments=arguments,
|
||||
is_query=is_query,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user