VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix C — Programming with VISA/C# : Example 3 ─ LIST Command
 
Example 3 ─ LIST Command
Example 3 – Code Listing
using System;
using System.IO;
using NationalInstruments.VisaNS;
using System.Threading;
 
namespace Example3
{
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;
 
string responseString = null;
string[] split = null;
 
try
{
//Open a Session to the VNA
mySession = ResourceManager.GetLocalManager().Open(sAddress);
//Cast this to a message based session
mbSession = (MessageBasedSession)mySession;
 
//Timeout to 1 second (1000 ms)
mbSession.Timeout = 1000;
mbSession.TerminationCharacter = 0x0a;
mbSession.TerminationCharacterEnabled = false;
mbSession.SendEndEnabled = true;
 
//We're expected a lot of response data to the LIST command
//We’ll increase the size of the read buffer.
mbSession.DefaultBufferSize = 50000;
testClear(mbSession);
 
//Set the Language to NATIVE and send the LIST command
mbSession.Write("LANG NATIVE\n");
mbSession.Write("LIST\n");
responseString = testStatus_Read(mbSession);
//The results are delimited by a newline (\n)
//The Split() function is handy for many string parsing functions.
split = responseString.Split('\n');
 
//Send results to a file
//Results are written to a file in the same directory as the exe.
StreamWriter output = new StreamWriter("List.txt");
foreach (string s in split)
output.WriteLine(s);
output.Close();
mbSession.Write("RTL\n");
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");
}
private static void keepConsoleUp()
{
Console.WriteLine("");
Console.WriteLine("Enter to Continue");
Console.ReadLine();
}
//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 string 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;
string errorString = null;
 
while (((int)sb & (b2 + b4)) == 0)
{
Thread.Sleep(10);
sb = mbSession.ReadStatusByte();
}
if (((int)sb & b2) != 0)
{
errorString = mbSession.Query("OGE\n");
Console.WriteLine("Error Queue: " + errorString);
 
}
else if (((int)sb & b4) != 0)
{
responseString = mbSession.ReadString();
}
mbSession.Write("*CLS\n");
return responseString;
}
}
}
Example 3 – Discussion
1. This and the next few examples don’t write anything to the Console unless there is an error. Output is sent to file in the same directory of the running program (\Example3\Bin\Debug\List.txt).
Example 3 – VectorStar Command Listing
The List of all commands supported by VectorStar.
 
2. Use LANG LIGHT. Lightning has the same command but is comma delimited:
//Set the Language to LIGHTNING and send the LIST command
mbSession.Write("LANG LIGHT\n");
mbSession.Write("LIST\n");
responseString = testStatus_Read(mbSession);
//the results are delimited by a comma
split = responseString.Split(',');
3. Use WGPIB to get more help on any command.
Example 3 – WGPIB Help Command Description
Help will tell you what type of command (Lightning, Native, 8510) you're asking about and provides syntax.