Locking Sample Code
- Updated2025-01-24
- 3 minute(s) read
This example uses a shared lock because two sessions are opened for performing trigger operations. The first session receives triggers and the second session sources triggers. A shared lock is needed because an exclusive lock would prohibit the other session from accessing the same resource. If viWaitOnEvent() fails, this example performs a viClose() on the resource manager without unlocking or closing the sessions. When the resource manager session closes, all sessions that were opened using it automatically close as well. Likewise, remember that closing a session that has any lock results in automatically releasing its lock(s).
C
Example
#include "visa.h" #define MAX_COUNT 128 int main(void) { ViStatus status; /* For checking errors */ ViSession defaultRM; /* Communication channels */ ViSession instrIN, /* Communication channels */ ViChar instrOUT; /* Access key for lock */ ViByte accKey[VI_FIND_BUFLEN]; /* To store device data */ ViEventType buf[MAX_COUNT]; /* To identify event */ ViEvent etype; /* To hold event info */ ViUInt32 event; /* To hold byte count */ retCount; /* Begin by initializing the system */ status = viOpenDefaultRM(&defaultRM); if (status < VI_SUCCESS) { /* Error Initializing VISA...exiting */ return -1; } /* Open communications with VXI Device at Logical Addr 16 */ status = viOpen(defaultRM, "VXI0::16::INSTR", VI_NULL, VI_NULL, &instrIN); status = viOpen(defaultRM, "VXI0::16::INSTR", VI_NULL, VI_NULL, &instrOUT); /* We open two sessions to the same device */ /* One session is used to assert triggers on TTL channel 4 */ /* The second is used to receive triggers on TTL channel 5 */ /* Lock first session as shared, have VISA generate the key */ /* Then lock the second session with the same access key */ status = viLock(instrIN, VI_SHARED_LOCK, 5000, VI_NULL, accKey); status = viLock(instrOUT, VI_SHARED_LOCK, VI_TMO_IMMEDIATE, accKey, accKey); /* Set trigger channel for sessions */ status = viSetAttribute(instrIN, VI_ATTR_TRIG_ID,VI_TRIG_TTL5); status = viSetAttribute(instrOUT,VI_ATTR_TRIG_ID,VI_TRIG_TTL4); /* Enable input session for trigger events */ status = viEnableEvent(instrIN, VI_EVENT_TRIG, VI_QUEUE, VI_NULL); /* Assert trigger to tell device to start sampling */ status = viAssertTrigger(instrOUT, VI_TRIG_PROT_DEFAULT); /* Device will respond with a trigger when data is ready */ if ((status = viWaitOnEvent(instrIN, VI_EVENT_TRIG, 20000, &etype, &event)) < VI_SUCCESS) { viClose(defaultRM); return -1; } /* Close the event */ status = viClose(event); /* Read data from the device */ status = viRead(instrIN, buf, MAX_COUNT, &retCount); /* Your code should process the data */ /* Unlock the sessions */ status = viUnlock(instrIN); status = viUnlock(instrOUT); /* Close down the system */ status = viClose(instrIN); status = viClose(instrOUT); status = viClose(defaultRM); return 0; }
Visual Basic
Example
Private Sub vbMain() Const MAX_COUNT = 128 Dim stat As ViStatus 'For checking errors Dim dfltRM As ViSession 'Communication channels Dim sesnIN As ViSession 'Communication channels Dim sesnOUT As ViSession 'Communication channels Dim aKey As String * VI_FIND_BUFLEN 'Access key for lock Dim buf As String * MAX_COUNT 'To store device data Dim etype As ViEventType 'To identify event Dim event As ViEvent 'To hold event info Dim retCount As Long '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