Using Visual Studio 2010 for Instrument Programming

[NOTE: If you are using Visual Studio 2012, please see the updated post]

Visual Studio is one of the most common platforms for programming in various languages including C/C++, C# and VB. This article highlights how C/C++ and C#  can be used for instrument control, should you choose to program in any of these languages.

One of the easiest methods to bypass these tricky configuration steps is to use a ready-made programming example that will not only have the settings done beforehand, it will also give you a place to start with a basic program. At the same time, it is always good to know the steps involved whenever you have to set up a programming environment to work with an external instrument.

The first step is the most important one as it involves performing some settings on the IDE for the code to link to the relevant libraries and compile successfully.

Using C/C++

1. Add the following include directory to ensure visa.h is found:

Note: This path assumes you are using Agilent/Keysight VISA. For any other vendor’s VISA installation, change the path accordingly.

C:\Program Files (x86)\IVI Foundation\VISA\WinNT\agvisa\include

VS2010-1

2. Under Linker, add Linker additional dependency path:

 C:\Program Files (x86)\IVI Foundation\VISA\WinNT\agvisa\lib\msc\visa32.lib

VS2010-2

3. Write your code, compile and run

Here is a sample program for reference:

#include <stdlib.h>
#include <string.h>

void main()
{
	char devAddr[20];
	char str[50];
	int str_size = 50;
	char str2[10];
	int str_size2 = 10;
	double real1 = 0.0;

	ViSession rm, instr_handle;

	printf("Enter Device Address\n");
	scanf("%s", devAddr);

	viOpenDefaultRM(&rm);
	viOpen(rm, devAddr, VI_NULL, VI_NULL, &instr_handle);
	viQueryf(instr_handle, "*IDN?\n", "%#T", &str_size, str);
	printf("IDN: %s\n", str);
		
	getchar();
	getchar();
}

 

Using C#

1. Add a reference to the VISA-COM library

CS-1

You will find the VISA-COM library under the COM Tab as shown below:

CS-2

2. Next make sure you have references added to the top of your program as shown below:

CS-3

3. Write your code, compile and run

Here is a sample C# program for reference:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ivi.Visa;
using Ivi.Visa.Interop;
using System.Runtime.InteropServices;


ResourceManager ioMgr = new ResourceManager();
FormattedIO488 instrument = new FormattedIO488();

IVisaSession session = null;

try
            {                
                session = ioMgr.Open("TCPIP::localhost::5025::SOCKET", AccessMode.NO_LOCK, 3000, "");
            }
catch (COMException ex)
            {
                Console.WriteLine("Failed to connect to the SCPI interface.");
            }

instrument.IO = (IMessage)session;
instrument.IO.SendEndEnabled = false;
instrument.IO.Timeout = 10000;                       //in milliseconds
instrument.IO.TerminationCharacterEnabled = true;   //Defaults to false    

instrument.WriteString("*IDN?");
string idnString = instrument.ReadString();            
Console.WriteLine("VISA Identification String: " + idnString);

instrument.IO.Close();

That’s it! Once you have the above piece of code working, you can refer to the programming and command guide of the instrument and get started with programming.

One comment:

Leave a Reply

Your email address will not be published. Required fields are marked *