Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Remote Controlling Instrument Over LAN

0.00/5 (No votes)
25 Dec 2011 1  
Step by Step explanation of remote controlling test instruments over LAN

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).

instrumentDriverArchitecture.png

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)

UIDesign.png

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)

N9010ANewProject-300x207.png

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
{
    // N9010A Spectrumm Analyzer Class
    public class N9010A
    {
        //N9010A IP Address
        private string IpAddr;
        //N9010A Port number
        private int PortNumber;
        //a network endpoint as an IP address and a port number
        private IPEndPoint ip = null;
        //IPEndPoint property for N9010A
        public IPEndPoint Ip
        {
            get
            {
                if (ip == null)
                    ip = new IPEndPoint(IPAddress.Parse(this.IpAddr), this.PortNumber);
                return ip;
            }
            set { ip = value; }
        }
        //N9010A Spectrum Analyzer Constructor
        //instrIPAddress:N9010A IP Address,ex:"192.168.001.047"
        //instrPortNo:N9010A Port Number, ex:5025
        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");
        }

        //Writes an remote command to an N9010A with "\n"
        public void writeLine(string command)
        {
            Soket.Send(Encoding.ASCII.GetBytes(command + "\n"));
        }
        //Reads the N9010A response as string to a command sent with writeLine method
        public string readLine()
        {
            byte[] data = new byte[1024];
            int receivedDataLength = Soket.Receive(data);
            return Encoding.ASCII.GetString(data, 0, receivedDataLength);
        }

        Socket soket = null;

        //TCPIP connection to a N9010A
        public Socket Soket
        {
            get
            {
                if (soket == null)
                {
                    soket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    try
                    {
                        if (!soket.Connected)
                            soket.Connect(this.Ip);
                    }
                    //Throw an exception if not connected
                    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();
        }
        //When invoked, the Instrument is connected and instrument handle is created
        private void btnSAConnect_Click(object sender, EventArgs e)
        {
            this.saBaglan(this.tbSAAddress.Text, this.tbSAPort.Text);
        }
        //Sends a command "*IDN?" to an instrument whose IP Address is saAddress Port Number is saPort, reads the response from N9010A and update the User Interface
        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:

SpectrumAnalyzerIdnResponse.png

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

        
        // Adjusts the N9010A's RBW, VBW and Sweep Time
        // <param name="rbw" />RBW in MHz
        // <param name="vbw" />VBW in MHz
        // <param name="swe" />Sweep Auto 0-->OFF, 1-->ON
        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"));
        }

//Read the Marker Peak values and return the marker1 frequency to SAFreqOut, amplitude value to SAAmpOut
public void readMarker1Peak(out double SAFreqOut, out double SAAmpOut)
        {
            writeLine(":CALC:MARK1:STAT ON");
            writeLine(":CALC:MARK1:MAX");//Do a peak search
            writeLine(":CALC:MARK1:X?");//Query the marker peak value
            SAFreqOut = double.Parse(readDouble())/1E6;//in MHz
            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. 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here