VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix C — Programming with VISA/C# : Example 7 – Output a Bitmap
 
Example 7 – Output a Bitmap
Example 7 – Code Listing
using System;
using System.Text;
using System.IO;
using NationalInstruments.VisaNS;
using System.Threading;
 
namespace Example6
{
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;
 
byte[] 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 3 seconds (3000 ms)
mbSession.Timeout = 3000;
mbSession.TerminationCharacter = 0x0a;
mbSession.TerminationCharacterEnabled = false;
mbSession.SendEndEnabled = true;
 
//We expect about 534k bytes
//BMP files can be rather large so set up the buffer
//size to read it all at once.
mbSession.DefaultBufferSize = 600000;
testClear(mbSession);
 
//Set the Language to LIGHT,
mbSession.Write("LANG LIGHT\n");
//set color on white and output bitmap over gpib
mbSession.Write("BMPC;OBMP\n");
responseArray = testStatus_ReadArbBinaryByte(mbSession);
 
//Send results to a new Binary file
BinaryWriter output = new BinaryWriter(File.Open("VS.bmp", FileMode.Create));
output.Write(responseArray);
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 byte[] testStatus_ReadArbBinaryByte(MessageBasedSession mbSession)
{
//These are the bits to check
int b2 = 4, //Error Queue is not empty
b4 = 16; //MAV = Message Available
byte[] responsebytes = null;
byte[] 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. But we still want to
//strip off the arb block header.
responsebytes = mbSession.ReadByteArray();
replybytes = arbToByte(responsebytes);
 
}
mbSession.Write("*CLS\n");
return replybytes;
}
 
private static void testClear(MessageBasedSession mbSession)
{
mbSession.Write("*CLS\n");
}
 
//Convert Arb Block Binary Data to Byte array
//This function reads the arbitrary block header and then grabs and
//returns the byte array data.
//We continue to use the MemoryStream and BinaryReader classes.
private static byte[] arbToByte(byte[] responseBytes)
{
int i = 0;
byte[] 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(byte);
//resize the response array
dReturn = new byte[dataCount];
//set the index of the start of the data
i += count1;
 
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.ReadByte();
}
}
return dReturn;
}
}
}
 
Example 7 – Discussion
Output should be in \Example6\Bin\Debug\VS.bmp.
Example 6 – Output Results as BMP File
Results from Example 6.
 
Bitmap File Properties
Properties of the bitmap file in this example.