Skip to content

Query Instruments

Most interaction with instruments comes down to two operations: sending a query (expecting a response) or sending a command (no response expected). mcpyvisa provides instrument_query and instrument_write for these, with the important distinction being whether the instrument sends data back.

The naming convention follows SCPI: commands ending with ? are queries that return data. Commands without ? configure the instrument without returning a response.

ToolWhen to useExample
instrument_queryThe command expects a response back from the instrumentMEAS:VOLT:DC?, *IDN?, SYST:ERR?
instrument_writeThe command configures the instrument with no responseCONF:VOLT:DC 10,0.001, *RST, OUTP ON

All tools accept an instrument parameter that can be either a configured alias or a full VISA resource string:

instrument_query("dmm", "MEAS:VOLT:DC?")
instrument_write("psu", "OUTP ON")
instrument_identify("scope")

Aliases are defined in mcpyvisa.toml under [instruments.*] sections. They map to a VISA resource string and backend.

The simplest way to read a value from a multimeter or other measurement instrument:

> Measure DC voltage on the DMM

The LLM calls instrument_query("dmm", "MEAS:VOLT:DC?"):

+1.23456789E+01

The response is the raw string from the instrument — typically a number in scientific notation.

These commands work on most modern SCPI-compliant multimeters (Keysight 34401A, Keithley 2000, Fluke 8846A, etc.):

MEAS configures, triggers, and reads in a single command:

MEAS:VOLT:DC? DC voltage, autorange
MEAS:VOLT:DC? 10,0.001 DC voltage, 10V range, 1mV resolution
MEAS:VOLT:AC? AC voltage
MEAS:CURR:DC? DC current
MEAS:CURR:AC? AC current
MEAS:RES? 2-wire resistance
MEAS:FRES? 4-wire resistance
MEAS:FREQ? Frequency

For more control, separate configuration from reading:

> Configure the DMM for DC voltage on the 10V range

The LLM calls instrument_write("dmm", "CONF:VOLT:DC 10,0.001"), then:

> Now read the measurement

The LLM calls instrument_query("dmm", "READ?"):

+1.23456789E+01

CONF sets the function and range without triggering. READ? triggers a new measurement and returns the result. This pattern is useful when you want to configure once and take multiple readings.

Triggered measurements with INIT and FETCH?

Section titled “Triggered measurements with INIT and FETCH?”

For precise triggering control:

instrument_write("dmm", "CONF:VOLT:DC 10,0.001")
instrument_write("dmm", "TRIG:SOUR IMM")
instrument_write("dmm", "INIT")
instrument_query("dmm", "FETCH?")

INIT starts the measurement process. FETCH? retrieves the result without triggering a new measurement.

Some measurements take longer than the default 3-second read timeout:

  • High-resolution DC measurements with long integration times (10 PLC or 100 PLC)
  • Frequency measurements on low-frequency signals
  • Resistance measurements in high ranges (above 10 MOhm)
  • Self-test queries (*TST?)
> Set the read timeout on bench-a to 30 seconds, then run a self-test on the DMM

The LLM calls configure_ar488("bench-a", read_timeout_ms=30000), then instrument_query("dmm", "*TST?").

Use the per-backend config for consistently slow instruments

Section titled “Use the per-backend config for consistently slow instruments”

If an instrument is always slow (e.g., a precision electrometer), set a higher timeout in the config file rather than adjusting at runtime:

mcpyvisa.toml
[[backend]]
name = "precision-bench"
type = "ar488"
transport = "serial"
port = "/dev/ttyUSB1"
read_timeout_ms = 15000

After sending commands, check whether the instrument encountered an error:

> Check for errors on the DMM

The LLM calls instrument_query("dmm", "SYST:ERR?"):

0,"No error"

If there was an error:

-222,"Data out of range"

Read SYST:ERR? in a loop until you get 0,"No error" — instruments queue multiple errors:

> Keep reading errors from the DMM until the queue is empty

Older instruments (HP 3478A, Fluke 8840A, Solartron 7150) use proprietary command sets. Use instrument_write and instrument_query the same way, but with instrument-specific commands:

> Send "F1RA" to the HP 3478A

The LLM calls instrument_write("hp3478", "F1RA").

For instruments with non-standard response behavior, you can still use instrument_query with any command string and read the response:

> Send "F1RA" to the HP 3478A and read the response

The LLM calls instrument_query("hp3478", "F1RA").

To return an instrument to its power-on defaults:

> Reset the DMM

The LLM calls instrument_reset("dmm"), which sends *RST. The instrument reconfigures to its factory default measurement settings. Wait a few seconds after reset before sending new commands — some instruments take time to reconfigure internally.

When mcpyvisa is communicating with an instrument, the front panel may be locked out. To return control to a human operator:

> Return the DMM to local control

The LLM calls instrument_local("dmm"). The instrument’s front panel becomes active again.

To explicitly lock out the front panel (preventing accidental changes during automated sequences):

> Put the DMM in remote mode with lockout

The LLM calls instrument_remote("dmm", lockout=True).