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.
Start off by downloading the Example C# 34461A code from Agilent.com
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:
string DutAddr = "USB0::0x0957::0x1C07::US00000069::0::INSTR"; //String for USB
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 devicemyDmm.WriteString("*IDN?"); //Send the command for the DMM to return an IDN string string IDN = myDmm.ReadString(); //Read the string from the DMM Console.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.