Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Can anyone direct me to a link that shows an example of using C# and measurement studio 7.0 and the visa class to read/write to the serial port?

I am writing an application that will have a user selectable interface to an instrument. The user will select either a serial, gpib, usb, or ethernet instrument interface. I really need an example of how to read/write the serial port using the visa namespace in C#. Can anyone direct me to such an example. I plan to start with the serial port and then add the others via menu selections.
Thanks
Barry
0 Kudos
Message 1 of 6
(4,785 Views)
Hi Barry,
Attached is an application that has a user selectable interface to an instrument via serial, gpib, usb, or ethernet instrument interface.
Br,Sergei
"Only a life lived in the service to others is worth living..." - Albert Einstein
0 Kudos
Message 2 of 6
(4,776 Views)
Barry,
The example code from the zip file Sergei posted should already be on your system, as it is one of our installed VisaNS examples. You should find it on your computer at "C:\Program Files\National Instruments\MeasurementStudioVS2003\DotNET\Examples\Visa\VisaicNS." The other example programs there might help you get started as well.
All of the instrument connection types you listed share a common base class called MessageBasedSession. This base class should provide all of the functionality you need to perform reads and writes with your device. Also, we provide a factory class called ResourceManager for exactly the case you've described, where you don't know until runtime the exact session class you need. A simple example would look something like this:

//assumes that resource name describes one of the connection types you lists - usb, serial, gpib, ethernet, etc.
using (MessageBasedSession mbs = (MessageBasedSession)ResourceManager.GetLocalManager().Open(resourceName))
{
mbs.Write("*IDN?");
string idQueryResponse = mbs.ReadString();
}

You could pull the value of the resourceName variable from your user interface and use it to construct the session. From that point, regardless of the actual type of connection you're using, you should be able to work with the MessageBasedSession reference to perform I/O.
0 Kudos
Message 3 of 6
(4,750 Views)
Thanks Glen,
As a follow up, for a serial port resource how would I set the baud rate, stop bits, parity, this type of information?
Thanks for your help.
Barry
0 Kudos
Message 4 of 6
(4,744 Views)
Barry,

The NI-VISA Help is a great resource as well. You can find it by going to Start>>Programs>>National Instruments>>VISA>>NI-VISA Help

It looks like the function you want to use is viSetAttribute(), and the attributes are VI_ATTR_ASRL_BAUD, VI_ATTR_ASRL_STOP_BITS, VI_ATTR_ASRL_PARITY...

Hope this helps

Shawn B.
National Instruments
Use NI products on Linux? Come join the NI Linux Users Community
0 Kudos
Message 5 of 6
(4,713 Views)
Check out this link for info on how to set this up using the VISA .NET API.
Bilal Durrani
NI
0 Kudos
Message 6 of 6
(4,689 Views)