Locking Example Discussion

NI-VISA

Locking Example Discussion

As you can see, the program does not differ with respect to controlling the instrument. The ability to lock and unlock the resource simply involves inserting the viLock() and viUnlock() operations around the code that you want to ensure is protected, as far as the instrument is concerned.

To lock a resource, you use the viLock() operation on the session to the resource. Notice that the second parameter is VI_EXCLUSIVE_LOCK. This parameter tells VISA that you want this session to be the only session that can access the device. The next parameter, 5000, is the time in milliseconds you are willing to wait for the lock. For example, another program may have locked its session to the resource before you. Using this timeout feature, you can tell your program to wait until either the other program has unlocked the session, or 5 s have passed, whichever comes first.

The final two parameters are used in the lock sharing feature of viLock() and are discussed further in VISA Locks. For most applications, however, these parameters are set to VI_NULL. Notice that if the viLock() call succeeds, you then have exclusive access to the device. Other programs do not have access to the device at all. Therefore, you should hold a lock only for the time you need to program the device, especially if you are designing an instrument driver. Failure to do so may cause other applications to block or terminate with a failure.

When using a VISA lock over the Ethernet, the lock applies to any machine using the given resource. For example, calling viLock() when using a National Instruments ENET Serial controller prevents other machines from performing I/O on the given serial port.

To end the example, the application calls viUnlock() when it has acquired the data from the instrument. At this point, the resource is accessible from any other session in any application.