Asynchronous Read/Write Services
In addition to the synchronous read and write services, VISA has operations for asynchronous I/O. The functionality of these operations is identical to that of the synchronous ones; therefore, the topics covered in the previous section apply to asynchronous read and write operations as well. The main difference is that a job ID is returned from the asynchronous I/O operations instead of the transfer status and return count. You then wait for an I/O completion event, from which you can get that information.
Note You must enable the session for the I/O completion event before beginning an asynchronous transfer. |
One other difference is the timeout attribute, VI_ATTR_TMO_VALUE. This attribute may or may not apply to asynchronous operations, depending on the implementation. If you want to ensure that asynchronous operations never time out, even on implementations that do use the timeout attribute, set the attribute value to VI_TMO_INFINITE. If you want to ensure that asynchronous operations do not last beyond a certain period of time, even on implementations that do not use the timeout attribute, you should abort the I/O using the viTerminate() operation if it does not complete within the expected time, as shown in the following code.
status = viEnableEvent(instr, VI_EVENT_IO_COMPLETION, VI_QUEUE, VI_NULL);
status = viWriteAsync(instr, "READ:WAVFM:CH1" ,14, &jobID);
status = viWaitOnEvent(instr, VI_EVENT_IO_COMPLETION, 10000, &etype, &event);
if (status < VI_SUCCESS) {
status = viTerminate(instr, VI_NULL, jobID);
/* now the I/O completion event should exist in the queue*/
status = viWaitOnEvent(instr, VI_EVENT_IO_COMPLETION,0, &etype, &event);
}
As long as an asynchronous operation is successfully posted (if the return value from the asynchronous operation is greater than or equal to VI_SUCCESS), there will always be exactly one I/O completion event resulting from the transfer. However, if the asynchronous operation (viReadAsync() or viWriteAsync()) returns an error code, there will not be an I/O completion event. In the above example, if the I/O has not completed in 10 seconds, the call to viTerminate() aborts the I/O and results in the I/O completion event being generated.
The I/O completion event has attributes containing information about the transfer status, return count, and more. For a more complete description of the I/O completion event and its attributes, refer to VI_EVENT_IO_COMPLETION. For a more detailed example using asynchronous I/O, see the queuing and callback mechanism example.
Note The asynchronous I/O services are not available when programming with Visual Basic. |