Locking Sample Code Example (VB)

NI-VISA

Locking 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.

Private Sub vbMain()

Const MAX_COUNT = 128

Dim stat
Dim dfltRM
Dim sesnIN
Dim sesnOUT
Dim aKey
Dim buf
Dim etype
Dim event
Dim retCount
As ViStatus
As ViSession
As ViSession
As ViSession
As String * VI_FIND_BUFLEN
As String * MAX_COUNT
As ViEventType
As ViEvent
As Long
'For checking errors
'Communication channels
'Communication channels
'Communication channels
'Access key for lock
'To store device data
'To identify event
'To hold event info
'To hold byte count
Rem Begin by initializing the system
stat = viOpenDefaultRM(dfltRM)
If (stat < VI_SUCCESS) Then

Rem Error initializing VISA...exiting
Exit Sub

End If

Rem Open communications with VXI Device at Logical Addr 16
stat = viOpen(dfltRM, "VXI0::16::INSTR", VI_NULL, VI_NULL, sesnIN)
stat = viOpen(dfltRM, "VXI0::16::INSTR", VI_NULL, VI_NULL, sesnOUT)

Rem We open two sessions to the same device
Rem One session is used to assert triggers on TTL channel 4
Rem The second is used to receive triggers on TTL channel 5

Rem Lock first session as shared, have VISA generate the key
Rem Then lock the second session with the same access key
stat = viLock(sesnIN, VI_SHARED_LOCK, 5000, "", aKey)
stat = viLock(sesnOUT, VI_SHARED_LOCK, VI_TMO_IMMEDIATE, aKey, aKey)

Rem Set trigger channel for sessions
stat = viSetAttribute(sesnIN, VI_ATTR_TRIG_ID, VI_TRIG_TTL5)
stat = viSetAttribute(sesnOUT, VI_ATTR_TRIG_ID, VI_TRIG_TTL4)

Rem Enable input session for trigger events
stat = viEnableEvent(sesnIN, VI_EVENT_TRIG, VI_QUEUE, VI_NULL)

Rem Assert trigger to tell device to start sampling
stat = viAssertTrigger(sesnOUT, VI_TRIG_PROT_DEFAULT)

Rem Device will respond with a trigger when data is ready
stat = viWaitOnEvent(sesnIN, VI_EVENT_TRIG, 20000, etype, event)
If (stat < VI_SUCCESS) Then

stat = viClose (dfltRM)
Exit Sub

End If

Rem Close the event
stat = viClose(event)

Rem Read data from the device
stat = viRead(sesnIN, buf, MAX_COUNT, retCount)

Rem Your code should process the data

Rem Unlock the sessions
stat = viUnlock(sesnIN)
stat = viUnlock(sesnOUT)

Rem Close down the system
stat = viClose(sesnIN)
stat = viClose(sesnOUT)
stat = viClose(dfltRM)

End Sub