You can enable SCPI compliant instruments to generate a service request when the instrument needs attention or some event has happened. You can use this service request to raise an event (also called a call back) by VISA COM . This allows you to have your code perform other tasks and to interrupt those tasks on demand from the instrument.
Some typical ways an instrument can generate a service request include:

  • When all instrument operations have completed (such as a reading)
  • If an instrument error occurs
  • If a measurement is of dubious quality
  • If an operational condition is met
  • If the instrument power has been turned off and on since the last time the register was read

The service request uses the registers in the instrument's SCPI Status System. Each instrument implements the registers in a way that makes sense for the instrument type. For example, a waveform generator might have a bit to indicate when an external reference frequency is lost, whereas a multimeter might have a bit to indicate an Ohms measurement overload occurred. You must refer to the instrument documentation to determine what ways the instrument can generate a service request.

Not all GPIB controllers can raise an event in response to an instrument service request. The Keysight VISA COM I/O library supports the SRQ Event for Keysight GPIB cards. If your interface card does not support the SRQ event, you will need to use polling as described in Polling for SRQ section.

Note: You cannot use SRQ events with the RS-232 interface.

For these examples we'll use an Keysight 34401A DMM in Visual Basic 6.0 to demonstrate the use of the SRQ event. A complete project is provided on the same web page as this tutorial. The following describes that example.

Setting up the SRQ event is a 5-steps process. Consult the instrument documentation to determine what instrument actions can generate a service request.

  1. Set up IeventHandler to fire the event IeventHandler_HandleEvent when the instrument gives an SRQ.
  2. Clear the instrument and configure the instrument to give an SRQ when the selected operation is complete.
  3. Set up the instrument for the required operation and initiate the operation. Include the IEEE-488.2 Common Command *OPC as the last instrument command.
  4. If the program is executing when waiting for the event to fire, a DoEvents statement is required.
  5. When the IeventHandler_HandleEvent event fires, handle the service request (for the DMM we will use the event to indicate when to get the readings).

1. Set up IEventHandler

To establish an event that is executed when the instrument sees an SRQ (Service Request), we must use the Implements declaration to create the event procedure at the top of the module. This declaration must be right after the Option Explicit declaration.

Implements VisaComLib.IEventHandler

We also have to declare an object that allows us to turn on the SRQ event. This declaration is at the module level scope.

Dim SRQ As VisaComLib.IeventManager

We can now enable the service request to generate the event. In the 34401A DMM example, this is done in the same routine that sets up the I/O session.

' Set the SRQ to the I/O session and
' enable the SRQ
Set SRQ = DMM.IO
SRQ.InstallHandler EVENT_SERVICE_REQ, Me
SRQ.EnableEvent EVENT_SERVICE_REQ, EVENT_HNDLR

When the service request fires, it raises the event IEventHandler_HandleEvent . Create an event handler routine that can be executed from this event. For the 34401A we will read the data from the instrument, since this event indicates that the readings are complete.

2. Clear the instrument and configure to give an SRQ

The Keysight 34401A Multimeter can take a burst of readings that may take many seconds or even minutes for number of high-resolution measurements. To indicate the completion of a burst of readings we set the Standard Event register using the *ESE 1 command. The value of 1 indicates bit 0 or Operation Complete bit. Then we set the Status Byte Enable register using the *SRE 32 command to enable the Standard Event bit (bit 5) in the Status Byte register to enable the SRQ line. The code to enable Operation Complete, and enable SRQ is below for the Keysight 34401A DMM. See the VB tutorials on how to create an IO connection DMM of type FormattedIO488.

With DMM
  .Output "*RST" ' Reset multimeter
  .Output "*CLS" ' Clear status registers
  ' Enable Operation Complete bit to 
  .Output "*ESE 1"
  ' Enable standard event bit in status byte
  .Output "*SRE 32"
  .WriteString "*OPC?"     ' Assure synchronization
  strTemp = .ReadString    ' Discard returned value
End With

3. Set up the instrument for the required task

At this point we set up the configuration of the instrument to take a reading. This is the code to set the 34401A to take a burst of readings.

With DMM
    .WriteString "Configure:Voltage:dc 10"  
    .WriteString "Voltage:DC:NPLC 10"
    .WriteString "Trigger:count" & Str$(numberReadings)
    ' Initiate the readings
    .WriteString "Init"
    ' Set 'operation complete' bit in standard
    ' event registers when measurement is complete
    .WriteString "*OPC"
End With

4. Prepare the code to accept an event

An event (call back) will only fire when the application gives up control or when the Visual Basic program is in 'idle'. If the program is running when the instrument executes an SRQ, the event will not fire until the program gives up control with a DoEvents statement, or until the program has completed and is idle.

Timer events are the best tool for processing tasks when waiting for an event. In the DMM example we will use a Timer event to periodically write to the display to indicate the status. Between the timer events the program is idle and can receive an event. See Allowing Users to Interrupt Tasks of the Visual Basic Programmers Guide documentation for more information on using timers for background tasks.

If program execution continues while waiting for an SRQ event, then a DoEvents statement can be used to give up control and fire the event. Control returns to your application as soon as all other applications in the environment have had a chance to respond to pending events. This doesn't cause the current application to give up the focus, but it does enable background events to be processed.

5. Handle the service request

When the SRQ event fires the sub IEventHandler_HandleEvent is executed. The program is directed to another routine. For the DMM example we will call the sub ReadData from the event handler. After reading the data, the program goes idle. In your program you may want to redirect it to continue.

Note: On version 1.0 of VISA COM change the argument name event to avoid using a VB key word. In this example the argument event was changed to SRQevent

Private Sub IEventHandler_HandleEvent( _
        ByVal vi As VisaComLib.IEventManager, _
        ByVal SRQevent As VisaComLib.IEvent, _
        ByVal userHandle As Long)
    ' Once the SRQ is detected, then get the data
    ReadData
End Sub
Private Sub ReadData()
    Dim readings() As Double
    
    With DMM
        .WriteString "Fetch?"      ' Query for the data in memory
        ' get the data put in array
        readings = . ReadList(ASCIIType_R8)     
    End With
End Sub