VectorStar™ MS4640A SeriesMicrowave Vector Network Analyzer : Appendix C — Programming with VISA/C# : Example 1 ─ Open Session and *IDN?
 
Example 1 ─ Open Session and *IDN?
Example 1 – Code Listing
using System;
using NationalInstruments.VisaNS;
 
namespace Example1
{
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;
 
//response string
string responseString = null;
 
try
{
//open a Session to the VNA
mySession = ResourceManager.GetLocalManager().Open(sAddress);
//cast this to a message based session
mbSession = (MessageBasedSession)mySession;
//Send "*IDN?" command
mbSession.Write("*IDN?\n");
//Notice that the Session Write commands are terminated with a “/n”.
//This is the newline character and is not really needed for GPIB,
//USB or VXI-11, but is needed for Sockets.
//It doesn’t cause any problems to include it.
//Read the response
responseString = mbSession.ReadString();
 
//Write to Console
Console.WriteLine("Response to *IDN?:");
Console.WriteLine(responseString);
//Return to Local Control
mbSession.Write("RTL\n");
//Close the Session
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);
Console.WriteLine();
}
 
keepConsoleUp();
// This is here so the Console stays up until the user hits return.
}
 
private static void keepConsoleUp()
{
Console.WriteLine("");
Console.WriteLine("Enter to Continue");
Console.ReadLine();
}
}
}
Example 1 – Discussion
1. The program output shown below.
Example 1 – Console Output
The console output should look something like this if everything worked. The result is Manufacturer, Model, Serial Number, and Firmware Version.
2. If it didn’t work then check that the sAddress string matches your setup. This is the error you’ll get if the address is wrong as shown in the figure below.
Example 1 – Exception Message
Results if the connection string is no good.
The first thing to observe is the VISA connection string. Here are some possible strings:
//VXI-11 Connection string
string sAddress = "TCPIP0::10.0.1.194::INSTR";
 
//GPIB Connection string
string sAddress = "GPIB0::6::INSTR";
 
//USB Connection string (vendor::product::serial_number)
string sAddress = "USB0::0x0B5B::0xFFD0::MS4647A-12345::INSTR";
The beauty of using VISA is that the only thing that needs to be changed for any of these possible communication protocols is the connection string. The rest of the code should be exactly the same (except for SOCKETS). For TCP/IP we recommend using VXI-11 since it better implements the IEEE 488.2 standard and all status checking.
But just in case, the connection string for a TCP/IP socket connection is:
//TCP/IP Sockets Connection string
string sAddress = "TCPIP0::10.0.1.194::5001::SOCKET";
3. Opening the communication session.
//open a Session to the VNA
mySession = ResourceManager.GetLocalManager().Open(sAddress);
//cast this to a message based session
mbSession = (MessageBasedSession)mySession;
4. Write to VNA and Read from VNA
//Send "*IDN?" command
mbSession.Write("*IDN?\n");
//Read the response
responseString = mbSession.ReadString();
Notice that the mbSession.Write() commands are terminated with a “/n”. This is the newline character. The next example discusses session parameters. Some of the other examples will get into reading binary data and arbitrary block data.
5. Exception Handling
The VISA driver catches certain types of errors like timeouts and .NET catches the rest.
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);
Console.WriteLine();
}
Example 1 – VisaException and Timeout
VisaException catches a timeout on a ReadString()
 
6. Close the session using Dispose()
//Close the Session
mbSession.Dispose();