VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix C — Programming with VISA/C# : Example 6 – Output S2P File
 
Example 6 – Output S2P File
Example 6 – Code Listing
using System;
using System.Text;
using System.IO;
using NationalInstruments.VisaNS;
using System.Threading;
 
namespace Example7
{
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[] splitN = 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 3 second (1000 ms)
mbSession.Timeout = 3000;
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);
 
//Set the Language to NATIVE
mbSession.Write("LANG NATIVE\n");
mbSession.Write(":SENSE:SWEEP:POINTS 25\n");
//set format to Hz and Re/Im
//We set the format of the S2P file parameters to
//Frequency in Hz and data in Real/Imag format.
mbSession.Write(":FORM:SNP:FREQ HZ\n");
mbSession.Write(":FORM:SNP:PAR REIM\n");
//Output S2P file over GPIB
mbSession.Write("TRS;WFS;OS2P\n");
responseStringN = testStatus_ReadArb(mbSession);
//the results are delimited by a newline (\n)
//Here the SplitN string array will hold each line of the S2P file.
splitN = responseStringN.Split('\n');
 
//Send results to a file
StreamWriter output = new StreamWriter("VS_S2P.s2p");
foreach (string s in splitN)
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 6 – Discussion
Expected output is in \Example6\Bin\Debug\VS_S2P.s2p.
Example 6 – S2P File to PC
Transfer of an S2P file to the PC.