Programming instructions
Programming Examples
RS-232 Programming Examples
Chapter 2 95
Interface Check Using VISA and C
This program uses VISA library functions to communicate with the signal generator. The
program verifies that the RS-232 connections and interface are functional. In this example
the COM2 port is used. The serial port is referred to in the VISA library as ‘ASRL1’ or ‘ASRL2’
depending on the computer serial port you are using. Launch Microsoft Visual C++, add the
required files, and enter the following code into the .cpp source file.
The following program example is available on the ESG Documentation CD-ROM as
rs232ex1.cpp.
//****************************************************************************************
// PROGRAM NAME: rs232ex1.cpp
//
// PROGRAM DESCRIPTION: This code example uses the RS-232 serial interface to
// control the signal generator.
//
// Connect the computer to the signal generator using an RS-232 serial cable.
// The user is asked to set the signal generator for a 0 dBm power level
// A reset command *RST is sent to the signal generator via the RS-232
// interface and the power level will reset to the -135 dBm level.The default
// attributes e.g. 9600 baud, no parity, 8 data bits,1 stop bit are used.
// These attributes can be changed using VISA functions.
//
// IMPORTANT: Set the signal generator BAUD rate to 9600 for this test
//****************************************************************************************
#include <visa.h>
#include <stdio.h>
#include "StdAfx.h"
#include <stdlib.h>
#include <conio.h>
void main ()
{
int baud=9600; // Set baud rate to 9600
printf("Manually set the signal generator power level to 0 dBm\n");
printf("\n");
printf("Press any key to continue\n");
getch();
printf("\n");
ViSession defaultRM, vi; // Declares a variable of type ViSession
// for instrument communication on COM 2 port
ViStatus viStatus = 0;
// Opens session to RS-232 device at serial port 2
viStatus=viOpenDefaultRM(&defaultRM);
viStatus=viOpen(defaultRM, "ASRL2::INSTR", VI_NULL, VI_NULL, &vi);