Add instrument CLI commands

This commit is contained in:
2025-09-26 17:56:36 +00:00
parent 37cf0a9c4b
commit b8f1352e8c
2 changed files with 102 additions and 0 deletions

View File

@@ -125,5 +125,39 @@ def run_test_cmd(
)
@app.command(name="query")
def query_cmd(
instrument: Annotated[
str,
typer.Argument(help="Instrument to query (chamber, psu, or dmm)."),
],
command: Annotated[
str,
typer.Argument(help="SCPI command to send (e.g., *IDN?, TEMP:SETPOINT?)."),
],
config_file: Annotated[
str | None,
typer.Option("--config", "-c", help="Path to configuration YAML file."),
] = None,
) -> None:
"""Send a SCPI command to an instrument and print the response.
Useful for debugging and manual instrument control. Connect to
instruments based on configuration and send raw SCPI commands.
Examples:
py-dvt-ate query chamber "*IDN?"
py-dvt-ate query psu "VOLT? 1"
py-dvt-ate query dmm "MEAS:VOLT:DC?"
"""
from py_dvt_ate.app.instrument_commands import query_instrument
query_instrument(
instrument=instrument,
command=command,
config_file=config_file,
)
if __name__ == "__main__":
app()

View File

@@ -0,0 +1,68 @@
"""Instrument query commands for CLI."""
import typer
from py_dvt_ate.app.config import load_config
from py_dvt_ate.instruments.factory import InstrumentConfig, InstrumentFactory
def query_instrument(
instrument: str,
command: str,
config_file: str | None = None,
) -> None:
"""Send a SCPI command to an instrument and print the response.
Args:
instrument: Instrument to query (chamber, psu, or dmm).
command: SCPI command to send.
config_file: Path to configuration YAML file.
"""
# Load configuration
config_path = config_file or "config/default.yaml"
try:
config = load_config(config_path)
except FileNotFoundError as err:
typer.echo(f"Error: Configuration file not found: {config_path}", err=True)
raise typer.Exit(code=1) from err
except Exception as e:
typer.echo(f"Error loading configuration: {e}", err=True)
raise typer.Exit(code=1) from e
# Create instruments
try:
# Convert AppConfig to InstrumentConfig
inst_config = InstrumentConfig(
backend=config.instruments.backend,
simulator_host=config.instruments.simulator.host,
chamber_port=config.instruments.simulator.thermal_chamber_port,
psu_port=config.instruments.simulator.power_supply_port,
dmm_port=config.instruments.simulator.multimeter_port,
chamber_visa=config.instruments.pyvisa.thermal_chamber,
psu_visa=config.instruments.pyvisa.power_supply,
dmm_visa=config.instruments.pyvisa.multimeter,
)
instruments = InstrumentFactory.create(inst_config)
except Exception as e:
typer.echo(f"Error connecting to instruments: {e}", err=True)
raise typer.Exit(code=1) from e
# Send command to the specified instrument
try:
# Access the transport layer to send raw commands
if instrument == "chamber":
response = instruments.chamber._transport.query(command) # type: ignore[attr-defined]
elif instrument == "psu":
response = instruments.psu._transport.query(command) # type: ignore[attr-defined]
elif instrument == "dmm":
response = instruments.dmm._transport.query(command) # type: ignore[attr-defined]
else:
typer.echo(f"Error: Unknown instrument '{instrument}'", err=True)
typer.echo("Valid instruments: chamber, psu, dmm", err=True)
raise typer.Exit(code=1)
if response:
typer.echo(response)
except Exception as e:
typer.echo(f"Error sending command: {e}", err=True)
raise typer.Exit(code=1) from e