Leveraging the .NET Framework in Measurement and Automation Applications


Nate D'Anna
National Instruments

April 2005

Summary: Discusses the enhanced features of the .NET Framework version 1.1, including easy-to-use language constructs, robust distributed communication functionality, and language and technology interoperability. (8 printed pages)


Easy-to-Use Language Constructs
Distributed Communication
Add-On Tools


The .NET Framework offers an ideal platform for engineering measurement and automation applications, ranging from the most simple temperature measurement to complex multi-tier distributed test systems. The .NET Framework version 1.1 offers easy-to-use language constructs, robust distributed communication functionality, language and technology interoperability, and rich add-on tools for optimizing .NET in measurement and automation applications. Looking forward, version 2.0 of the .NET Framework contains even more exciting features that you can incorporate into your applications, such as click-once deployment to ensure that all testers or manufacturing stations are always running the same test software version. If you are still considering migrating to .NET, or even if you have already adopted .NET, learning how to leverage all that .NET has to offer can be very beneficial.

Easy-to-Use Language Constructs

Whether you program is in Visual Basic, C++, Java, or FORTRAN, there is probably a .NET language that offers an easy upgrade path for you. The most popular .NET languages are Visual Basic and C#. C# is a natural migration for C++ programmers, with familiar syntax, such as if-else statements, while loops, and new keywords. Visual Basic .NET builds on familiar Visual Basic syntax, in addition to a new object-oriented programming paradigm.

Visual Basic .NET and C# offer language constructs that make them ideal choices for test and measurement, including object-orientation, managed exception (error) handling, and memory management. Measurement and automation applications are good candidates for object-oriented implementation because test systems typically consist of real-world objects that need software representation. For example, you may want to encapsulate data acquisition (DAQ) modules, instrument drivers, or even complete test racks. To get more specific, a data acquisition class could contain properties such as sampling rate, gain, and voltage range, and also methods such as read, write, and reset. In addition to easily representing real-world objects, object-orientation provides clear separation between objects, which facilitates division between distributed systems.

Most, if not all, measurement and automation applications require hardware integration. Anyone who has dealt with hardware programming knows the difficulty in handling hardware-related errors, especially when error handling code is necessary after every I/O call. This melting pot of hardware I/O communication and error handling code increases program complexity and decreases readability, especially if you use goto statements. In contrast, the .NET Framework offers a language construct ideal for handling I/O errors. Both Visual Basic .NET and C# offer try-catch-finally exception blocks to logically separate error handling and cleanup code from I/O code. Simply place all I/O code in a try block like so:

// Place hardware I/O code here
catch(Exception e)
//Place code to display error here
// Cleanup hardware here

[Visual Basic .NET]
 'Place hardware I/O code here        
Catch e As Exception
 'Place code to display error here                        
 'Cleanup hardware here
End Try

If an error occurs anywhere in this code block, the application throws an exception that causes the current block to exit. The exception is then trapped in the catch block where your application can respond to the error. The .NET Framework provides specific exceptions such as OutOfMemoryException, NullReferenceException, and DevideByZeroException, in addition to offering you the ability to define custom exceptions. As shown in the earlier code snippet, a general exception handler, Exception, catches all exceptions. From within these catch blocks, you can handle the exception by displaying the error and possibly restarting the I/O. The finally block always executes last, regardless of an exception being thrown. In the finally block, you can insert cleanup code such as code to release a handle to an instrument.

Memory management is another key feature of the .NET Framework. Many engineering applications deal with large amounts of data, especially when acquiring data continuously. Imagine a data acquisition device sampling at 106 samples/second. Assuming a double data type, after 10 seconds you have already acquired over 76 MB of data. In the past, managing this data and preventing memory leaks was a time-consuming process that distracted you from the main goal of measuring and automating, particularly in C and C++. The .NET Framework manages all memory for you, allowing you to focus on your application. Under the hood, the .NET Framework garbage collector disposes unused objects. As a programmer, you can simply allocate memory when needed, while letting the garbage collector handle memory cleanup.

Distributed Communication

Whether you are automating multiple manufacturing plants, performing distributed analysis for computational intense algorithms, or remotely monitoring an oil well, the .NET Framework provides a powerful set of communication options. The most talked about feature for remote communication between applications are Web services. Web services are based upon industry standards, such as XML and HTTP. This allows remote applications to freely exchange data, even between different networks, firewalls, operating systems, and programming languages. You can use Web services to expose Web server functionality to worldwide clients through the Internet. Such functionality might include multiple testers logging test results to a central database using a data logging Web service.

  Advantages Disadvantages Use Cases
Web Services
  • Loosely coupled nodes
  • Thin clients
  • Interoperability
  • Open communication
  • Slower than remoting
  • Limited data type support
  • Automated parts ordering
  • Database logging
  • Fast communication
  • Callback support
  • Lifecycle management
  • Object passing capability
  • Robust client applications
  • Must use .NET Framework
  • High-speed distributed analysis within an intranet
  • In-vehicle testing
  • Rich GUI
  • JavaScript support
  • Open communication standards
  • Thin clients
  • Does not require .NET Framework on the client
  • Must use IIS for Web server
  • Remote monitoring
  • Remote control

