SRQ Example

The following Python example demonstrates how to send SCPI commands to the VNA to set up a SRQ (Service Request) event.

Some SCPI commands take a long time to complete. These commands are also called “overlapped commands”. The most common example is SENS:SWE:MODE SING (for a slow channel).

There are 3 alternative ways to block on these commands:

  1. The simplest is using *OPC?. In this case, the client is blocked until the command completes.

Disadvantages:

  1. Polling. In this case, *OPC is sent and loop the *ESR? until it returns true. This is simple – but not quite as simple as alternative #1.

Disadvantage

  1. SRQ Events. This is the best solution, but the most complicated to implement. The following example shows how to implement the SRQ method.

See Also

Python Basics

 

 

from time import sleep
import pyvisa as visa
from pyvisa import constants
# Change this variable to the address of your instrument
VISA_ADDRESS = 'TCPIP0::localhost::inst0::INSTR'

# Create a connection (session) to the instrument
resourceManager = visa.ResourceManager()
session = resourceManager.open_resource(VISA_ADDRESS)
session.called = False

def handle_event(resource, event, user_handle):
    resource.called = True
    print(f"Handled event {event.event_type} on {resource}\n")

# Command to preset the instrument and deletes the default trace, measurement, and window
session.write("SYST:FPR")

# Create and turn on window 1
session.write("DISP:WIND1:STAT ON")

# Create the measurement
session.write("CALC1:MEAS1:DEF 'S11'")

# Displays measurement 1 in window 1 and assigns the next available trace number to the measurement
session.write("DISP:MEAS1:FEED 1")

session.write("SENS:SWE:MODE SING")
opcCode = session.query("*OPC?")


# Type of event we want to be notified about

event_type = constants.EventType.service_request
# Mechanism that we want to be notified about
event_mech = constants.EventMechanism.handler
wrapped = session.wrap_handler(handle_event)
user_handle = session.install_handler(event_type, wrapped, 42)
session.enable_event(event_type, event_mech, None)

# The following code enables service request
# Turn on the SRQ for operation complete? bit
session.write("*SRE 32")
# Clear any pending status
session.write("*CLS")    
# Set sweep time to 2 seconds          
session.write("SENS:SWE:TIME 2")
print("Sweep Started")
session.write("SENS:SWE:MODE SING")
session.write("*OPC")   # Tell VNA that we are interested in operation complete status
while not session.called:
    print("Waiting")
    sleep(0.01)
print("Done")
session.disable_event(event_type, event_mech)
session.uninstall_handler(event_type, wrapped, user_handle)