VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix B — Programming with LabWindows/CVI : Example 3 – Error Checking
 
Example 3 – 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”. But the third string is not a valid GPIB command and the instrument reports this. The code snippet is below.
 
//==============================================================================
//
// Title: Example 3
// Purpose: Error Checking
//
// Created on: 11/30/2011 by David Judge.
// Copyright: Anritsu. All Rights Reserved.
//
//==============================================================================
 
//==============================================================================
// Include files
#include <ansi_c.h>
#include <visa.h>
#include <userint.h>
#include "au464x.h"
 
ViSession session;
ViStatus checkErr (ViStatus status);
#define CHECKERR(fCal) \
if (au464x_status = checkErr((fCal)), au464x_status < VI_SUCCESS) \
goto Error; else
 
 
int main (int argc, char *argv[])
{
ViStatus status;
ViUInt32 read_count;
ViStatus au464x_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(au464x_init ("VectorStar_Test", VI_TRUE, VI_FALSE, &session));
//Use Native VNA Error Checking
CHECKERR(au464x_writeInstrData (session, "LANG NATIVE"));
//First send a known good command
CHECKERR(au464x_writeInstrData (session, "*IDN?"));
CHECKERR(viRead (session, (ViPBuf)l_buffer, 50, &read_count));
//Send an OID - also a good command
CHECKERR(au464x_writeInstrData (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(au464x_writeInstrData (session, "ABC"));
au464x_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];
ViUInt32 read_count;
ViChar error_buffer [1024];
ViInt32 my_error_code = 0;
ViInt32* error_code = &my_error_code;
ViUInt16 stb;
ViUInt16 VNA_ERROR = 4; //This means there is an error
ViUInt16 VNA_ERROR_MSG = 16; //This means there is a message in the error buffer
if (status >= 0)
viReadSTB (session, &stb);
// Check if stb & VNA_ERROR is set
// Add one more check in this function to look at the status byte for
// something in the error queue (bit 2) and also Message Available (bit 4).
if (status < VI_SUCCESS | | (((stb & VNA_ERROR) > 0) && ((stb & VNA_ERROR_MSG) > 0)))
{
au464x_writeInstrData (session, ":SYST:ERR?");
viRead (session, (ViPBuf)error_message, 256, &read_count);
SetWaitCursor (0);
sprintf (error_buffer, "Instrument Error: %s\n", error_message);
printf ("%s\n", error_buffer);
au464x_writeInstrData (session, "*CLS");
}
return status;
}
 
 
Example 3 – Service Request Status Register – VectorStar Status Register
The Service Request Status Register. It is slightly changed from Lightning to VectorStar. If LANG LIGHT is set then the Lightning configuration of the Status Register is used. In this example we used “LANG NATIVE” so the VectorStar status register is used. The code checks b2 to see if the error queue is not empty and also b4 to see if a message is available.
 
Example 3 – Error Checking
We catch the command error after sending the bogus “ABC” command and report the message (Faulty program mnemonic syntax).