Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Sep 6, 2006

Overcoming .NET Challenges in Test and Control

38 ratings | 3.82 out of 5
Print
Whether you are an experienced .NET developer or considering your first .NET program, it is essential that you learn to conquer the challenges you face when developing test and control applications on the .NET platform. With the emergence of .NET in 2002, Microsoft introduced a completely new technology that brings many benefits to engineers, especially for Web and database connectivity. As with many disruptive technologies, .NET presents new challenges, especially because it was not designed from a test and control engineer’s perspective, but from that of a system and IT developer. This article introduces you to .NET for test and control applications and teaches you how to successfully implement your next-generation .NET system.

The Benefits of .NET
.NET brings a completely new set of benefits to your applications, including connectivity to the Web via Web services, support for many languages, object orientation, and a managed environment with garbage collection. Web services enable instant communication between distributed applications, allowing different departments to share test data over common standards such as HTTP and XML. With support for more than 20 languages including C#, Visual Basic .NET, and FORTRAN .NET, .NET provides a framework for development teams to share and reuse code from multiple programming languages in a single application. Object orientation is another major benefit of .NET, allowing programmers to encapsulate real-world test hardware, such as data acquisition devices and instruments, as objects. For example, a data acquisition class could contain properties such as sampling rate and methods such as read. In addition, .NET handles many low-level tasks such as memory management -- programmers can allocate new memory on demand, and then rely on the garbage collector to dispose of the memory when it is no longer needed.
With these broad benefits, however, come challenges. When .NET was designed, the needs of test and control engineers were not at the forefront. Here are some common challenges that you may face, and how to successfully conquer them.

Challenge 1 -- .NET is general-purpose, but I need test- and control-specific functionality.
Because .NET is a complete framework for program development, you can create a wide range of applications, from financial to information technology to test and control. This is possible because the .NET framework provides an array of libraries ranging from database connectivity to threading to remoting, and user interface controls ranging from text boxes to calendar controls to data grids. For test and control applications that require graphing, analysis, and hardware I/O, you could use the default controls provided with Visual Studio .NET to create a business application (see Figure 1), but not to graph and visualize test data. Also, consider that scientific analysis libraries, such as a fast Fourier transform (FFT), are not provided by the .NET framework, so you have to design and implement these libraries yourself. Finally, it is ideal to interact with your instruments and control systems before actually writing all the code.



Figure 1. Visual Studio.NET features general-purpose user interface controls.


Challenge 2 -- This is new technology, but how do I preserve my existing investment?
Your company may have spent years developing thousands, if not millions, of lines of code that you do not want to simply throw away. You may have also seen your applications first incorporate C code, followed by ActiveX, then COM+. The big question is, “What is coming after .NET?” This rate of change may be acceptable for IT applications whose life spans are on the order of months, but not for test and control applications whose life spans may be on order of years or decades. When moving to a new technology, the first question you should ask is, “How can I reuse my existing programming investment?”
Incorporating existing C, C++, or Visual Basic 6.0 code into a .NET application is not trivial. With C or C++, you can create dynamic link libraries (DLLs) out of your existing code and call them from a .NET application. Calling a DLL from .NET requires Platform Invoke (P/Invoke) to call unmanaged code from the managed world. As shown in Figure 2, this requires manually declaring each function from the DLL you want to import. If you have 500 functions that you want to reuse, you need to manually wrap all 500 functions with DllImport in C# or Declare Function in Visual Basic .NET. The most challenging part, though, is not just declaring the function name, but ensuring that all the .NET data types match the data types in the DLL. For example, character arrays become strings, C pointers become IntPtr, and so on. You might think you are done after your program successfully compiles, but you discover data type mismatches only at run time when the CLR actually invokes the DLL.


[+] Enlarge Image

Figure 2. Calling a DLL from .NET requires manually declaring each function from the DLL that you want to import.


Challenge 3 -- .NET is managed, but I need access to low-level I/O.
.NET is a managed environment, isolating you from the underlying hardware. The .NET Common Language Runtime (CLR) provides a programming infrastructure that automatically handles programming tasks that used to be manual coding challenges, such as memory management. The CLR acts as a buffer between your application and the actual memory on the host computer, thus preventing direct access to memory and registers. To control devices such as GPIB boards, serial ports, and data acquisition devices, your program still needs direct memory access. This is typically done through a DLL. But, as seen in challenge 2, you must manually wrap every DLL function using P/Invoke and then ensure that all data types match. If you want to write your own device driver, you need to choose an unmanaged language such as C that can run in the low-level “kernel mode” of your OS and not just in “user mode,” to which .NET applications are restricted.


