VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix D — Programming Basics with Legacy Software : LabWindows/CVI Programming Examples
 
LabWindows/CVI Programming Examples
The examples in this section demonstrate the use of the au37xxx CVI driver for controlling the MS4640A. The following few steps are necessary before getting started with programming.
1. Set up communications to the MS4640A using VXI-11 (TCP/IP) by noting the IP Address of the VNA and set up a resource (a connection string).
MS4640A IP Address
2. Launch NI-MAX and create a new resource. The programs will reference this resource rather than a specific address.
Measurement and Automation Explorer (MAX)
3. Select the VISA TCP/IP Resource.
Creating a New Resource Dialog
4. If the controlling PC and the VNA are on the same local sub-network (this is usually true if the first 3 numbers in the IP address are the same – for example, 10.0.1.x in this case), then you can Auto-detect the VNA. Otherwise, you need to manually enter the IP address.
Create New VISA TCP/IP Resource Dialog
5. Select the detected instrument.
Create New VISA TCP/IP Resource Dialog
6. Give the instrument an alias (the VectorStar_Test alias is used in LabWindows/CVI).
Create New VISA TCP/IP Resource Dialog
7. Note that the VISA connection string has been replaced with the VISA alias.
Measurement and Automation Explorer Configuration
8. In LabWindows/CVI, create a new Project from Template.
 
9. Use the Command-line Application Template.
 
10. Set up the CVI to load the Anritsu driver automatically every time CVI starts up in:
Library | Customize to have CVI load the driver into our user Library every time CVI starts. This is a preferable way to have access to the driver.
Customizing CVI
11. Browse to find the au37xxx.fp file.
Customize Library Menu
12. Add the Function Tree (“.fp”) file.
Selecting File
13. The driver is now in the Libraries folder.
Driver Library
14. Copy the driver DLL to someplace on the on the system path. If the system path is not known or a custom path is not important, copy the file to C:\Windows\System32.
Copying Driver DLL
15. Copy the DLL to C:\Windows\System32. This step is necessary (the LabWindows/CVI documentation explains more about copying the DLL file).
Copying Driver DLL
Example 0 – Opening a Session
This first example opens a communication session to the VNA and then uses two of the driver VIs to get some information about the VNA.
// Include files
#include <ansi_c.h>
#include "au37xxx.h"

int main (int argc, char *argv[])
{
ViSession session;
ViStatus status;
ViChar d[256];
ViChar d1[256];

printf("Example 0: Connection and Revision Query\n\n");
//status = au37xxx_init ("TCPIP0::10.0.1.196::INSTR", VI_TRUE, VI_FALSE, &session);
status = au37xxx_init ("VectorStar_Test", VI_TRUE, VI_FALSE, &session);
status = au37xxx_revision_query (session, d,d1);
au37xxx_close(session);
printf("Driver Version: %s\n",d);
printf("Firmware Version: %s\n",d1);

printf("\n\nHit return to exit:");
getc(stdin);

return 0;
}
 
The results of running Example 0 are shown below. The au37xxx_revision_query() function returns two strings. The first is the version of the driver and the second is the version of firmware on the VNA.
Example 0
Example 1 – Sending the *IDN? Command and Displaying Results
The previous example used only driver functions to get some information from the VNA. The GPIB command, “*IDN?” returns the Manufacturer, Model #, Serial Number and Firmware Version. This command was used previously in the Anritsu GPIB, USB, VXI-11, and TCP/IP Exerciser. In this example, the “*IDN?” command is directly issued and then the different parts of the response string are parsed.
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else

int main (int argc, char *argv[])
{

ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
ViChar l_buffer[50];
ViChar* p2Manf = NULL;
ViChar* p2Model = NULL;
ViChar* p2Ser = NULL;
ViChar* p2Firm = NULL;

printf("Example 1: Using the *IDN? Query\n\n");
CHECKERR(au37xxx_init ("VectorStar_Test", VI_TRUE, VI_FALSE, &session));
CHECKERR(au37xxx_write (session, "*IDN?"));
CHECKERR(viRead (session, (ViPBuf)l_buffer, 50, &read_count));
au37xxx_close(session);
p2Manf = strtok(l_buffer, ",");
p2Model = strtok(NULL, ",");
p2Ser = strtok(NULL, ",");
p2Firm = strtok(NULL, "\n");

printf("Manufacturer: %s\nModel: %s\nSer#: %s\nFirmware: %s\n",
p2Manf,p2Model,p2Ser,p2Firm);

printf("\n\nHit return to exit:");
getc(stdin);

return 0;

Error:
printf("\n\nDetected an Error--Hit return to exit:");
getc(stdin);

return 0;
}

ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];

