Queuing and Callback Mechanism Sample Code Example (VB)

NI-VISA

Queuing and Callback Mechanism Sample Code Example (Visual Basic)

Note  The Visual Basic examples in the NI-VISA Help use the VISA data types where applicable. This feature is available only on Windows. To use this feature, select the VISA library (visa32.dll) as a reference from Visual Basic. This makes use of the type library embedded into the DLL.

Visual Basic does not support callback handlers, so currently the only way to handle events is through viWaitOnEvent(). Because Visual Basic does not support asynchronous operations either, this example uses the viRead() call instead of the viReadAsync() call.

Private Sub vbMain()

Const MAX_CNT = 1024

Dim stat
Dim dfltRM
Dim sesn
Dim bufferHandle
Dim retCount
Dim etype
Dim event
Dim stb
As ViStatus
As ViSession
As ViSession
As String
As Long
As ViEventType
As ViEvent
As Integer
Rem Begin by initializing the system
Rem NOTE: For simplicity, we will not show error checking
stat = viOpenDefaultRM(dfltRM)
If (stat < VI_SUCCESS) Then

Rem Error initializing VISA...exiting
Exit Sub

End If

Rem Open communication with GPIB device at primary address 2
stat = viOpen(dfltRM, "GPIB0::2::INSTR", VI_NULL, VI_NULL, sesn)

Rem Allocate memory for buffer
Rem In addition, allocate space for the ASCII NULL character
bufferHandler = Space$(MAX_CNT + 1)

Rem Enable the driver to detect events
stat = viEnableEvent(sesn, VI_EVENT_SERVICE_REQ, VI_QUEUE, VI_NULL)

Rem Tell the device to begin acquiring a waveform
stat = viWrite(sesn, "E0x51; W1", 9, retCount)

Rem The device asserts SRQ when the waveform is ready
stat = viWaitOnEvent(sesn, VI_EVENT_SERVICE_REQ, 20000, etype, event)
If (stat < VI_SUCCESS) Then

Rem Waveform not received...exiting
stat = viClose (dfltRM)
Exit Sub

End If
stat = viReadSTB (sesn, stb)

Rem Read the data
stat = viRead(sesn, bufferHandle, MAX_CNT, retCount)
Rem Your code should process the waveform data

Rem Close the event context
stat = viClose (event)

Rem Stop listening for events
stat = viDisableEvent(sesn, VI_ALL_ENABLED_EVENTS, VI_ALL_MECH)

Rem Close down the system
stat = viClose(sesn)
stat = viClose(dfltRM)

End Sub