Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Using External Code in LabVIEW.pdf
Скачиваний:
49
Добавлен:
29.05.2015
Размер:
1.85 Mб
Скачать

Chapter 4 Programming Issues for CINs

Examples with Variably Sized Data

The following examples describe how to create CINs that work with variably sized data types. Refer to Chapter 3, CINs, for more information about creating CINs.

Concatenating Two Strings

To create a CIN that concatenates two strings and use input-output terminals by passing the first string as an input-output parameter to the CIN, complete the following steps. The top right terminal of the CIN returns the result of the concatenation. This example shows only the 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 lstrcat.vi.

3.Create a .c file for the CIN and name it lstrcat.c. LabVIEW creates the following .c file.

/*

* CIN source file */

#include "extcode.h"

CIN MgErr CINRun(

LStrHandle var1,

LStrHandle var2);

CIN MgErr CINRun(

LStrHandle var1,

LStrHandle var2) {

/* ENTER YOUR CODE HERE */

return noErr;

}

Using External Code in LabVIEW

4-14

www.ni.com

Chapter 4 Programming Issues for CINs

4.Fill in the CINRun function, as follows:

CIN MgErr CINRun( LStrHandle strh1, LStrHandle strh2) {

int32 size1, size2, newSize; MgErr err;

size1 = LStrLen(*strh1); size2 = LStrLen(*strh2); newSize = size1 + size2;

if(err = NumericArrayResize(uB, 1L, (UHandle*)&strh1, newSize))

goto out;

/* append the data from the second string to first string */

MoveBlock(LStrBuf(*strh2), LStrBuf(*strh1)+size1, size2);

/* update the dimension (length) of the first string */

LStrLen(*strh1) = newSize; out:

return err;

}

In this example, CINRun is the only routine that performs substantial operations. CINRun concatenates the contents of strh2 to the end of strh1, with the resulting string stored in strh1.

5.Before performing the concatenation, NumericArrayResize resizes strh1 to hold the additional data.

If NumericArrayResize fails, it returns a non-zero value of type MgErr. In this case, NumericArrayResize could fail if LabVIEW does not have enough memory to resize the string. Returning the error code gives LabVIEW a chance to handle the error. If CINRun reports an error, LabVIEW aborts the calling VIs. Aborting the VIs might free up enough memory so LabVIEW can continue running.

After resizing the string handle, MoveBlock copies the second string to the end of the first string. MoveBlock is a support manager routine that moves blocks of data. Finally, this example sets the size of the first string to the length of the concatenated string.

© National Instruments Corporation

4-15

Using External Code in LabVIEW

Chapter 4 Programming Issues for CINs

Computing the Cross Product of Two

Two-Dimensional Arrays

To create a CIN that accepts two two-dimensional arrays and then computes the cross product of the arrays, complete the following steps. The CIN returns the cross product in a third parameter and a Boolean value as a fourth parameter. This Boolean parameter is TRUE if the number of columns in the first matrix is not equal to the number of rows in the second matrix. This example shows only the front panel, block diagram, and source code.

1.To create the CIN, follow the instructions in the Creating a CIN section in Chapter 3, CINs.

The front panel for this VI is shown in the following illustration.

The block diagram for this VI is shown in the following illustration.

2.Save the VI as cross.vi.

3.Save the .c file for the CIN as cross.c. Following is the source code for cross.c with the CINRun routine added.

/*

* CIN source file */

#include "extcode.h"

Using External Code in LabVIEW

4-16

www.ni.com

Chapter 4 Programming Issues for CINs

#define ParamNumber 2

/* The return parameter is parameter 2 */

#define NumDimensions 2

/* 2D Array */

/*

* typedefs */

typedef struct {

int32 dimSizes[2]; float64 arg1[1]; } TD1;

typedef TD1 **TD1Hdl;

CIN MgErr CINRun(TD1Hdl A, TD1Hdl B, TD1Hdl AxB, LVBoolean *error);

CIN MgErr CINRun(TD1Hdl A, TD1Hdl B, TD1Hdl AxB, LVBoolean *error) {

int32

i,j,k,l;

int32

rows, cols;

float64

*aElmtp, *bElmtp, *resultElmtp;

MgErr

err=noErr;

int32

newNumElmts;

if ((k = (*ah)–>dimSizes[1]) != (*bh)–>dimSizes[0]) {

*error = LVTRUE; goto out;

}

*error = LVFALSE;

rows = (*ah)–>dimSizes[0];

/* number of rows in a and result */ cols = (*bh)–>dimSizes[1];

/* number of cols in b and result */ newNumElmts = rows * cols;

if (err = SetCINArraySize((UHandle)AxB, ParamNumber, newNumElmts))

goto out; (*resulth)–>dimSizes[0] = rows; (*resulth)–>dimSizes[1] = cols; aElmtp = (*ah)–>arg1;

bElmtp = (*bh)–>arg1; resultElmtp = (*resulth)–>arg1; for (i=0; i<rows; i++)

for (j=0; j<cols; j++) { *resultElmtp = 0; for (l=0; l<k; l++)

*resultElmtp += aElmtp[i*k + l] *

© National Instruments Corporation

4-17

Using External Code in LabVIEW

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]