Locking Sample Code

NI-VISA

Locking Sample Code

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

Example

Note  This example shows C source code. There is also an example in Visual Basic syntax.

#include "visa.h"

#define MAX_COUNT 128

int main(void)
{

ViStatus
ViSession
ViSession
ViChar
ViByte
ViEventType
ViEvent
ViUInt32
status;
defaultRM;
instrIN,
instrOUT;
accKey[VI_FIND_BUFLEN];
buf[MAX_COUNT];
etype;
event;
retCount;
/* For checking errors */
/* Communication channels */
/* Communication channels */
/* Access key for lock */
/* To store device data */
/* To identify event */
/* To hold event info */
/* To hold byte count */
/* 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;

}