LabVIEW Code Interface Node (CIN) is a block diagram node that links C/C++ code to LabVIEW. Interface nodes make it possible for one to use algorithms written in a programming language or to access platform-specific features or hardware that LabVIEW does not directly support. CINs are resizable and also show data types for the connected inputs and outputs, Any number of arbitrary LabVIEW data type parameters can be passed to or from a CIN.
An example is used to demonstrate how to embed a C/C++ interpreter into LabVIEW using CIN. In this example, a virtual instrument generates two periodic signals, one is sine and the other is triangle. These two signals are then added together by a C script. The input sine and triangle signals of the VI are shown on the top left. The frequency, amplitude, and phase of the two input signals can be changed at the lower left. The two input signals are added together by the script shown at the lower right and displayed on the top right. The text file embedch.ch on the lower right is saved via the Save File button that is controlled by the icons at the bottom of the block diagram. The Ch script function with the prototype,
is used to process the signal. The array aptr[] contains the sine signal and array bptr[] contains the triangle signal in a certain length specified by the third argument len. To process the input signals with different algorithms, one only need to change the script function. For example, to subtract the triangle signal from the sine signal only the statement
in function func() needs to be changed to
The detailed steps to embed a Ch function into the LabVIEW using CIN are described as follows.
(1) To create the CIN and C/C++ file: Place a CIN on the block diagram, add the required input and output terminals, right-click the node and select Create.c File. A file will be created which contains a prototype and a template for the CINRun routine for file waveprocessCIN.c. In this example, the parameters are listed from the left to right in the same order from the top to bottom as they were wired to the CIN. Thus sinH and angH are structure and errorp is pointer type. They represent the sine and triangle signals and error message. The parameter sinH is both the input signal of the sine wave and the resulting output signal. The parameter angH is an input signal of a triangle wave. The complete code for the CINrun routine is shown in upward. The variable names can be changed for the readability of code. The values of sine and triangle waves are transferred to the parameters sinH and angH of structure type that contain the data arrays and length. The data arrays and length are passed to the Ch function fun() through variables sinElmtp and angElmtp of pointer type and len, respectively, by function Ch_CallFunByName(). The source code for func() function in file embedch.ch is shown in example1.
(2) Compile the C/C++ source code including Ch scripting engine and function: The program for a CIN uses APIs in Embedded Ch described in section 2.2. It must be compiled as a LabVIEW subroutine file with the file extension .lsb. After compiling the C/C++ code in one of the compilers that LabVIEW supports, we use a LabVIEW utility to put the object code into the .lsb format. Because the compiling process is often complex, LabVIEW includes utilities that simplify the process. These utilities take a simple specification for CIN and create object code which can be loaded into LabVIEW. These tools vary depending on the compiler used. In this example, the program is compiled in a Ch shell using Microsoft Visual Studio .NET. The compile and link processes are automated using a Makefile.
(3) Load the CIN object code: To load the code resource, right-click the node and select Load Code Resource. Select the .lsb file created in the last step. LabVIEW loads the object code into the memory and links the code to the current front panel or block diagram. After saving the VI, the file with extension .lsb containing the object code does not need to be resident on the computer running LabVIEW for the VI to run.