Example VISA Message-Based Application

NI-VISA

Example VISA Message-Based Application

The following is an example VISA application using message-based communication.

Example

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

#include "visa.h"

int main(void)
{

ViSession
ViUInt32
ViChar
ViChar
ViStatus
defaultRM, instr;
retCount;
idnResult[72];
resultBuffer[256];
status;
/* Open Default Resource Manager */
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 */
viOpen(defaultRM, "GPIB::1::INSTR", VI_NULL, VI_NULL, &instr);
/* Initialize the timeout attribute to 10 s */
viSetAttribute(instr, VI_ATTR_TMO_VALUE, 10000);
/* Set termination character to carriage return (\r=0x0D) */
viSetAttribute(instr, VI_ATTR_TERMCHAR, 0x0D);
viSetAttribute(instr, VI_ATTR_TERMCHAR_EN, VI_TRUE);
/* Don't assert END on the last byte */
viSetAttribute(instr, VI_ATTR_SEND_END_EN, VI_FALSE);
/* Clear the device */
viClear(instr);
/* Request the IEEE 488.2 identification information */
viWrite(instr, "*IDN?\n", 6, &retCount);
viRead(instr, idnResult, 72, &retCount);

/* Use idnResult and retCount to parse device info */

/* Trigger the device for an instrument reading */
viAssertTrigger(instr, VI_TRIG_PROT_DEFAULT);
/* Receive results */
viRead(instr, resultBuffer, 256, &retCount);
/* Close sessions */
viClose(instr);
viClose(defaultRM);
return 0;

}