Conquering the Challenges
Despite the pitfalls introduced by .NET, you still can be successful using this new technology. National Instruments has specialized in measurement and automation for more than 28 years and offers a variety of development and driver software to help you overcome the aforementioned challenges.


National Instruments Measurement Studio and .NET
To better address the needs of scientists and engineers, NI Measurement Studio supplements Visual Studio .NET with functionality specific to test and control. Without Measurement Studio, simply acquiring measurement data, performing an FFT, and displaying the results on a graph would require writing your own .NET class libraries and controls from scratch. Measurement Studio provides a complete suite of native .NET scientific user interface controls such as graphs, knobs, gauges, thermometers, and more. All of these controls are designed to be fully extensible so you can use them as a base class to define your own custom controls. For engineering analysis, Measurement Studio includes libraries for curve fitting, frequency domain analysis, filtering, windowing, signal generation, and more. Finally, Measurement Studio includes code generating assistants to help you interactively program data acquisition tasks and instrument control operations. With Measurement Studio, you can take advantage of all the features that .NET offers, plus measurement-specific controls, libraries, and assistants. And because Measurement Studio is an add-on toolkit for Visual Studio, it fits in seamlessly with your current .NET controls and C# and Visual Basic .NET source code.



[+] Enlarge Image


Figure 3. Measurement Studio provides scientific user interface control and data acquisition.



National Instruments LabVIEW and .NET




Figure 4. LabVIEW can call a variety of technologies.

NI LabVIEW is a fully functional graphical programming environment with which you can quickly create VIs (virtual instruments) with intuitive graphical code. Over time, as other technology such as OLE evolved into ActiveX, then into .NET, LabVIEW has continued to provide a stable programming platform based on graphical code. Every time a new technology is introduced, LabVIEW shields you from rewriting your applications from scratch or struggling to incorporate old technologies with new technologies. Instead, LabVIEW provides a common code base from which you can still incorporate other technologies, including .NET assemblies, ActiveX, DLLs, C code, and The MathWorks, Inc. MATLABĀ® script (see Figure 4). Specifically for .NET, you can use LabVIEW to instantiate .NET classes, invoke methods, and set and get properties. Figure 5 shows LabVIEW source code that is invoking a .NET assembly to access the current CPU utilization and display the results on a waveform chart.




Figure 5. LabVIEW can directly invoke .NET assemblies.



NI TestStand and .NET
NI TestStand is a ready-to-run test management environment for automating and sequencing your test and validation systems. With NI TestStand, you can sequence test modules created in a variety of languages and technologies, including .NET assemblies, LabVIEW VIs, DLLs, ActiveX components, and HT Basic code. For .NET, NI TestStand includes a fully functional .NET adapter with which you can call .NET methods, get and set properties, and debug .NET source code directly from NI TestStand. With a single NI TestStand project, you can easily sequence old test modules created in C with new test routines created using the latest .NET technology. To configure a test step in NI TestStand, simply point and click to the location of your module as shown in Figure 6.


Figure 6. Use NI TestStand to manage .NET test modules.


National Instruments Device Drivers and .NET
As mentioned earlier, you cannot create device drivers in .NET because the CLR is restricted from the “kernel mode” of the OS. As a result, National Instruments provides .NET hardware APIs for many of its hardware drivers (see Figure 7). These .NET APIs provide an object-oriented interface to the hardware, managed exception handling, and event handlers. This implementation provides a natural .NET programming experience with hardware.



Figure 7. National Instruments provides .NET hardware APIs for many of its hardware drivers.


Successful .NET Use for Test and Control
.NET is a new technology that offers many benefits for test and control applications, but also presents challenges. You can successfully overcome these challenges, but you must first understand them and then use the right tools. National Instruments tools such as Measurement Studio, LabVIEW, NI TestStand, and NI drivers make the migration to .NET easier and your application more powerful.

View interactive tutorials on NI Measurement Studio for data acquisition and instrument control applications.

Nate D'Anna
Measurement Studio Product Marketing Manager
nate.danna@ni.com

MATLABĀ® is a registered trademark of The MathWorks, Inc.

This article first ran in the August 5, 2005, issue of NI News.


38 ratings | 3.82 out of 5
Print

Reader Comments | Submit a comment »

 

Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).