Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Labview Code Interface Reference Manual.pdf
Скачиваний:
33
Добавлен:
29.05.2015
Размер:
1.13 Mб
Скачать

Chapter 2 CIN Parameter Passing

The data is declared as a one-dimensional C-style array. LabVIEW stores data row by row, as shown in the following illustration.

For an array with r rows and c columns, you can access the element at row i and column j as shown in the following code fragment.

value = (*arrayh)->arg1[i*c + j];

Working with Clusters

The following example takes an array of clusters and a single cluster as inputs, and the clusters contain a signed 16-bit integer and a string. The input for the array of clusters is an input-output terminal. In addition to the array of clusters, the CIN returns a Boolean and a signed 32-bit integer. If the cluster value is already present in the array of clusters, the CIN sets the Boolean to TRUE and returns the position of the cluster in the array of clusters using the 32-bit integer output. If the cluster value is not present, the CIN adds it to the array, sets the Boolean output to FALSE, and returns the new position of the cluster in the array of clusters.

This example shows only the front panel, block diagram, and source code. Follow the instructions in the Steps for Creating a CIN section of Chapter 1, CIN Overview, to create the CIN.

LabVIEW Code Interface Reference Manual

2-22

© National Instruments Corporation

Chapter 2 CIN Parameter Passing

The front panel for this VI is shown in the following illustration. Save the VI as tblsrch.vi.

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

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

/*

* CIN source file */

#include "extcode.h"

#define ParamNumber 0

/* The array parameter is parameter 0 */

/* stubs for advanced CIN functions */

UseDefaultCINInit

© National Instruments Corporation

2-23

LabVIEW Code Interface Reference Manual

Chapter 2 CIN Parameter Passing

UseDefaultCINDispose

UseDefaultCINAbort

UseDefaultCINLoad

UseDefaultCINUnload

UseDefaultCINSave

/*

* typedefs */

typedef struct { int16 number; LStrHandle string; } TD2;

typedef struct {

int32 dimSize; TD2 arg1[1];

} TD1;

typedef TD1 **TD1Hdl;

 

CIN MgErr CINRun(

 

 

TD1Hdl

clusterTableh,

 

TD2

*elementp,

 

LVBoolean

*presentp,

 

int32

*positionp);

CIN MgErr CINRun(

 

 

TD1Hdl

clusterTableh,

 

TD2

*elementp,

 

LVBoolean

*presentp,

 

int32

*positionp) {

int32

size,i;

 

MgErr

err=noErr;

 

TD2

*tmpp;

 

LStrHandle

newStringh;

 

TD2

*newElementp;

int32

newNumElements;

size = (*clusterTableh)->dimSize; tmpp = (*clusterTableh)->arg1;

LabVIEW Code Interface Reference Manual

2-24

© National Instruments Corporation

Chapter 2 CIN Parameter Passing

*positionp = -1; *presentp = LVFALSE;

for(i=0; i<size; i++) { if(tmpp->number == elementp->number)

if(LStrCmp(*(tmpp->string), *(elementp->string)) == 0)

break;

tmpp++;

}

if(i<size) { *positionp = i; *presentp = LVTRUE; goto out;

}

newStringh = elementp->string; if(err = DSHandToHand((UHandle *)

&newStringh))

goto out;

newNumElements = size+1; if(err =

SetCINArraySize((UHandle)clusterTableh,

ParamNumber, newNumElements)) {

DSDisposeHandle(newStringh); goto out;

}

(*clusterTableh)->dimSize = size+1;

newElementp = &((*clusterTableh) ->arg1[size]);

newElementp->number = elementp->number; newElementp->string = newStringh;

*positionp = size; out:

return err;

}

© National Instruments Corporation

2-25

LabVIEW Code Interface Reference Manual

Chapter 2 CIN Parameter Passing

In this example, CINRun is the only routine that performs substantial operations. CINRun first searches through the table to see if the element is present. CINRun then compares string components using the LabVIEW routine LStrCmp, which is described in the Online Reference. If CINRun finds the element, the routine returns the position of the element in the array.

If the routine does not find the element, you have to add a new element to the array. Use the memory manager routine DSHandToHand to create a new handle containing the same string as the one in the cluster element that you passed to the CIN. CINRun then increases the size of the array using SetCINArraySize and fills the last position with a copy of the element that you passed to the CIN.

If the SetCINArraySize call fails, the CIN returns the error code returned by the manager. If the CIN is unable to resize the array, LabVIEW disposes of the duplicate string handle.

LabVIEW Code Interface Reference Manual

2-26

© National Instruments Corporation

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