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

Chapter 3 CIN Advanced Topics

In some cases, you may want globals for each reference to the code resource multiplied by the number of usages of the VI (if the VI is reentrant). For each instance of one of these globals, LabVIEW allocates the CIN data space for the use of the code interface node. Within the CINInit, CINDispose, CINAbort, and CINRun routines you can call the GetDSStorage routine to retrieve the value of the CIN data space for the current instance. You can also call SetDSStorage to set the value of the CIN data space for this instance.

You can use this storage location to store any 4-byte quantity that you want to have for each instance of one of these globals. If you need more than four bytes of global data, you can store a handle or pointer to a structure containing your globals.

The following two lines of code are examples of the exact syntax of these two routines, defined in extcode.h.

int32 GetDSStorage(void);

This routine returns the value of the 4-byte quantity in the CIN data space that LabVIEW allocates for each CIN code resource, or for each use of the surrounding VI (if the VI is reentrant). You should call this routine only from CINInit, CINDispose, CINAbort, or CINRun.

int32 SetDSStorage(int32 newVal);

This routine sets the value of the 4-byte quantity in the CIN data space that LabVIEW allocates for each CIN use of that code resource, or the uses of the surrounding VI, (if the VI is reentrant). It returns the old value of the 4-byte quantity in that CIN data space. You should call this routine only from CINInit, CINDispose, CINAbort, or CINRun.

Examples

The following two examples illustrate the differences between code globals and CIN data space globals. In both examples, the CIN takes a

© National Instruments Corporation

3-9

LabVIEW Code Interface Reference Manual

Chapter 3 CIN Advanced Topics

number and returns the average of that number and the previous numbers passed to it.

When you design your code, decide whether it is appropriate to use code globals or data space globals. If you use code globals, calling the same code resource from multiple nodes or different reentrant VIs will affect the same set of globals. In the code globals averaging example, the result will indicate the average of all values passed to the CIN.

If you use CIN data space globals, each CIN that calls the same code resource and each VI, if the VI is reentrant, can have its own set of globals. In the CIN data space averaging example, the results would indicate the average of values passed to a specific node for a specific data space.

If you have only one CIN referencing the code resource, and the VI that contains that CIN is not reentrant, it does not matter which method you choose.

Using Code Globals

The following code implements averaging using code globals. Notice that the variables are initialized in CINLoad. If the variables are dynamically created (that is, if they are pointers or handles), you can allocate the memory for the pointer or handle in CINLoad, and deallocate it in CINUnload. You can do this because CINLoad and CINUnload are called only once, regardless of the number of references to the code resources and the number of data spaces. Notice that the UseDefaultCINLoad macro is not used, because this .c file has a CINLoad function.

/*

* CIN source file */

#include "extcode.h"

LabVIEW Code Interface Reference Manual

3-10

© National Instruments Corporation

Chapter 3 CIN Advanced Topics

float64 gTotal; int32 gNumElements;

/* stubs for advanced CIN functions */

UseDefaultCINInit

UseDefaultCINDispose

UseDefaultCINAbort

UseDefaultCINUnload

UseDefaultCINSave

CIN MgErr CINRun(float64 *new_num, float64 *avg);

CIN MgErr CINRun(float64 *new_num, float64 *avg)

{

gTotal += *new_num; gNumElements++;

*avg = gTotal / gNumElements;

return noErr;

}

CIN MgErr CINLoad(RsrcFile rf)

{

gTotal=0;

gNumElements=0;

return noErr;

}

Using CIN Data Space Globals

The following is an alternative implementation of averaging using CIN data space globals. A handle for the global data is allocated in CINInit, and stored in the CIN data space storage using SetDSStorage. When LabVIEW calls the CINInit, CINDispose, CINAbort, or CINRun routines, it ensures that

GetDSStorage and SetDSStorage will return the 4 byte CIN data space value for that node or CIN data space.

When you want to access that data, use GetDSStorage to retrieve the handle and then dereference the appropriate fields (see the code for

© National Instruments Corporation

3-11

LabVIEW Code Interface Reference Manual

Chapter 3 CIN Advanced Topics

CINRun in the following example). Finally, in your CINDispose routine you need to dispose of the handle.

/*

* CIN source file */

#include "extcode.h"

/* stubs for advanced CIN functions */

UseDefaultCINAbort

UseDefaultCINLoad

UseDefaultCINUnload

UseDefaultCINSave

typedef struct {

 

float64

total;

int32

numElements;

} dsGlobalStruct;

 

CIN MgErr CINInit() { dsGlobalStruct **dsGlobals; MgErr err = noErr;

if (!(dsGlobals = (dsGlobalStruct **) DSNewHandle(sizeof(dsGlobalStruct))))

{

/* if 0, ran out of memory */ err = mFullErr;

goto out;

}

(*dsGlobals)->numElements=0; (*dsGlobals)->total=0;

SetDSStorage((int32) dsGlobals); out:

return noErr;

}

CIN MgErr CINDispose()

LabVIEW Code Interface Reference Manual

3-12

© National Instruments Corporation

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