Occasionally you may need to prevent other applications from using the same resource that you are using. VISA has a service called locking that you can use to gain exclusive access to a resource. VISA also has another locking option in which you can have multiple sessions share a lock. Because lock sharing is an advanced topic that may involve inter-process communication, see Lock Sharing for more information. The following example uses the simpler form, the exclusive lock, to prevent other VISA applications from modifying the state of the specified serial port.

Locking Example (C)

Note The following example uses bold text to distinguish lines of code that are different from the other introductory programming examples.
#include "visa.h"
#define MAX_CNT 200
int main(void)
{
    ViStatus     status;              /* For checking errors */
    ViSession    defaultRM, instr;    /* Communication channels */
    ViUInt32     retCount;            /* Return count from string I/O */
    ViChar       buffer[MAX_CNT];     /* Buffer for string I/O */
    /* Begin by initializing the system */
    status = viOpenDefaultRM(&defaultRM);
    if (status < VI_SUCCESS) {
        /* Error Initializing VISA...exiting */
        return -1;
    }
    /* Open communication with Serial Port 1 */
    /* NOTE: For simplicity, we will not show error checking */
    status = viOpen(defaultRM, "ASRL1::INSTR", VI_NULL, VI_NULL, &instr);

    /* Set the timeout for message-based communication */
    status = viSetAttribute(instr, VI_ATTR_TMO_VALUE, 5000);

    /* Lock the serial port so that nothing else can use it */
    status = viLock(instr, VI_EXCLUSIVE_LOCK, 5000, VI_NULL, VI_NULL);

    /* Set serial port settings as needed */
    /* Defaults = 9600 Baud, no parity, 8 data bits, 1 stop bit */
    status = viSetAttribute(instr, VI_ATTR_ASRL_BAUD, 2400);
    status = viSetAttribute(instr, VI_ATTR_ASRL_DATA_BITS, 7);

    /* Set this attribute for binary transfers, skip it for this text example */
    /* status = viSetAttribute(instr, VI_ATTR_ASRL_END_IN, 0); */

    /* Ask the device for identification */
    status = viWrite(instr, "*IDN?\n", 6, &retCount);
    status = viRead(instr, buffer, MAX_CNT, &retCount);

    /* Unlock the serial port before ending the program */
    status = viUnlock(instr);

    /* Your code should process the data */

    /* Close down the system */
    status = viClose(instr);
    status = viClose(defaultRM);
    return 0;
}

Locking Example (VB)

Note The Visual Basic examples in this manual 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_CNT = 200
    Dim stat        As ViStatus
    Dim dfltRM      As ViSession
    Dim sesn        As ViSession
    Dim retCount    As Long
    Dim buffer      As String * MAX_CNT 
   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 communication with Serial Port 1
   Rem NOTE: For simplicity, we will not show error checking
   stat = viOpen(dfltRM, "ASRL1::INSTR", VI_NULL, VI_NULL, sesn)

   Rem Set the timeout for message-based communication
   stat = viSetAttribute(sesn, VI_ATTR_TMO_VALUE, 5000)

   Rem Lock the serial port so that nothing else can use it
   stat = viLock(sesn, VI_EXCLUSIVE_LOCK, 5000, "", "")

   Rem Set serial port settings as needed
   Rem Defaults = 9600 Baud, no parity, 8 data bits, 1 stop bit
   stat = viSetAttribute(sesn, VI_ATTR_ASRL_BAUD, 2400)
   stat = viSetAttribute(sesn, VI_ATTR_ASRL_DATA_BITS, 7)

   Rem Ask the device for identification
   stat = viWrite(sesn, "*IDN?", 5, retCount)
   stat = viRead(sesn, buffer, MAX_CNT, retCount)

   Rem Unlock the serial port before ending the program
   stat = viUnlock(sesn)

   Rem Your code should process the data

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

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