From 5e69085875bc6e6a9e067448c751edc821603720 Mon Sep 17 00:00:00 2001 From: Kai Chappell Date: Mon, 21 Apr 2025 12:03:55 +0000 Subject: [PATCH] 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 --- src/py_dvt_ate/instruments/scpi.py | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/py_dvt_ate/instruments/scpi.py b/src/py_dvt_ate/instruments/scpi.py index d7d3df4..89bcd82 100644 --- a/src/py_dvt_ate/instruments/scpi.py +++ b/src/py_dvt_ate/instruments/scpi.py @@ -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, + )