72 Chapter 2
Programming Examples
LAN Programming Examples
ViStatus status;// Declares a type ViStatus variable
ViSession defaultRM, instr;// Declares a type ViSession variable
ViUInt32 retCount;// Return count for string I/O
ViChar buffer[MAX_COUNT];// Buffer for string I/O
status = viOpenDefaultRM(&defaultRM); // Initialize the system
// Open communication with Serial
// Port 2
status = viOpen(defaultRM, "TPCIP0::19::INSTR", VI_NULL, VI_NULL, &instr);
if(status){ // If problems then prompt user
printf("Could not open ViSession!\n");
printf("Check instruments and connections\n");
printf("\n");
exit(0);}
// Set timeout for 5 seconds
viSetAttribute(instr, VI_ATTR_TMO_VALUE, 5000);
// Ask for sig gen ID string
status = viWrite(instr, (ViBuf)"*IDN?\n", 6, &retCount);
// Read the sig gen response
status = viRead(instr, (ViBuf)buffer, MAX_COUNT, &retCount);
buffer[retCount]= '\0'; // Indicate the end of the string
printf("Signal Generator ID = "); // Print header for ID
printf(buffer); // Print the ID string
printf("\n"); // Print carriage return
// Flush the read buffer
// Set sig gen power to -5dbm
status = viWrite(instr, (ViBuf)"POW:AMPL -5dbm\n", 15, &retCount);
// Query the power level
status = viWrite(instr, (ViBuf)"POW?\n",5,&retCount);