As a rule, send commands and queries in different program messages.
Command sequence and synchronization
IEEE 488.2 defines a distinction between overlapped and sequential commands:
A sequential command finishes executing before the next command starts executing. Commands that are processed quickly are usually implemented as sequential commands.
An overlapped command does not automatically finish executing before the next command starts executing. Usually, overlapped commands take longer to be processed and allow the program to do other tasks while being executed. If overlapped commands have to be executed in a defined order, for example, to avoid wrong measurement results, they must be executed sequentially. This queueing is called synchronization between the controller and the instrument.
Setting commands within one command line are not necessarily serviced in the order in which they have been received, even if they are implemented as sequential commands. To make sure that commands are carried out in a certain order, each command must be sent in a separate command line.
If you combine a query with commands that affect the queried value in a single program message, the response of the query is not predictable.
The following commands always return the specified result:
:FREQ:STAR 1GHZ; SPAN 100
:FREQ:STAR?
Returned result is always 1000000 (1 GHz).
Whereas the result for the following commands is not specified by SCPI:
:FREQ:STAR 1GHZ;STAR?; SPAN 100
The result could be 1 GHz if the instrument executes commands as they are received. However, the instrument can defer executing the individual commands until a program message terminator is received. Thus, the result could also be the value of STARt before the command was sent.
Suppose an instrument implements INITiate as an overlapped command to initiate a sweep. Assuming that INITiate takes longer to execute than *OPC, sending the following commands results in initiating a sweep. When the command has been executed, the OPC bit in the event status register ESR is set:
INIT;*OPC
Sending the following commands also initiates a sweep:
INIT;*OPC;*CLS
However, the operation is still pending when the instrument executes the clear status command (*CLS) that sets the operation complete command idle state (OCIS). So, *OPC is effectively skipped and the instrument does not set the OPC bit until it executes another *OPC command.
To prevent an overlapping execution of commands, one of the commands *OPC, *OPC? or *WAI can be used. All three commands cause a certain action only to be carried out after the hardware has been set. The controller can be forced to wait for the corresponding action to occur.
Command | Action | Programming the controller |
---|---|---|
*OPC | Sets the Operation Complete bit in the Standard Event Status Register (ESR) after all previous commands have been executed. |
|
*OPC? | The instrument does not respond to further commands until 1 is returned, which happens when all pending operations are completed. | Send *OPC? directly after the command whose processing must be terminated before other commands can be executed. |
*WAI | The instrument holds the processing of further commands until all commands sent before the Wait-to-Continue command (WAI) have been executed. | Send *WAI directly after the command whose processing must be terminated before other commands are executed. Due to no instrument response, we recommend other methods. |
Command synchronization using *WAI or *OPC? is a good choice if the overlapped command takes only little time to process. The two synchronization commands simply block overlapping execution of the command. Append the synchronization command to the overlapped command, for example:
SINGle;*OPC?
For time-consuming overlapped commands, you can allow the controller or the instrument to do other useful work while waiting for command execution. Use one of the following methods:
Execute *SRE 32
Sets the Event Status Bit (ESB) of the service request enable register (SRE - bit 5 weighted 32) to 1 to enable ESB service request.
Execute *ESE 1
Sets the OPC mask bit of the standard event status register (ESR - bit 0 weighted 1) to 1.
Send the overlapped command with *OPC.
Example: INIT;*OPC
Wait for an ESB service request.
The service request indicates that the overlapped command has finished.
Execute *SRE 16.
Sets the Message Available bit (MAV) of the SRE (bit 4 weighted 16) to 1 to enable MAV service request.
Send the overlapped command with *OPC?.
Example: INIT;*OPC?
Wait for an MAV service request.
The service request indicates that the overlapped command has finished.
Send the overlapped command with *OPC?.
Example: viWrite("INIT: *OPC?");
Use serial poll (viReadSTB(vi, &stb)) and check MAV (bit 4 weighted 16) or ERR (bit 2 weighted 4).
Repeat the serial poll after 50 ms to 1000 ms until the operation is complete. Note that excessive serial polls can slow down the operation of your instrument.
A MAV bit indicates that the overlapped command has finished successfully, ERR bit indicates that the overlapped command has failed.
For more programming examples, see "Command synchronization".