In this video, I'll be discussing DMM programming in Visual Studio. I'll be using the introductory
programming examples for the 34460A and 34461A DMM's as our test code. Many of you can use
this as an introduction to programming Agilent instruments in Visual Studio. My name is David
Tu. And to keep the video a little bit more interesting, you can imagine my voice coming
out of astronaut sloth over here.
I'll be going over the usage of VISA COM including opening and closing a session and
how to handle time outs. I'll talk about developing in Visual Studio and some of the best practices
that I use for programming including try/catch routines and the function that captures the
DMM errors. And finally, we'll be doing a run of setting up the DMM for voltage, current,
resistance, frequency, and temperature, and do a live debug of the program. I'll primarily
be using C# as my test bed, but we'll also be using Visual Basic .net in this video
as well just to highlight some of the differences.
Let's go ahead and open up Visual Studio 2005 and the C# solution file. I developed
the code in Visual Studio 2005 because it is easier to upgrade versus downgrade from
a Visual 2010 solution file to the 2005 file version. Now that I have it open, you'll notice
that the solution file has eight different projects, which basically includes all eight
of our programming examples. Each project contains its own little programming example.
We're going to focus in on the basic functions example for this video. And first thing I
want to do, I actually want to set this as the startup project. That way, when we do
start debugging the code, it'll actually debug this code versus some of the other projects
that we have loaded here. The other thing we wanted to discuss was the VISA COM library.
A lot of people have questions about how to add a reference.
So, I'm just going to right click and click add a reference here. And under the COM tab,
I'm going to type in VIS, which will drop me right down to VISA COM 3.0 library. And
I press okay, and that kind of just re-adds this VISA COM library. So, after you've done
that, the first step to actually coding and talking to an instrument is by setting up
a resource manager, and then opening a session to your DMM.
The application note that -- I've put a link down here -- actually discusses this in pretty
good detail. And so you can read that and it should answer most of your questions that
you will have. One of the features that I put in here is I have at the top a using line
using Ivi.Visa.Interop. So, when I do that in Visual Studio, that means that I really
don't have to type in this header portion to get to the lower level methods.
So, what that means is that I can actually type in the code as Ivi.Visa.Interop. As you'll
notice, as I'm typing it, it kind of sets up the code for me. So, that's called intellisense
in Visual Studio. �.ResourceManager�.
So, I can do it that way or I can just type in ResourceManager rm = new blah, blah,
blah. So, that's one of the nice features of Visual Studio and one of the reasons why
I primarily like to develop in Visual Studio. Let's see. The next step to actually start
talking to your instrument is to set up a DUT address. This is just a variable I like
to put in there so that when we open up the session, it's a little bit cleaner.
But, for each of your IO's you will actually have a different string for setting up your
DUT address. I've put in examples for GPIB LAN and USB here. And for this example, we're
actually going to open up IO libraries and talk to this particular instrument over here.
And what I'm going to do is click here --click on the instrument -- and just copy over the
VISA address, and paste it into our program.
That'll allow me to talk to that instrument. Next step, we'll actually open up a handle
to that instrument by setting the open command, the data address, the access mode no lock.
So, it means that other controllers can talk to that instrument. Sign the initial time
out to two seconds, and then option string, which I don't really use.
I don't know what that means [laughter]. The next line that we have in the code is we reset
the instrument time out to three seconds. For those of you that aren't
familiar, this time out isn't actually a program time out or a bus time out. It's actually
if the instrument doesn't give me a response within three seconds, an error will be thrown
and it'll actually be caught by our error handler.
At this point, I guess it's a good time to talk about the error handler, which includes
a try/catch routine. A try/catch routine kind of -- everything runs everything
that's inside the braces for the try, so it'll try to run the code, and then if there's an
error, it'll actually go into the catch. And for our catch routine, we actually just write
down that there's an error that occurred and whatever the error message is. The finally
statement basically says, After the try/catch code has run, it'll finally run all this other
code."
In our finally code, we actually close the handle for the DMM. If it's possible, release
the COM object for the handle, as well as the resource manager. And finally, just so
that the program doesn't close without you actually seeing the error messages, we actually
say press any key to continue. Another best practice besides that try/catch is to write
a DMM error handler. So, basically, after every set of commands, I send this check DMM
error.
It's a method that basically sends the error command to the DMM and checks if there's actually
something wrong with the DMM itself. So, if that response string from the DMM says, "No
error" then it's just going to return and say, "Let's go ahead and proceed with the
rest of the program." However, if there is an error, it'll continue to send the system
error command to the DMM until it receives a no error string, and then all those error
strings will be accumulated and thrown as an exception, which will then be printed out
to our console. Let's go ahead and return back to our main line of code and start discussing
the actual code that talks to the DMM.
One of the things that I like to do is I like to kind of clear out the instrument, and make
sure it's in a good state and I'm talking to the right instrument. So, I can clear the
bus on the instrument by sending this IO.clear command; send a *RST command. This is actually
the first time I'm actually sending a command, so let's talk about that a little bit. This
right string command actually has two parameters, which is the data and then the Boolean flush
parameter.
So, really the *RST is the command I want to send to the instrument. And then the second
parameter, I've always set to true. It's actually an optional parameter in Visual Basic and
later versions of Visual Studio, but for Visual Studio 2005 C#, it's a non-optional parameter,
so we have to set that. But, you'll notice in our Visual Basic .net code, I don't actually
have to send that second parameter.
So, let's go ahead and pop back over to C#. So, I send the *RST and secondly, I send
the *IDN command. So, the *IDN just says, "What instrument are you?' And, "Tell
me what your identity string is." I send the command and here is where I first do a ReadString()
response -- try to get a response from the DMM. And I do that by reading a string and
saving it as a variable.
And then finally, I write it out to the console. I mentioned configuring for voltage, current,
resistance, and all that stuff. So, basically, in this programming example, we actually do
configure the DMM to lots of different functions, and just do a quick read for it. One of the
things to mention about these commands is that they can actually be found in the manual
that you can download. And there is a SCPI programming reference section, which has all
of these commands.
And these commands can be found in the configure subsystem. It's pretty straightforward for
this programming example, but I know a lot of you don't necessarily know how to send the commands
and what the parameters are. So, you can actually go through these and try it out. It's really
a very basic example. But, in general, each of these configure commands kind of set the
range and resolution.
You can actually just send the command like we do for our resistance, which will then
auto range and auto select your resolution for you. So, you can send it without the parameters
or you can send it with the parameters to be a little bit more specific on what you
want to run. So, I think that we are ready to actually try to run this code and actually
maybe debug it as well. So, maybe the first thing I'll do is I'll set a break point here
by clicking on this left hand side and press run. So, the first thing I notice is that
it actually reported what my IDN string is.
So, talking to this unit right here. So, I can actually step through each line of code
by pressing this button here, and watch each line execute. I just wrote out the DC voltage
reading. The DMM currently has open terminal, so the readings are going to be either really
small or open terminal readings.
And then here we actually run the check DMM error, so let's step into that. It'll actually
check that the DMM had no error. So, I can scroll over the variable and see what the
value is. So, that's a nice feature of Visual Studio. So, I'm going to go ahead and let
it continue to run.
And what you'll see is that each of the readings kind of came back for AC voltage, DC current,
AC current, 2-wire resistance, 4-wire resistance, frequency, and temperature. So,
it's a nice little program that configures all of the functions for your DMM. Alright.
I think it's about it for our example today. It's a long video, but thanks for watching
us.
And hopefully, we'll get you to watch a few more videos. So, thank you very much and remember
astronaut sloth reminds you to reach for the stars.