Example 5 – Acquiring Binary Data, Arbitrary Block
Example 5 – Code Listing
using System;
using System.Text;
using System.IO;
using NationalInstruments.VisaNS;
using System.Threading;
 
namespace Example5
{
    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;
 
            double[] responseArray = 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);
 
                //Set the Language to NATIVE
                //This code group sets the sweep to 8 points, 
                //then makes trace2 active, sets to Mag-Phase, does a trigger sweep,
                //and waits for sweep to finish. 
                //Then it sets output format to 64 bit binary and 
                //least significant bit first (little endian) and
                //asks for Final Data.
                mbSession.Write("LANG NATIVE\n");
                mbSession.Write("CH2;MPH;\n");
                mbSession.Write(":SENSE:SWEEP:POINTS 8\n");
                mbSession.Write("TRS;WFS;HLD\n");
                mbSession.Write("LSB;FMB;OFD\n");
                responseArray = testStatus_ReadArbBinaryDouble(mbSession);
                //responseArray[] will hold the 16 readings from the VNA. 
                //They will be mag1, phase1, mag2, phase2, etc.
                
                //Send results to a file
                StreamWriter output = new StreamWriter("OFD.txt");
                output.WriteLine("Native Results (Mag-Phase)");
                foreach (double d in responseArray)
                    output.WriteLine(d);
                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 double[] testStatus_ReadArbBinaryDouble(MessageBasedSession mbSession)
        {
            //These are the bits to check
            int b2 = 4,     //Error Queue is not empty
                b4 = 16;    //MAV = Message Available
            byte[] responsebytes = null;
            double[] replybytes = 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)
            {
                //Here we use the ReadByteArray() function to read the 
                //binary data into a byte array. 
                //Then we convert the bytes to doubles.
                responsebytes = mbSession.ReadByteArray();
                replybytes = arbToDouble(responsebytes);
 
            }
            mbSession.Write("*CLS\n");
            return replybytes;
        }
 
        private static void testClear(MessageBasedSession mbSession)
        {
            mbSession.Write("*CLS\n");
        }
 
        //Here we convert Arb Block Binary Data to a double array
        //This function reads the arbitrary block header and then converts
        //the byte array into a double array. Every 8 bytes is converted to
        //a double. We use a MemoryStream and BinaryReader to do the conversion.
        private static double[] arbToDouble(byte[] responseBytes)
        {
            int i = 0;
            double[] dReturn = null;
 
            //Arbitrary Block should start with a # 
            if (responseBytes[i++] == '#')
            {
                //Header is ASCII, get 2nd byte and convert to int
                string sCount = ASCIIEncoding.ASCII.GetString(responseBytes, i++, 1);
                int count1 = int.Parse(sCount);
                //now read the bytecount string and convert to int
                string sBytes = ASCIIEncoding.ASCII.GetString(responseBytes, i, count1);
                int count2 = int.Parse(sBytes);
                //the number of doubles is the #bytes/sizeof(double) 
                int dataCount = count2 / sizeof(double);
                //resize the response array
                dReturn = new double[dataCount];
                //set the index of the start of the data
                i += count1;
 
                //There are many ways to convert a byte array to a double array
                MemoryStream stream = new MemoryStream(responseBytes, i, count2);
                //BinaryReader reads this data type in little-endian format
                //So we must use the LSB mnemonic when acquiring the data
                BinaryReader reader = new BinaryReader(stream);
                for (int ii = 0; ii < dataCount; ii++)
                {
                    dReturn[ii] = reader.ReadDouble();
                }
             }
            return dReturn;
        }
    }
}
 
Example 5 – Discussion
Output file from this program should be in \Example5\bin\Debug\OFD.txt.