- •Worldwide Technical Support and Product Information
- •National Instruments Corporate Headquarters
- •Worldwide Offices
- •Important Information
- •Warranty
- •Copyright
- •Trademarks
- •WARNING REGARDING USE OF NATIONAL INSTRUMENTS PRODUCTS
- •Contents
- •About This Manual
- •Conventions
- •Related Documentation
- •Calling Code in Various Platforms
- •Characteristics of the Two Calling Approaches
- •Details of Call Library Function
- •Details of a CIN
- •Calling Shared Libraries
- •Figure 2-1. Call Library Function Dialog Box
- •Calling Conventions (Windows)
- •Parameters
- •Calling Functions That Expect Other Data Types
- •Building a Shared Library (DLL)
- •Task 1: Build the Function Prototype in LabVIEW
- •Task 2: Complete the .c File
- •Required Libraries
- •Task 3: Build a Library Project in an External IDE
- •Figure 2-2. Creating a Project in Visual C++
- •Figure 2-3. Setting the Use run-time library control, Microsoft Visual C++
- •Gnu C or C++ Compilers on Solaris, Linux, or HP-UX
- •Metrowerks CodeWarrior on Power Macintosh
- •Calling External APIs
- •Common Pitfalls with the Call Library Function
- •Incorrect Function Name
- •Data Types
- •Constants
- •Calling Conventions
- •Example 1: Call a Shared Library that You Built
- •Configuration of Call Library Function
- •Create Front Panel
- •Create the Block Diagram
- •Example 2: Call a Hardware Driver API
- •Figure 2-4. VI That Calls Hardware
- •Example 3: Call the Win32 API
- •Table 2-1. Mapping Win32 Data Types to Standard C Data Types
- •Table 2-2. Mapping Win32 Data Types to LabVIEW Data Types
- •Constants
- •Table 2-3. Selected Constants for MessageBox
- •Figure 2-5. Combining Function Constants in LabVIEW
- •Determining the Proper Library and Function Name
- •Unicode Versions and ANSI Versions of Functions
- •Configuring a Call to the Win32 API
- •Figure 2-6. Configuring Call Library Function to call the Win32 API
- •Figure 2-7. Block Diagram for a Call to the Win32 API
- •Figure 2-8. Running a LabVIEW Call to the Win32 API
- •Additional Examples of LabVIEW Calls to DLLs
- •Debugging DLLs and Calls to DLLs
- •Troubleshooting the Call Library Function
- •Troubleshooting your DLL
- •Troubleshooting Checklist
- •Module Definition Files
- •Array and String Options
- •Arrays of Numeric Data
- •String Data
- •Figure 2-9. The LabVIEW String Format
- •Figure 2-10. The Pascal String Format
- •Figure 2-11. The C String Format
- •Array and String Tip
- •Supported Languages
- •Macintosh
- •Microsoft Windows
- •Solaris, Linux, and HP-UX
- •Resolving Multithreading Issues
- •Making LabVIEW Recognize a CIN as Thread Safe
- •Using C Code that is Thread Safe
- •Creating a CIN
- •Step 1. Set Up Input and Output Terminals for a CIN
- •Input-Output Terminals
- •Output-Only Terminals
- •Step 2. Wire the Inputs and Outputs to the CIN
- •Step 3. Create a .c File
- •Step 4. Compile the CIN Source Code
- •Compile on Macintosh
- •Microsoft Windows
- •Solaris 2.x
- •HP-UX and Linux
- •gcc Compiler
- •Step 5. Load the CIN Object Code
- •LabVIEW Manager Routines
- •Pointers as Parameters
- •Debugging External Code
- •DbgPrintf
- •Windows
- •UNIX
- •Passing Parameters
- •Parameters in the CIN .c File
- •Passing Fixed-Size Data to CINs
- •Scalar Numerics
- •Scalar Booleans
- •Refnums
- •Clusters of Scalars
- •Return Value for CIN Routines
- •Examples with Scalars
- •Creating a CIN That Multiplies Two Numbers
- •Passing Variably Sized Data to CINs
- •Alignment Considerations
- •Arrays and Strings
- •Paths
- •Clusters Containing Variably Sized Data
- •Resizing Arrays and Strings
- •SetCINArraySize
- •NumericArrayResize
- •Examples with Variably Sized Data
- •Concatenating Two Strings
- •Working with Clusters
- •Manager Overview
- •Basic Data Types
- •Scalar
- •char
- •Dynamic
- •Memory-Related
- •Constants
- •Memory Manager
- •Memory Allocation
- •Memory Zones
- •Using Pointers and Handles
- •File Manager
- •Identifying Files and Directories
- •Path Specifications
- •File Descriptors
- •File Refnums
- •Support Manager
- •CIN Routines
- •Data Spaces and Code Resources
- •One Reference to the CIN in a Single VI
- •Loading a VI
- •Unloading a VI
- •Loading a New Resource into the CIN
- •Compiling a VI
- •Running a VI
- •Saving a VI
- •Aborting a VI
- •Multiple References to the Same CIN in a Single VI
- •Multiple References to the Same CIN in Different VIs
- •Single-Threaded Operating Systems
- •Multithreaded Operating Systems
- •Code Globals and CIN Data Space Globals
- •Examples
- •Memory Manager Functions
- •Support Manager Functions
- •Mathematical Operations
- •ASCIITime
- •AZCheckHandle/DSCheckHandle
- •AZCheckPtr/DSCheckPtr
- •AZDisposeHandle/DSDisposeHandle
- •AZDisposePtr/DSDisposePtr
- •AZGetHandleSize/DSGetHandleSize
- •AZHandAndHand/DSHandAndHand
- •AZHandToHand/DSHandToHand
- •AZHeapCheck/DSHeapCheck
- •AZHLock
- •AZHNoPurge
- •AZHPurge
- •AZHUnlock
- •AZMaxMem/DSMaxMem
- •AZMemStats/DSMemStats
- •AZNewHandle/DSNewHandle
- •AZNewHClr/DSNewHClr
- •AZNewPClr/DSNewPClr
- •AZNewPtr/DSNewPtr
- •AZPtrAndHand/DSPtrAndHand
- •AZPtrToHand/DSPtrToHand
- •AZPtrToXHand/DSPtrToXHand
- •AZRecoverHandle/DSRecoverHandle
- •AZSetHandleSize/DSSetHandleSize
- •AZSetHSzClr/DSSetHSzClr
- •BinSearch
- •BlockCmp
- •Cat4Chrs
- •ClearMem
- •CPStrBuf
- •CPStrCmp
- •CPStrIndex
- •CPStrInsert
- •CPStrLen
- •CPStrRemove
- •CPStrReplace
- •CPStrSize
- •CToPStr
- •DateCString
- •DateToSecs
- •FAddPath
- •FAppendName
- •FAppPath
- •FArrToPath
- •FCopy
- •FCreate
- •FCreateAlways
- •FDepth
- •FDirName
- •FDisposePath
- •FDisposeRefNum
- •FEmptyPath
- •FExists
- •FFlattenPath
- •FFlush
- •FGetAccessRights
- •FGetDefGroup
- •FGetEOF
- •FGetInfo
- •FGetPathType
- •FGetVolInfo
- •FileNameCmp
- •FileNameIndCmp
- •FileNameNCmp
- •FIsAPath
- •FIsAPathOfType
- •FIsAPathOrNotAPath
- •FIsARefNum
- •FIsEmptyPath
- •FListDir
- •FLockOrUnlockRange
- •FMakePath
- •FMClose
- •FMOpen
- •FMove
- •FMRead
- •FMSeek
- •FMTell
- •FMWrite
- •FName
- •FNamePtr
- •FNewDir
- •FNewRefNum
- •FNotAPath
- •FPathCmp
- •FPathCpy
- •FPathToArr
- •FPathToAZString
- •FPathToDSString
- •FPathToPath
- •FRefNumToFD
- •FRefNumToPath
- •FRelPath
- •FRemove
- •FSetAccessRights
- •FSetEOF
- •FSetInfo
- •FSetPathType
- •FStrFitsPat
- •FStringToPath
- •FTextToPath
- •FUnFlattenPath
- •FVolName
- •GetALong
- •HexChar
- •HiByte
- •HiNibble
- •IsAlpha
- •IsDigit
- •IsLower
- •IsUpper
- •LoByte
- •Long
- •LoNibble
- •LStrBuf
- •LStrCmp
- •LStrLen
- •LToPStr
- •MilliSecs
- •MoveBlock
- •NumericArrayResize
- •Offset
- •PPStrCaseCmp
- •PPStrCmp
- •Printf
- •PStrBuf
- •PStrCaseCmp
- •PStrCat
- •PStrCmp
- •PStrCpy
- •PStrLen
- •PStrNCpy
- •PToCStr
- •PToLStr
- •QSort
- •RandomGen
- •SecsToDate
- •SetALong
- •SetCINArraySize
- •StrCat
- •StrCmp
- •StrCpy
- •StrLen
- •StrNCaseCmp
- •StrNCmp
- •StrNCpy
- •SwapBlock
- •TimeCString
- •TimeInSecs
- •ToLower
- •ToUpper
- •Unused
- •Word
- •Glossary
Chapter 4 Programming Issues for CINs
Examples with Scalars
The following examples describe how to create CINs that work with scalar data types. Refer to Chapter 3, CINs, for more information about creating CINs.
Creating a CIN That Multiplies Two Numbers
To create a CIN that takes two single-precision floating-point numbers and returns their product, complete the following steps.
1.Place the CIN on the block diagram.
2.Add two input and output terminals to the CIN.
3.Place two single-precision numeric controls and one single-precision numeric indicator on a front panel. Wire the node as shown in the following illustration. A*B is wired to an output-only terminal pair.
4.Save the VI as mult.vi.
5.Right-click the node and select Create .c File. LabVIEW prompts you to select a name and a storage location for a .c file.
6.Name the file mult.c. LabVIEW creates the following.c file:
/*
* CIN source file */
#include "extcode.h"
CIN MgErr CINRun (float32 *A, float32 *B, float32 *A_B);
CIN MgErr CINRun (float32 *A, float32 *B, float32 *A_B) {
/* ENTER YOUR CODE HERE */
return noErr;
}
Using External Code in LabVIEW |
4-4 |
www.ni.com |
Chapter 4 Programming Issues for CINs
This .c file contains a prototype and a template for the CINRun routine of the CIN. LabVIEW calls the CINRun routine when the CIN executes. In this example, LabVIEW passes CINRun the addresses of the three 32-bit floating-point numbers. The parameters are listed left to right in the same order as you wired them (top to bottom) to the CIN. Thus, A, B, and A_B are pointers to A, B, and A*B, respectively.
As described in the Parameters in the CIN .c File section earlier in this chapter, the float32 data type is not a standard C data type. For most C compilers, the float32 data type corresponds to the float data type. However, this may not be true in all cases, because the C standard does not define the sizes for the various data types. You can use these LabVIEW data types in your code because extcode.h associates these data types with the corresponding C data type for the compiler you are using. In addition to defining LabVIEW data types, extcode.h also prototypes LabVIEW routines you can access. Refer to the Manager Overview section for descriptions of these data types and routines.
7.For this multiplication example, fill in the code for the CINRun routine. You do not have to use the variable names LabVIEW gives you in CINRun; you can change them to increase the readability of the code.
CIN MgErr CINRun (float32 *A, float32 *B, float32 *A_B);
{
*A_B = *A * *B; return noErr;
}
CINRun multiplies the values to which A and B refer and stores the results in the location to which A_B refers. It is important CIN routines return an error code, so LabVIEW knows whether the CIN encountered any fatal problems and handles the error correctly.
If you return a value other than noErr, LabVIEW stops running the VI.
8.Compile the source code and convert it into a form LabVIEW can use. The following sections summarize the steps for each of the supported compilers. Refer to the Compile on Macintosh section in Chapter 3, CINs, for more information about completing this step on your platform.
(Macintosh Programmer’s Workshop) Create a file named mult.lvm. Make sure the name variable is set to mult. Build mult.lvm.
(Metrowerks CodeWarrior) Create a new project and place mult.c in it. Build mult.lsb.
© National Instruments Corporation |
4-5 |
Using External Code in LabVIEW |
Chapter 4 Programming Issues for CINs
(Microsoft Visual C++ Compiler Command Line and Symantec C for Windows)
Create a file named mult.lvm. Make sure the name variable is set to mult. Build mult.lvm.
(Microsoft Visual C++ Compiler IDE for Windows) Create a project.
(UNIX Compilers) Create a makefile using the shell script lvmkmf in the cintools directory. For this example, enter the following command:
lvmkmf mult
This creates a file called Makefile. After running lvmkmf, enter the standard make command, which uses Makefile to create a file called mult.lsb, which you can load into the CIN in LabVIEW.
9.Right-click the node and select Load Code Resource. Select mult.lsb, the object code file you created.
You should be able to run the VI. If you save the VI, LabVIEW saves the
CIN object code along with the VI.
Comparing Two Numbers, Producing a Boolean
Scalar
To create a CIN that compares two single-precision numbers, complete the following steps. If the first number is greater than the second one, the return value is TRUE; otherwise, the return value is FALSE. This example shows only the block diagram and the code.
1.To create the CIN, follow the instructions in the Creating a CIN section in Chapter 3, CINs.
The diagram for this CIN is shown in the following illustration.
2.Save the VI as aequalb.vi.
3.Create a .c file for the CIN and name it aequalb.c. LabVIEW creates the following.c file:
/*
* CIN source file */
#include "extcode.h"
Using External Code in LabVIEW |
4-6 |
www.ni.com |