Embedding an Interpreter via CIN Block

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.

The block diagram for the VI for processing sine and triangle signals
#include "extcode.h" #include <stdio.h> #include <string.h> #include <stdlib.h> #include <embedch.h> #include <windows.h> typedef struct { int32 dimSize; float64 arg1[1]; } TD1; typedef TD1 **TD1Hdl; CIN MgErr CINRun(TD1Hdl Y1, TD1Hdl Y2, LVBoolean *error); CIN MgErr CINRun(TD1Hdl sinH, TD1Hdl angH, LVBoolean *errorp) { ChInterp_t interp; int status,k; char *argvv[]={"embedch.ch", NULL}; int32 len; float64 *sinElmtp, *angElmtp; MgErr mgError=noErr; *errorp = LVTRUE; if ((k = (*sinH)->dimSize) != (*angH)->dimSize) { *errorp = LVFALSE; return 0; } len = (*sinH)->dimSize; /* number of rows in a and result */ sinElmtp = (*sinH)->arg1; angElmtp = (*angH)->arg1; /* initialize embedded Ch */ Ch_Initialize(&interp, NULL); /* run an Embedded Ch program indicated by argvv */ status = Ch_RunScript(interp, argvv); status = Ch_CallFuncByName(interp, "fun", NULL, sinElmtp, angElmtp, len); Ch_End(interp); return noErr; }

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,

Void fun(double *aptr, double *bptr, int len);

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

aptr[i] += bptr[i];

in function func() needs to be changed to

aptr[i] = bptr[i];

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.

A CIN block for the script function func().

(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.

Main Page