What happens if you have to use some code written in another language (such as C, C11, or Visual Basic)? Or if, for some reason, you just miss typing in all those semicolons in your familiar text-based code? LabVIEW does give you some options for interfacing with code from other languages. If you are thinking about writing all your code in C or C11, you should check out LabWindows/CVI (available from National Instruments), a programming environment very similar to LabVIEW; the main difference is that C code replaces the graphical block diagram. But if you'd like to (dare we say it?) actually have fun programming, then stick to LabVIEW and use conventional code only when you have to. You may want to skip this section if you aren't familiar with writing code in other programming languages such as C, or if you don't expect to need to interface LabVIEW and external code. LabVIEW gives you three options to call external, compiled code:
Sysetm Exec.vi is the simplest option to launch a separate executable or program that does what you need. On Windows, Linux, and Mac OS X systems, you do this with the System Exec function (Connectivity>>Libraries & Executables palette), shown in Figure 14.37. Figure 14.37. System Exec.vi
If you have dynamically linked libraries: Dynamic Link Libraries (.dll) on Windows, Shared Libraries or Shared Objects (.so) on Linux, and Frameworks (.framework) on Mac OS X, you can use the Call Library Function Node in LabVIEW (Connectivity>>Libraries & Executables palette), shown in Figure 14.38. Figure 14.38. Call Library Function Node
Finally, if you want to write your own C code and embed it into LabVIEW, you should use the Code Interface Node (CIN) (Connectivity>>Libraries & Executables palette), shown in Figure 14.39. Figure 14.39. Code Interface Node
This section talks about interfacing with lower-level compiled code in other languages, but you can also use LabVIEW to communicate with inter-application frameworks like ActiveX and .NET. You'll learn about these in Chapter 16, "Connectivity in LabVIEW." LabVIEW can also go the other direction. If you need your external code to call a LabVIEW program, LabVIEW can export a VI into an executable (.exe), dynamic link library (DLL) on Windows, shared library (.so) on Linux, and framework (.framework) on Mac OS X. We discussed this capability briefly in Chapter 3, "The LabVIEW Environment." Using the Call Library Function Node to Call DLLsWe'll look into a little more detail at one of the more common ways you might need to call external code: when it is provided in the form of a DLL. DLLs are small, compiled libraries in a .dll file that encapsulate some functionality and export a set of functions that other applications (like LabVIEW) can call. Usually, you need good documentation to be provided with a DLL for you to be able to use it, because you need to know what the functions are and how the data should be passed to and from them. Although we will refer to the external libraries here as DLLs, which are for the Windows platform, the exact same steps would apply to calling Frameworks under Mac OS X or Shared Libraries under Linux. Watch Out! Calling DLLs incorrectly or calling DLLs that are buggy can make your system become unresponsive, crash LabVIEW, or even crash your entire computer. Be sure you've saved your work and test carefully when using DLLs. And in some cases, keep the fire extinguisher nearby! Activity 14-4: Calling a DLL in LabVIEWIn this activity, you'll see how to call a DLL. To use a simple DLL that you already have on your system to compute the mean value of an array, we'll call a math-analysis DLL that already ships with LabVIEW, the lvanlys.dll. This DLL is actually used by many of LabVIEW's math functions.
If you aren't familiar with C language, this means that the function Mean_head returns a long (32-bit) numeric, and takes in as arguments a pointer called xH, a pointer called mean, and a pointer called dsperr. This function takes an array of numerics referenced by the pointer xH and calculates the mean, placing its value in the memory location referenced by the pointer mean. If there is an error, its numeric code is referenced by the pointer dsperr.
Error Cluster From Error Code.vi Use Error Cluster From Error Code.vi (Programming>>Dialog & User Interface palette) to convert the error or warning code to an error cluster. This VI is useful when you receive a return value from a DLL call or when you return user-defined error codes.
If all of this seems complicated for such a simple function, it is! Using pure LabVIEW is easier and much more fun. That's why you should try to avoid DLLs unless you have a good reason to use them, such as needing a routine only available as a DLL or in some speed-critical application. There are many other complex issues involved in LabVIEW's communicating with external code. Because many of these are highly dependent on the processor, operating system, and compiler you're using, we won't attempt to go any further into discussing DLLs or CIN functions. If you'd like more details, consult the LabVIEW help documentation and the application notes at http://zone.ni.com. |