Echelon Neuron C User Manual

Page 92

Advertising
background image

80

How Devices Communicate Using Network Variables

// TODO: If needed by the application algorithm, transform

// the raw *pFloat NV value into the scaled float equivalent

// using the following formula:

// scaled = A * 10**B * (*pFloat + C)

// Scaling factors are accessible via the scaling_factor_X

// members of the SCPTnvType CP, for example

// nvo1::nvType.scaling_factor_a. This transformation is a

// costly operation and it is recommended to design

// the application algorithm such that this conversion

// is not required at all, if possible.

}
} // GetCurrent()
void SetCurrent(float_type* pFloat) {

// One union to hold all possible current types, plus the initial

// type of the changeable NV.

union

{

unsigned

long

uLong;

signed long sLong;

SNVT_volt_f xInitial;

}

nvLocal;


boolean

bConversionOK;

boolean

bProcessABC;


bConversionOK = TRUE;

bProcessABC = nvoVolt::nvType.type_category == NVT_CAT_SIGNED_LONG

|| nvoVolt::nvType.type_category == NVT_CAT_UNSIGNED_LONG;


if (bProcessABC) {

// TODO: if needed by the application algorithm, revert the

// conversion done in GetCurrent() by using the following

//

formula:

// raw = (*pFloat / (A * 10**B)) - C

// See GetCurrent(), above, for more details.

}

switch (nvoVolt::nvType.type_category) {

case

NVT_CAT_SIGNED_LONG:

// Current type is signed long. Convert from float.

nvLocal.sLong

=

fl_to_slong(pFloat);

break;
case

NVT_CAT_UNSIGNED_LONG:

// Current type is unsigned long. Convert from float.

nvLocal.uLong

=

fl_to_ulong(pFloat);

break;
case NVT_CAT_INITIAL:

// Fall through to float.

case

NVT_CAT_FLOAT:

// Float is current. No conversion is required, just

// copy data into local variable.

nvLocal.xInitial

=

*pFloat;

break;
default:

// Unsupported type. The changeLength() handler should

// have recognized this and rejected the type earlier.

// Log this application error and set the device offline:

error_log(TYPE_ERROR);
go_offline();
bConversionOK

=

FALSE;

} // switch


if (bConversionOK) {

// Update the actual network variable in case the conversion

// was OK (current type is in fact supported).

// A more generic implementation of these conversion functions

// is likely to use a pointer to the changeable type network

// variable's initial type as a second argument, thus allowing

// the SetCurrent() and GetCurrent() functions to be used for

// all changeable type NVs of the same initial type.

// This approach is likely to require explicit calls to the

// propagate() function; see the Neuron C Reference Guide

Advertising