Chapter 2 45
Programming Examples
GPIB Programming Examples
#include <visa.h>
#include <iostream>
#include <stdlib.h>
#include <conio.h>
void main ()
{
 ViSession   defaultRM, vi;      // Declares variables of type ViSession
                                // for instrument communication
ViStatus viStatus = 0;          // Declares a variable of type ViStatus
                                // for GPIB verifications
char rdBuffer [256];            // Declare variable to hold string data
int num;                        // Declare variable to hold integer data
viStatus=viOpenDefaultRM(&defaultRM);     // Initialize VISA system
                               // Open session to GPIB device at address 19
viStatus=viOpen(defaultRM, "GPIB::19::INSTR", VI_NULL, VI_NULL, &vi);
if(viStatus){                   // If problems then prompt user
printf("Could not open ViSession!\n");
printf("Check instruments and connections\n");
printf("\n");
exit(0);}
viPrintf(vi, "*RST\n");         // Reset the signal generator 
viPrintf(vi, "FREQ 500 kHz\n"); // Set the source CW frequency for 500 kHz
viPrintf(vi, "FREQ:CW?\n");     // Query the CW frequency
viScanf(vi, "%t", rdBuffer);    // Read signal generator response 
printf("Source CW frequency is : %s\n", rdBuffer);  // Print the frequency
viPrintf(vi, "POW:AMPL -2.3 dBm\n");  // Set the power level to -2.3 dBm
viPrintf(vi, "POW:AMPL?\n");    // Query the power level
viScanf(vi, "%t", rdBuffer);    // Read the response into rdBuffer
printf("Source power (dBm) is : %s\n", rdBuffer); // Print the power level