Another technique you can use to communicate between applications is remoting. Remoting provides all the power of the .NET Framework for distributed applications. You can use remoting to program tightly coupled distributed applications without worrying about communication between clients and servers. Simply program as though all the remote classes were local, taking advantage of functionality like callbacks and events, and let the .NET Framework handle the networking details. A good example of remoting in action involves creating a distributed analysis infrastructure to perform nearly real-time test data computation.

ASP.NET is another feature of the .NET Framework. It is an incredibly compelling technique to create rich Web page front-ends with .NET Framework-powered back-ends. As Web users interact with Web pages, data is sent back to the server to cycle industrial pumps, calibrate test systems, or troubleshoot machines. With ASP.NET, you can use your choice of .NET language to create Web pages while automatically generating HTML and JavaScript code.


Most measurement and automation applications include disjoint parts—from manufacturing testers, to assembly lines, to databases—many of which are programmed using different languages and technologies. As discussed earlier, you can connect these disjointed parts through techniques such as Web services, remoting, and ASP.NET. But, to provide further interoperability, you can use the .NET Framework with multiple programming languages such as C#, Visual C++, Visual Basic .NET, and even COBOL .NET, in addition to incorporating ActiveX and DLL components.

For language interoperability, there are more than 20 .NET-compliant languages. Visual Studio .NET 2003 ships with C#, J#, Visual Basic .NET, and C++ .NET, but other languages such as COBOL .NET and SmallTalk .NET are now .NET-compliant. In .NET applications, you can use components created in different .NET languages in a single solution, as shown in Figure 1.

Figure 1. Using multi-language components in a single solution

.NET languages are compiled into a common form called intermediate language (IL). IL is language-independent, thus you can reuse components in other NET languages. These components are packaged as assemblies. The common language runtime (CLR) then executes the assembly on a specific operating system. Currently, the CLR is only fully implemented for Windows operating systems, but there is work being done to deploy the CLR to Linux (Mono) and Macintosh machines. When this happens, you could develop .NET assemblies on a Linux machine and deploy these to Windows without recompiling or modifying your assemblies.

.NET offers further interoperability by allowing you to incorporate technologies such as ActiveX and DLLs into your applications. This is essential for reusing existing hardware, such as PLCs that only have ActiveX or DLL interfaces. When using Visual Studio .NET 2003, simply drag-and-drop an ActiveX control on your user interfaces to automatically create a runtime callable wrapper (RCW). This RCW exposes the ActiveX component functionality to your .NET application. You can also incorporate DLLs into your .NET application by using Platform Invoke (P/Invoke). As shown in the following code snippet, you can use P/Invoke to declare a method from within a DLL and use it in your .NET application. After declaring the DLL method, you can use it as if it were a native .NET function.

static extern bool MessageBeep(UInt32 beepType);

[Visual Basic .NET]
Declare Function MessageBeep Lib "User32.dll" ( _
ByVal beepType As UInt32) As Boolean

Add-On Tools

One of the most powerful features of the Visual Studio .NET 2003 is the availability of add-on tools that transform it from a flexible, general-purpose programming environment to an engineering workbench customized for measurement and automation applications. National Instruments Measurement Studio for Visual Studio .NET is one tool that offers native .NET scientific user interface controls, advanced analysis libraries, and instrument control and data acquisition APIs. Measurement Studio offers direct integration with Visual Studio, giving you access to all of your instruments and data acquisition devices from within a single environment. The following tools can be found at http://www.ni.com/mstudio:

  • Scientific user interface controls such as graphs, gauges, knobs, thermometers, tanks, LEDs, and slides.
  • Advanced engineering analysis such as FFTs, spectral measurements, array manipulation, filtering, and windowing.
  • Virtual Instrument Software Architecture (VISA) support, which provides a .NET API to control GPIB, serial, USB, and Ethernet instruments.
  • An Instrument Driver .NET Wizard to automatically wrap existing instrument drivers to work under .NET.
  • Data acquisition support, which provides a .NET API for analog input, analog output, digital I/O, and counter/timer operations.
  • Visual Studio integration, which offers access to all Measurement Studio functionality without leaving Visual Studio.
  • Instrument I/O Assistant to facilitate interactive communication and parsing with instruments, in addition to code generation.
  • DAQ Assistant for interactive data acquisition task configuration, testing, and code generation.

Figure 2 and Figure 3 below show some of these tools at work.

Figure 2. National Instruments Measurement Studio for Visual Studio .NET tool sample 1

Figure 3. National Instruments Measurement Studio for Visual Studio .NET tool sample 2


Version 1.1 of the .NET Framework proves to be a powerful platform for creating measurement and automation applications. With features such as object-orientation, managed exception handling, memory management, Web services, remoting, ASP.NET, language and technology interoperability, and add-on tools including Measurement Studio, the .NET Framework offers distinct advantages over Visual Basic 6.0 and C++. Looking forward to version 2.0 of the .NET Framework stands to offer even more exciting features for measurement and automation applications. One such feature, click-once deployment, checks that each running client application is the latest version. If not, it downloads the latest version from a Web server or shared directory. This function is particularly useful for upgrading numerous distributed test systems. The next generation of the .NET Framework also offers improved computation performance, further improving advanced analysis routines.

Nate D'Anna is a product marketing engineer for National Instruments. He specializes in distributed computing, data acquisition, and instrument control using the .NET Framework.