Message-Based Communication Example Discussion
We can break down the example into the following steps.
- Begin by initializing the VISA system. For this task you use viOpenDefaultRM(), which opens a communication channel with VISA itself. This channel has a purpose similar to a telephone line. The function call is analogous to picking up the phone and dialing the operator. From this point on, the phone line, or the value output from viOpenDefaultRM(), is what connects you to the VISA driver. Any communication on the line is between you and the VISA driver only. VISA Overview has more details about viOpenDefaultRM(), but for now it is sufficient for you to understand that the function initializes VISA and must be the first VISA function called in your program.
- Now you must open a communication channel to the device itself using viOpen(). Notice that this function uses the handle returned by viOpenDefaultRM(), which is the variable defaultRM in the example, to identify the VISA driver. You then specify the address of the device you want to talk to. Continuing with the phone analogy, this is like asking the operator to dial a number for you. In this case, you want to address a GPIB device at primary address 1 on the GPIB0 bus. The value for x in the GPIBx token (GPIB0 in this example) indicates the GPIB board to which your device is attached. This means that you can have multiple GPIB boards installed in the computer, each controlling independent buses. For more information on address strings, viOpen(), and viOpenDefaultRM(), see Initializing Your VISA Application.
The two VI_NULL values following the address string are not important at this time. They specify that the session should be initialized using VISA defaults. Finally, viOpen() returns the communication channel to the device in the parameter instr. From now on, whenever you want to talk to this device, you use the instr variable to identify it. Notice that you do not use the defaultRM handle again. The main use of defaultRM is to tell the VISA driver to open communication channels to devices. You do not use this handle again until you are ready to end the program.
- At this point, set a timeout value for message-based communication. A timeout value is important in message-based communication to determine what should happen when the device stops communicating for a certain period of time. VISA has a common function to set values such as these: viSetAttribute(). This function sets values such as timeout and the termination character for the communication channel. In this example, notice that the function call to viSetAttribute() sets the timeout to be 5 s (5000 ms) for both reading and writing strings.
- Now that you have the communication channel set up, you can perform string I/O using the viWrite() and viRead() functions. Notice that this is the section of the programming code that is unique for message-based communication. Opening communication channels, as described in steps 1 and 2, and closing the channels, as described in step 5, are the same for all VISA programs. The parameters that these calls use are relatively straightforward.
- First you identify which device you are talking to with instr.
- Next you give the string to send, or what buffer to put the response in.
- Finally, specify the number of characters you are interested in transferring.
- When you are finished with your device I/O, you can close the communication channel to the device with the viClose() function.
Notice that the program shows a second call to viClose(). When you are ready to shut down the program, or at least close down the VISA driver, you use viClose() to close the communication channel that was opened using viOpenDefaultRM().