Introduction
Remote Controlling Desktop Test Instruments consist of physically connecting the instrument to a computer using GPIB, USB, Ethernet, LXI, LAN and Serial Port, set the measurement conditions via instruments' command set, get the measurement results and log the measurement results for further analysis.
Some vendors(National Instruments, Agilent, Rohde Schwarz, Keithley, Tektronix...) offers an extensive assortment of instrument control hardware and software tools to help, save time and money throughout the life of your instrument control system. Improve performance and increase reliability with bus hardware for GPIB, USB, Ethernet, LXI, LAN, and serial while taking advantage of increased productivity due to software tools such as NI LabVIEW, Agilent VEE, Visual Studio C# and premade instrument drivers.
Using ready-to-use instrument drivers may not fit to our needs, since we need custom applications and measurements that vendors never know. So, developing instrument driver using direct I/O commands(the command set supplied with User/Programmer Manuals of the Test Instruments), solve the custom driver requirements. This article explains how to remote control the desktop electronic test instrument(in this case Agilent N9010A) step-by-step using C# over LAN.
Background
With Instrument control, one can connect to the desktop instrument using computer and take measurements. There are several different ways to control your instruments – you can either use an instrument driver or control the instrument through direct I/O commands (see Figure1).
Figure1.
Getting Started
In this tutorial, we are going to communicate with an instrument over LAN. The Instrument that we are communicate with is Agilent N9010A with IP number 192.168.1.47 and Port Number 5025.(See Figure2)
Figure2. N9010A LAN Communication User Interface
To accomplish this, we need to create new Windows Project using Visual Studio 2010, So click File->New Project. Then give a Name and Location to project(See Figure3)
Figure3.N9010A Project Settings
After creating a VS C#.NET project, we begin designing our User Interface(UI). Our UI consist of following controls and related properties;
Table1.Instrument Control Form
Control |
Name |
Text |
Windows.Forms |
MainForm |
N9010A Communication Over Lan |
Windows.Forms.Button |
btnConnect |
&Connect |
Windows.Forms.TextBox |
tbSAPort |
5025 |
Windows.Forms.TextBox |
tbSAAddress |
192.168.1.47 |
Windows.Forms.Label |
lblIDN |
Text for this control is filled by *IDN? query from N9010A |
Windows.Forms.Label |
labelControl1 |
SA IP: |
Windows.Forms.Label |
labelControl2 |
SA Port: |
Windows.Forms.Label |
labelControl3 |
IDN: |
Windows.Forms.ToolStripMenuItem |
tsmiFile |
File |
Windows.Forms.ToolStripMenuItem |
tsmiExit |
Exit |
In the following Class named N9010A
, we are going to write a N9010A communication infrastructue. The writeLine(string command)
method, sends a command to an instrument appending "\n". This "\n" character is known as EOL indicating the end of command. The readLine
method reads an instrument response to the remote command sent by writeLine(string command)
and returns as ASCII string.
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace N90101A
{
public class N9010A
{
private string IpAddr;
private int PortNumber;
private IPEndPoint ip = null;
public IPEndPoint Ip
{
get
{
if (ip == null)
ip = new IPEndPoint(IPAddress.Parse(this.IpAddr), this.PortNumber);
return ip;
}
set { ip = value; }
}
public N9010A(string instrIPAddress, int instrPortNo)
{
this.IpAddr = instrIPAddress;
this.PortNumber = instrPortNo;
if (!Soket.Connected)
throw new ApplicationException("Instrument at "+ this.Ip.Address + ":" + this.Ip.Port + " is not connected");
}
public void writeLine(string command)
{
Soket.Send(Encoding.ASCII.GetBytes(command + "\n"));
}
public string readLine()
{
byte[] data = new byte[1024];
int receivedDataLength = Soket.Receive(data);
return Encoding.ASCII.GetString(data, 0, receivedDataLength);
}
Socket soket = null;
public Socket Soket
{
get
{
if (soket == null)
{
soket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
if (!soket.Connected)
soket.Connect(this.Ip);
}
catch (SocketException e)
{
Console.WriteLine("Unable to connect to server.");
throw new ApplicationException("Instrument at "+ this.Ip.Address + ":" + this.Ip.Port + " is not connected");
}
}
return soket;
}
set { soket = value; }
}
}
}
Let us design a form so that we can communicate with an instrument-N9010A. Controls that we used on the Form is given in Table1.
MainForm
class is as follows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
namespace N90101A
{
public partial class MainForm : Form
{
private N9010A sa;
public MainForm()
{
InitializeComponent();
}
private void btnSAConnect_Click(object sender, EventArgs e)
{
this.saBaglan(this.tbSAAddress.Text, this.tbSAPort.Text);
}
private void saBaglan(string saAddress, string saPort
{
try
{
sa = new N9010A(saAddress, int.Parse(saPort));
sa.writeLine("*IDN?");
this.lblIDN.Text = sa.readLine();
}
catch (ApplicationException ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
The IDN response acquired from Spectrum Analyzer is as folllows:
Improving N9010A Class
It is time to improve N9010A class by adding more methods.One can extend the class N9010A to a .dll by creating new Class Library Project
public void saRbwVbwSweTime(double rbw, double vbw, byte swe)
{
writeLine("BAND " + rbw + " MHZ");
writeLine("BAND:VID " + vbw + " MHZ");
writeLine("SWE:TIME:AUTO " + (swe == 0 ? "OFF":"ON"));
}
public void readMarker1Peak(out double SAFreqOut, out double SAAmpOut)
{
writeLine(":CALC:MARK1:STAT ON");
writeLine(":CALC:MARK1:MAX"); writeLine(":CALC:MARK1:X?"); SAFreqOut = double.Parse(readDouble())/1E6; writeLine(":CALC:MARK1:Y?");
SAAmpOut = double.Parse(readLine());
}
Conclusion
In this tutorial, I explained the how to Remote Control an Electronic Test Instrument over LAN and develop an instrument driver without a VISA libraries and Instrument Vendor device drivers(dlls, driver files...). To extend the N9010A Class, one can write and add more methods to a class and develop an custom drivers for an instrument.For more articles visit http://www.miltest.com/
Instrument Drivers for the Spectrum Analyzers, Network Analyzers, EMI Receivers, Multimeters, Signal Generators are also available and custom drivers may be designed. For further questions please contact me.