if (status < VI_SUCCESS)
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
MessagePopup ("Error", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
MessagePopup ("Error", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
The output from Example 1 is shown below. The au37xxx_write() function is used to directly send GPIB commands. The VISA function viRead() is then used to read the results.
Example 1
Example 2 – Error Checking
This example shows that if an invalid GPIB string is sent to the VNA then the CHECKERR macro catches the error and displays the error message from the VNA. Here we send two valid strings: “*IDN?” and then “OID”. Note that the third string is not a valid GPIB command and the instrument reports this.
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else


int main (int argc, char *argv[])
{

ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
ViChar l_buffer[50];
ViChar* p2Manf = NULL;
ViChar* p2Model = NULL;
ViChar* p2Ser = NULL;
ViChar* p2Firm = NULL;

printf("Example 2: Testing for Errors\n\n");
CHECKERR(au37xxx_init ("VectorStar_Test", VI_TRUE, VI_FALSE, &session));

//Use Native VNA Error Checking
CHECKERR(au37xxx_write (session, "LANG NATIVE"));

//First send a known good command
CHECKERR(au37xxx_write (session, "*IDN?"));
CHECKERR(viRead (session, (ViPBuf)l_buffer, 50, &read_count));

//Send an OID - also a good command
CHECKERR(au37xxx_write (session, "OID"));
CHECKERR(viRead (session, (ViPBuf)l_buffer, 50, &read_count));

//This command is not a valid VNA command and should generate an error
CHECKERR(au37xxx_write (session, "ABC"));
au37xxx_close(session);

printf("\n\nHit return to exit:");
getc(stdin);

return 0;

Error:
printf("\n\nDetected an Error--Hit return to exit:");
getc(stdin);

return 0;
}


ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4;

if (status >= 0)
viReadSTB (session, &stb);

//check if stb & VNA_ERROR is set

if ((status < VI_SUCCESS) | | ((stb & VNA_ERROR) > 0))
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
printf ("%s\n", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
The Service Request Status Register is shown below and is slightly changed from Lightning to the MS4640A VNA. If LANG LIGHT is set, then the Lightning configuration of the Status Register is used. In this example the “LANG NATIVE” command is sent to use the MS4640A status register. The code checks b2 to see if the error queue is not empty.
MS4640A Service Request Status Register
The command error is caught after sending the erroneous “ABC” command and reports the message, “Faulty program mnemonic syntax.”
Example 2
Example 3 – Sending Data to a File with the LIST Command
The Lightning commands “FMT1;LIST” are sent to get the full list of commands supported by the MS4640A. The sting returned is an ASCII arbitrary block and the au37xxx_readAsciiARBBlock() function is used to strip off the arbitrary block header. The results are then sent to a file.
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include <formatio.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else

int main (int argc, char *argv[])
{

ViInt32 retCount;

ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
int fileHandle;
static ViChar readBuffer[100000];

CHECKERR(au37xxx_init ("VectorStar_Test", VI_FALSE, VI_FALSE, &session));
CHECKERR(au37xxx_write(session,"FMT1;LIST"));

CHECKERR(au37xxx_readAsciiARBBlock(session,100000,readBuffer,&retCount));
au37xxx_close(session);

fileHandle = OpenFile (".\\commands.txt", VAL_WRITE_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
WriteFile (fileHandle, readBuffer, retCount);
CloseFile (fileHandle);

Error:
printf("\n\nHit return to exit:");
getc(stdin);
return 0;
}

ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4;

if (status >= 0)
viReadSTB (session, &stb);

//check if stb & VNA_ERROR is set

if ((status < VI_SUCCESS) | | ((stb & VNA_ERROR) > 0))
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
printf ("%s\n", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
The list of all commands supported by the MS4640A are listed in the commands.txt file.
MS4640A VNA Programming Commands List
Use the Anritsu GPIB, USB, VXI-11 Exerciser to get more help on any command. Help will tell you what type of command (Native, Lightning, HP8510) and provides syntax.
Anritsu GPIB, USB, VXI-11, and TCP/IP Exerciser
Example 4 – Acquiring Trace Data
In this example, the final data from Trace 2, which is set to Log magnitude and Phase data, is acquired programmatically. The data comes out in a one dimensional, interleaved array. The array must be parsed to get the log magnitude and phase data into two separate arrays.
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include <formatio.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else

int main (int argc, char *argv[])
{

ViInt32 retCount;
ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
FILE* fp;
ViChar readBuffer[500000];
ViReal64 fdata[201];
ViReal64 chanData[402];
ViReal64 lmData[201];
ViReal64 phaseData[201];
int i, ii = 0;

CHECKERR(au37xxx_init ("VectorStar_Test", VI_FALSE, VI_FALSE, &session));
CHECKERR(au37xxx_configureChannel (session, 2));
CHECKERR(au37xxx_configureSweep (session, 201, AU37XXX_SWEEP_NORMAL));
CHECKERR(au37xxx_readFrequencyValues (session, 201,fdata,&retCount));
CHECKERR(au37xxx_readChannelData (session, 2, AU37XXX_DATA_FINAL, 402,
chanData, &retCount));

au37xxx_close(session);

fp = fopen(".\\chanData.txt","w") ;

for (i=0;i<201;i++)
{
lmData[i] = chanData[ii++];
phaseData[i] = chanData[ii++];
fprintf(fp,"%e %10.4f\t%10.4f\n",fdata[i],lmData[i],phaseData[i]);
}

fclose(fp);

Error:
printf("\n\nHit return to exit:");
getc(stdin);
return 0;
}

ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4;

if (status >= 0)
viReadSTB (session, &stb);

//check if stb & VNA_ERROR is set

if ((status < VI_SUCCESS) | | ((stb & VNA_ERROR) > 0))
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
printf ("%s\n", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
The data is printed out into a three-column format: Frequency (Hz), Log Magnitude, and Phase (note that the data is simulated).
Simulated Trace Data
Example 5 – Smith Chart Data
This example is similar to the previous example, except the instrument is not reset, which sets up smith charts on trace 1 and 4.
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include <formatio.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else

int main (int argc, char *argv[])
{
ViInt32 retCount;
ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
FILE* fp;
ViChar readBuffer[500000];
ViReal64 fdata[201];
ViReal64 chanData[402];
ViReal64 impData[201];
ViReal64 reactData[201];
int i, ii = 0;

CHECKERR(au37xxx_init ("VectorStar_Test", VI_FALSE, VI_TRUE, &session));
CHECKERR(au37xxx_configureChannel (session, 1));
CHECKERR(au37xxx_configureSweep (session, 201, AU37XXX_SWEEP_NORMAL));
CHECKERR(au37xxx_readFrequencyValues (session, 201,fdata,&retCount));
CHECKERR(au37xxx_readChannelData (session, 1, AU37XXX_DATA_FINAL, 402,
chanData, &retCount));

au37xxx_close(session);

fp = fopen(".\\chanData.txt","w") ;
for (i=0;i<201;i++)
{
impData[i] = chanData[ii++];
reactData[i] = chanData[ii++];
fprintf(fp,"%e %10.4f\t%10.4f\n",fdata[i],impData[i],reactData[i]);
}

fclose(fp);

Error:
printf("\n\nHit return to exit:");
getc(stdin);
return 0;
}

ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4;

if (status >= 0)
viReadSTB (session, &stb);

//check if stb & VNA_ERROR is set

if ((status < VI_SUCCESS) | | ((stb & VNA_ERROR) > 0))
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
printf ("%s\n", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
The impedance/reactance data from Trace 1 (Smith Chart) is parsed into an ASCII file with a three-column format: Frequency (Hz), Impedance, and Reactance (note that the data is simulated).
Simulated Trace Data
Example 5 is modified to use the built-in Smith chart control. The following program is adapted from the Smith Chart Demo in the samples\apps\smithchart directory. The smith chart fp, found in toolslib\toolbox\smith.fp. is used. By default, Trace1 is set to output impedance values. Most Smith chart controls actually take a normalized impedance (normalized to 1), so the impedance/reactance pairs are divided by 50 ohms to get normalized smith chart data.
ViInt32 retCount;
ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;

ViReal64 fdata[201];
ViReal64 chanData[402];
ViReal64 impData[201];
ViReal64 reactData[201];
int i, ii = 0;

CHECKERR(au37xxx_init ("VectorStar_Test", VI_FALSE, VI_FALSE, &session));
CHECKERR(au37xxx_configureChannel (session, 1));
CHECKERR(au37xxx_configureSweep (session, 201, AU37XXX_SWEEP_NORMAL));
CHECKERR(au37xxx_readFrequencyValues (session, 201,fdata,&retCount));
CHECKERR(au37xxx_readChannelData (session, 1, AU37XXX_DATA_FINAL, 402,
chanData, &retCount));

au37xxx_close(session);

for (i=0;i<201;i++)
{
impData[i] = chanData[ii++];
reactData[i] = chanData[ii++];
gZ.Real = impData[i]/50.0;
gZ.Im = reactData[i]/50.0;

SMITH_PlotImpedancePoint(panelHandle, PANEL_GRAPH, &gZ,
VAL_SOLID_CIRCLE, IMPEDANCE_COLOR);
}
The Labwindows/CVI smith.fp is used to plot smith chart data.
Smith Chart Demo
Example 6 – Output S2P File
This example uses au37xxx_write() calls to accomplish tasks that the driver can’t perform. Specifically, to send Native MS4640A commands along with Lightning commands to output an S2P file from the VNA to the PC.
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include <formatio.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else

int main (int argc, char *argv[])
{

ViInt32 retCount;

ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
int fileHandle;
ViChar readBuffer[100000];

CHECKERR(au37xxx_init ("VectorStar_Test", VI_FALSE, VI_TRUE, &session));
CHECKERR(au37xxx_write(session,"LANG NATIVE"));
CHECKERR(au37xxx_write(session,":SENSE:SWEEP:POINTS 25"));
CHECKERR(au37xxx_write(session,":FORM:SNP:FREQ HZ"));
CHECKERR(au37xxx_write(session,":FORM:SNP:PAR REIM"));
CHECKERR(au37xxx_write(session,"TRS;WFS;OS2P"));

CHECKERR(au37xxx_readAsciiARBBlock(session,100000,readBuffer,&retCount));
au37xxx_close(session);
fileHandle = OpenFile (".\\dave.s2p", VAL_WRITE_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
WriteFile (fileHandle, readBuffer, retCount);
CloseFile (fileHandle);

Error:
printf("\n\nHit return to exit:");
getc(stdin);
return 0;
}

ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4;

if (status >= 0)
viReadSTB (session, &stb);

//check if stb & VNA_ERROR is set

if ((status < VI_SUCCESS) | | ((stb & VNA_ERROR) > 0))
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
printf ("%s\n", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
The resulting S2P file is transferred to the PC as shown below.
Transfer of an S2P file to the PC
Example 7 – Output BMP File
A similar technique can be used to get the bitmap data to a file as in the previous example. The Lightning commands “BMPC;OBMP” are used to output a bitmap file. BMPC selects color on white as the color scheme, making for better printouts. The au37xxx_readAsciiARBBlock() function is used to strip off the arbitrary block header and place the bitmap data into a file (the header corrupts the bitmap file).
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include <formatio.h>
#include "au37xxx.h"

ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au37xxx_status = checkErr((fCal)), au37xxx_status < VI_SUCCESS) \
goto Error; else

int main (int argc, char *argv[])
{
ViInt32 retCount;
ViStatus status;
ViUInt32 read_count;
ViStatus au37xxx_status = VI_SUCCESS;
int fileHandle;
static ViChar readBuffer[600000];

CHECKERR(au37xxx_init ("VectorStar_Test", VI_FALSE, VI_TRUE, &session));
CHECKERR(au37xxx_write(session,"LANG NATIVE"));
CHECKERR(au37xxx_write(session,"BMPC;OBMP"));

CHECKERR(au37xxx_readAsciiARBBlock(session,600000,readBuffer,&retCount));
au37xxx_close(session);

fileHandle = OpenFile (".\\dave.bmp", VAL_WRITE_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
WriteFile (fileHandle, readBuffer, retCount);
CloseFile (fileHandle);


Error:
printf("\n\nHit return to exit:");
getc(stdin);
return 0;
}


ViStatus checkErr (ViStatus status)
{
ViChar error_message [256];
ViChar error_buffer [1024];
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4;

if (status >= 0)
viReadSTB (session, &stb);

//check if stb & VNA_ERROR is set

if ((status < VI_SUCCESS) | | ((stb & VNA_ERROR) > 0))
{
au37xxx_error_message (session, status, error_message);
sprintf (error_buffer, "Primary Error: 0x%08X, %s\n", status, error_message);
printf ("%s\n", error_buffer);
au37xxx_error_query (session, error_message);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au37xxx_close(session);
session = 0;
}
return status;
}
When the VI runs, a dialog allow the file name to be selected. Make sure to save the file with a “.bmp” extension.
MS4640A VNA Bitmap Display