Example of Message-Based Communication

NI-VISA

Example of Message-Based Communication

Serial, GPIB, Ethernet, and VXI systems all have a definition of message-based communication. In GPIB, serial, and Ethernet, the messages are inherent in the design of the bus itself. For VXI, the messages actually are sent via a protocol known as word serial, which is based on register communication. In either case, the end result is sending or receiving strings.

The following example shows the basic steps in any VISA program.

Example

#include "visa.h"

#define MAX_CNT 200

int main(void)
{

ViStatus
ViSession
ViUInt32
ViChar
status;
defaultRM, instr;
retCount;
buffer[MAX_CNT];
/* For checking errors */
/* Communication channels */
/* Return count from string I/O */
/* 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 GPIB Device at Primary Addr 1 */
/* NOTE: For simplicity, we will not show error checking */
status = viOpen(defaultRM, "GPIB0::1::INSTR", VI_NULL, VI_NULL, &instr);

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

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

/* Your code should process the data */

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

}

Visual Basic Example

Example Discussion