VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix C — Programming with VISA/C# : Example 2 ─ Session Parameters and Status Checking
 
Example 2 ─ Session Parameters and Status Checking
Example 2 – Code Listing
using System;
using NationalInstruments.VisaNS;
using System.Threading;
 
namespace Example2
{
class Program
{
static void Main(string[] args)
{
//VXI-11 Connection string
string sAddress = "TCPIP0::10.0.1.196::INSTR";
 
//The VNA uses a message based session.
MessageBasedSession mbSession = null;
//But we'll just open a generic Session first
Session mySession = null;
try
{
//Open a Session to the VNA.
mySession = ResourceManager.GetLocalManager().Open(sAddress);
//Cast this to a message based session.
mbSession = (MessageBasedSession)mySession;
 
//Now set some Session Parameters.
//Timeout to 1 second (1000 ms).
mbSession.Timeout = 1000;
//Session Parameters were all set to default values in Example 1.
//Here we specifically set them.
 
//Use newline (\n or 0x0a) as termination character.
mbSession.TerminationCharacter = 0x0a;
//Terminate reads if the termination character is spotted.
mbSession.TerminationCharacterEnabled = false;
//Assert an END during the transfer of the last byte of data.
mbSession.SendEndEnabled = true;
//Send a *CLS
testClear(mbSession);
 
//Now try a few commands - first *IDN?
//Note that the \n is not included in these strings
//LANG NATIVE below allows us to work with the
//VectorStar Status Register setup in this example.
mbSession.Write("LANG NATIVE");
mbSession.Write("*IDN?");
Console.WriteLine("Response to *IDN?:");
testStatus_Read(mbSession);
 
//OID returns an instrument ID string
mbSession.Write("OID");
Console.WriteLine("Response to OID:");
testStatus_Read(mbSession);
 
//*OPT? returns the installed instrument options
mbSession.Write("*OPT?");
Console.WriteLine("Response to *OPT?:");
testStatus_Read(mbSession);
 
//This command doesn't exist, check that we catch it
//This command should be caught in testStatus().
mbSession.Write("ABC");
Console.WriteLine("Test ABC:");
testStatus(mbSession);
 
//This command is OK, no response is expected
mbSession.Write("CH3");
Console.WriteLine("Test CH3:");
testStatus(mbSession);
//Return to Local Control
mbSession.Write("RTL\n");
//Close the Session
mbSession.Dispose();
}
catch (VisaException v_exp)
{
Console.WriteLine("Visa caught an error!!");
Console.WriteLine(v_exp.Message);
}
catch (Exception exp)
{
Console.WriteLine("Something didn't work!!");
Console.WriteLine(exp.Message);
}
 
keepConsoleUp();
}
 
private static void testClear(MessageBasedSession mbSession)
{
mbSession.Write("*CLS\n");
}
 
//This function tests the Service Request Status Register
//and does a read if MAV is set.
//It prints an error message if any error is reported.
private static void testStatus_Read(MessageBasedSession mbSession)
{
//These are the bits to check
int b2 = 4, //Error Queue is not empty
b4 = 16; //MAV = Message Available
 
//Read the Status Byte of Service Request Status Register
StatusByteFlags sb = mbSession.ReadStatusByte();
string responseString = null;
 
 
while (((int)sb & (b2 + b4)) == 0)
//Before we start the read we want to see the MAV bit set (b4).
//But if an error occurs (b2) we’ll report it.
//We could add another test to make sure we don’t loop here forever.
{
Thread.Sleep(10);
sb = mbSession.ReadStatusByte();
}
if (((int)sb & b2) != 0)
{
responseString = mbSession.Query("OGE\n");
Console.WriteLine("Error Queue: " + responseString);
 
}
else if (((int)sb & b4) != 0)
{
responseString = mbSession.ReadString();
Console.WriteLine(responseString);
}
mbSession.Write("*CLS\n");
}
 
//This function checks if the GPIB command raises an error.
//This function tests the Service Request Status Register.
//It prints an error message if any error is reported.
private static void testStatus(MessageBasedSession mbSession)
{
//These are the bits to check
int b2 = 4; //Error Queue is not empty
 
//Read the Status Byte of Service Request Status Register
//wait 50 ms
Thread.Sleep(50);
 
//then check status
StatusByteFlags sb = mbSession.ReadStatusByte();
string responseString = null;
 
if (((int)sb & b2) != 0)
{
responseString = mbSession.Query("OGE\n");
Console.WriteLine("Error Queue: " + responseString);
 
}
else
{
Console.WriteLine("OK");
}
 
mbSession.Write("*CLS\n");
}
private static void keepConsoleUp()
{
Console.WriteLine("");
Console.WriteLine("Enter to Continue");
Console.ReadLine();
}
 
}
}
Example 2 – Discussion
1. In this example we check the Status Byte prior to reading the response.
The Status Byte is the Service Request Status Register and is discussed elsewhere.
We set the LANG to NATIVE to use the VectorStar Status Register Configuration.
In general we expect to get a Message Available (MAV) response when data is available to read.
Otherwise we expect something to be in the Error Queue (ERRQ).
Notice when the code send an unknown command “ABC” and the testStatus() function catches it and outputs the message in the Error Queue.
Example 2 – Service Request 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.
 
2. The expected output is shown below.
Example 2 – Faulty Command Message
Note the error message after sending the “ABC” fake command.