Pages

Wednesday, March 12, 2014

SCPI programming with Visual Studio

If you are just starting to program an instrument in Visual Studio, you might not know where to start. Having some source code to compare notes and start with is always very helpful and can get you up and running in a few hours. I find that just getting started in a new programming environment is the hardest step. In this post, I'm going to show you how to get started with some example code to help you to program a SCPI based instrument in Visual Studio. We'll talk about the first steps in SCPI programming, addressing your instrument, and some basic debug techniques too. In this post, I'll be using C# but many of the same techniques can be used in other languages in the Visual Studio environment.

Here's a youtube video that I made that explains many of the same steps that we'll cover in this post

Last year, I developed some example programs for the 34461A DMM. A DMM is one of the most basic instruments that you will find that has a programmatic interface. Measuring a DC voltage is a basic concept in electronics and thus is a good place to start when learning to automate a test instrument. Since you won't get stuck on what the instrument is doing, so you can just focus on what the program steps are.

This code was developed in Visual Studio (VS) 2005, so it should be compatible with most VS versions that you might have installed on your PC.

I structured the programming examples so that you can open up all of the different examples in one solution file. This way, you can view any of the individual examples in the solution explorer of VS, instead of needing to navigate to each programming example's project or C# file. If this is foreign to you, it'll be apparent if you keep reading.

Let's start off by opening the solution file that is found after you unzip everything from the above link.

This will open up all of the different example programs in VS. My PC has VS2010, so I'll be using that for the rest of this post. The steps that I'm describing should be very similar from VS2005 on up though. In the image to the right, you'll see all of the different programming examples that we
developed for the 34461A. Many of the examples can also be used with the 34401A, 34410A, and 34411A as well.

In this post, we'll focus on the most basic program, the "BasicFunctions_3446xA" example. In C#, the example programs are organized by project. This project contains .cs file that contains the source code, the project references which allow you to refer to other libraries, and the project properties which determine the build location and other behaviors of the program.

There are a couple of things that we'll want to do before we start looking at the code. First, we'll want to set this project as our startup project, this will allow us to start this programming example when we start debugging vs. starting one of the other programming examples. This is easily accomplished by right clicking on the BasicFunctions_3446xA project file and selecting "Set as Startup Project". You can view this step in the video if you click on this link or at the 1:40 mark in the video above.

Next we'll want to add in a reference to a library called Visa COM. This library is essential for talking to SCPI based instruments using VS, and enables you to "talk" to an instrument with standard I/O's such as USB, LAN, GPIB, and even RS232. This is installed with Agilent IO libraries.

The library reference has already been added for the example programs,
but this could be useful if you are starting a new project or the
example program is somehow broken. To add the reference to Visa Com library, right click on the References folder in your project and select Add Reference. In the pop up box, select the COM tab and find the "VISA COM X.X Type Library". The X.X is the version number and gets updated periodically with different versions of IO Libraries. This step can be seen at the 1:58 mark in the video above.

Now let's look at the essential pieces of code. The first code step in communicating with an instrument is to start with some instrument handles. These handles will help you to declare your instrument and use them.

//For more information on getting started using VISA COM I/O operations see the app note located at:http://cp.literature.agilent.com/litweb/pdf/5989-6338EN.pdf
Ivi.Visa.Interop.ResourceManager rm = new Ivi.Visa.Interop.ResourceManager(); //Open up a new resource manager
Ivi.Visa.Interop.FormattedIO488 myDmm = new Ivi.Visa.Interop.FormattedIO488(); //Open a new Formatted IO 488 session

The variable rm is a resource manager that allows you to manage the IO tools. You only need to use this in one important place in the code.

myDMM will be used as a variable to allow you to read and write to your DMM. We'll be using myDMM anywhere where we want to talk with the DMM.

After declaring these variables, we'll need to connect the myDMM variable with your actual instrument. This is done with the following command:

myDmm.IO = (IMessage)rm.Open(DutAddr, AccessMode.NO_LOCK, 2000, ""); //Open up a handle to the DMM with a 2 second timeout

The variable DutAddr is a string that can be copied directly from IO libraries. Open up Agilent Connection Expert, find the instrument that you want to talk to and copy the Visa address. The paste the string into the DutAddr space.

You should now be able to talk to your instrument! Let's test the connection by sending a simple *IDN? command. This command is what I normally use to see if my communications to the instrument are working and if I'm talking to the right instrument which is important if your bench or system has a lot of different equipment.

There are two main commands when talking to an instrument. A write command and a read command. Sending a command to the instrument to change a setting is common, and you will just want to write a command. This is commonly done with the WriteString() command. I've shown this below with the *RST command.

When you ask an instrument to send back a reading it is called a query, and the basic structure to do a query is to send the command then read back the response. So it is a two step process. Be careful though, if you don't immediately read the response and you send another command, the instrument will have an error. Here's the basic example

myDmm.WriteString("*RST"); //Reset the device

myDmm.WriteString("*IDN?"); //Send the command for the DMM to return an IDN string string IDN = myDmm.ReadString(); //Read the string from the DMMConsole.WriteLine(IDN); //report the DMM's identity

That's all there is to basic communications with an instrument in Visual Studio. If you haven't done so already, be sure to watch the video above since it goes a little more in depth on making measurements, best practices when programming, and debugging your program.

52 comments:

The link above for the zipfile with c# examples does not at all contain all the examples you have here?, also the ContiniousSampling example is not there.The only document i have found so far about programming the 34461a is the "operating and service guide" which contains very very little information, do you know where one can find more complete stuff for my meter?

The link above does not contain the Continuous Sampling example that I published previously. I don't have an easy way to update the link from the Agilent.com website. It normally takes a few rounds of red tape.

However, I did update the Continuous Sampling blog with a new link to download the Continuous Sampling code. The link isn't from Agilent but you can access the source code. Please check that blog for the links. The Operating and Service guide contains the full SCPI documentation. The SCPI documentation starts at page 101 and continues until page 270. I'm not sure there is more complete documentation from Agilent.

This site is good because they give us a new thing and new ideas and new topic how good all of they are we should appreciate them because of these good thing. . I know something information, to know you can click here Test and Tag brisbane rcd testing services brisbane

This is very interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your fantastic post. Also, I have shared your web site in my social networks!Regards - www.office.com/setupwww.office.com/setup

I am convinced that your website is convinced that weblog is very new. Probably I am going to bookmark your site. There are certainly impressive stories. Passion for sharing your website with us.Regards - www.norton.com/setup

SCPI is a programming language standard planned explicitly to control instruments it characterizes how you speak with these instruments from an outer PC. This site is the stage that could demonstrate much useful, thank you for sharing. Celebrity Leather Jacket

I found this post quiet interesting and exciting, and the author of this post has explained everything in detail that is the best thing that i liked about this post. I am looking forward to such great posts.office.com/setup | norton.com/setup | mcafee.com/activate

Our custom Top Essay Writing Service is the best and highly ranked by students because of our dedication to keeping the promise. Whatever you read from the website about us is true,and we are committed to delivering well prepared papers to meet your entire write my Legitimate Essay Writing Service academic needs.