VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix C — Programming with VISA/C# : Example 4 ─ Acquiring ASCII Data, Arbitrary Block
 
Example 4 ─ Acquiring ASCII Data, Arbitrary Block
Example 4 – Code Listing
using System;
using System.Text;
using System.IO;
using NationalInstruments.VisaNS;
using System.Threading;
 
namespace Example4
{
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 responseStringN = null;
string responseStringL = null;
string[] splitN = null;
string[] splitL = 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'll limit the number of data points to keep buffer small
mbSession.DefaultBufferSize = 5000;
testClear(mbSession);
 
//This next code group of code sets the sweep to 5 points,
//then makes trace1 active, sets to Smith Chart,
//does a trigger sweep and waits for sweep to finish.
//Then it sets output format to ASCII and asks for Final Data.
//Set the Language to NATIVE
mbSession.Write("LANG NATIVE\n");
mbSession.Write(":SENSE:SWEEP:POINTS 5\n");
mbSession.Write("CH1;SMI;\n");
mbSession.Write("TRS;WFS;HLD\n");
mbSession.Write("FMA;OFD\n");
responseStringN = testStatus_ReadArb(mbSession);
//the results are delimited by a newline (\n)
splitN = responseStringN.Split('\n');
 
//Set the Language to LIGHTNING
//Then we set the LANG LIGHT to see the subtle differences in
//how data is transferred when VectorStar is in Lightning mode.
mbSession.Write("LANG LIGHT\n");
mbSession.Write("FMA;OFD\n");
responseStringL = testStatus_ReadArb(mbSession);
//the results are delimited by a comma
splitL = responseStringL.Split(',');
 
//Send results to a file
StreamWriter output = new StreamWriter("OFD.txt");
output.WriteLine("Native Results");
foreach (string s in splitN)
output.WriteLine(s);
output.WriteLine("Lightning Results");
foreach (string s in splitL)
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);
}
}
 
private static void testClear(MessageBasedSession mbSession)
{
mbSession.Write("*CLS\n");
}
 
private static string testStatus_ReadArb(MessageBasedSession mbSession)
{
//These are the bits to check.
int b2 = 4, //Error Queue is not empty
b4 = 16; //MAV = Message Available
string responseString = null;
string replyString = null;
string errorString = null;
 
//Read the Status Byte of Service Request Status Register.
StatusByteFlags sb = mbSession.ReadStatusByte();
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();
replyString = stripHeader(responseString);
}
mbSession.Write("*CLS\n");
return replyString;
}
 
 
private static string stripHeader(string responseString)
{
int i = 0;
string strReturn = null;
if (responseString[i++] == '#')
{
//Header is ASCII, get 2nd byte and convert to int.
StringBuilder sCount = new StringBuilder(responseString, i++, 1, 1);
int count1 = int.Parse(sCount.ToString());
//now read the bytecount string and convert to int
StringBuilder sBytes = new StringBuilder(responseString,i,count1,count1);
int count2 = int.Parse(sBytes.ToString());
//set the index of the start of the data
i += count1;
//return the string with the header stripped off
strReturn = responseString.Substring(i);
}
return strReturn;
}
}
}
 
Example 4 – Discussion
1. Output result values are the same for Native mode and Lightning mode (since we put the unit in HOLD) but the delimiters are different.
Native mode uses a comma to separate the two values at each frequency and a newline (\n) to separate pairs of values while Lightning mode separates each value with a comma. The display was set for Smith Chart, impedance.
Example 4 – Listing and Graph Outputs
OFD command output on left. Resultant Smith Chart on right.
2. The OFD command returns an arbitrary block containing either ASCII or Binary data depending on the currently selected format.
In this example we select ASCII using the FMA command.
The Arbitrary block header is stripped off with the stripHeader(string responseString) function.
The next example shows how to get binary data.