Example of Handling Events

NI-VISA

Example of Handling Events

When dealing with instrument communication, it is very common for the instrument to require service from the controller when the controller is not actually looking at the device. A device can notify the controller via a service request (SRQ), interrupt, or a signal. Each of these is an asynchronous event, or simply an event. In VISA, you can handle these and other events through either callbacks or a software queue.

Callbacks

Using callbacks, you can have sections of code that are never explicitly called by the program, but instead are called by the VISA driver whenever an event occurs. Due to their asynchronous nature, callbacks can be difficult to incorporate into a traditional, sequential flow program. Therefore, we recommend the queuing method of handling events for new users. If you are an experienced user or understand callback concepts, see VISA Events Callbacks.

Queuing

When using a software queue, the VISA driver detects the asynchronous event but does not alert the program to the occurrence. Instead, the driver maintains a list of events that have occurred so that the program can retrieve the information later. With this technique, the program can periodically poll the driver for event information or halt the program until the event has occurred. The following example programs an oscilloscope to capture a waveform. When the waveform is complete, the instrument generates a VXI interrupt, so the program must wait for the interrupt before trying to read the data.

Example

Note  The following example uses bold text to distinguish lines of code that are different from the other introductory programming examples.

#include "visa.h"

int main(void)
{

ViStatus
ViSession
ViEvent
ViUInt16
status;
defaultRM, instr;
eventData;
statID;
/* For checking errors */
/* Communication channels */
/* To hold event info */
/* Interrupt Status ID */
/* Begin by initializing the system */
status = viOpenDefaultRM(&defaultRM);
if (status < VI_SUCCESS)
{

/* Error Initializing VISA...exiting */
return -1;

}

/* Open communication with VXI Device at Logical Address 16 */
/* NOTE: For simplicity, we will not show error checking */
status = viOpen(defaultRM, "VXI0::16::INSTR", VI_NULL, VI_NULL, &instr);

/* Enable the driver to detect the interrupts */
status = viEnableEvent(instr, VI_EVENT_VXI_SIGP, VI_QUEUE, VI_NULL);

/* Send the commands to the oscilloscope to capture the */
/* waveform and interrupt when done */

status = viWaitOnEvent(instr, VI_EVENT_VXI_SIGP, 5000, VI_NULL, &eventData);
if (status < VI_SUCCESS) {

/* No interrupts received after 5000 ms timeout */
viClose(defaultRM);
return -1;

}

/* Obtain the information about the event and then destroy the */
/* event. In this case, we want the status ID from the interrupt. */
status = viGetAttribute(eventData, VI_ATTR_SIGP_STATUS_ID, &statID);
status = viClose(eventData);

/* Your code should read data from the instrument and process it.*/

/* Stop listening to events */
status = viDisableEvent(instr, VI_EVENT_VXI_SIGP, VI_QUEUE);


/* Close down the system */
status = viClose(instr);
status = viClose(defaultRM);
return 0; }

Visual Basic Example

Example Discussion