Functions

Allow converting the values of analog-natured points from raw counts to engineering units.

Devices registers are transmitted to the SCADA as counts or numeric values without a defined unit or magnitude, sometimes requiring scaling, inversions, floating point conversions, etc. For this reason it is often necessary to convert them using an appropriate function.

For this purpose a Function is used, containing a fragment of JavaScript code that will be executed taking the register’s value as input and returning the converted value as output.

This way we can convert the raw value 996 to 9.96 Volts by applying a Function that divides by 100.

Functions are also used in the Calculations section to perform operations with the value of one or more points or SQL statements.

Screenshot

Definition

  • Click New in the upper right area of the window
  • Fill in the following fields
    Field Description
    Name Name of the Function. It will be shown under this name in the corresponding column of the Points section
    Function Enter the JavaScript function to use. For point value conversions it must take a single argument; however if it is to be used with a calculation it must receive as many arguments as input values are required
  • Click Save
  • From now on the new function will be available to be assigned in the Points and Calculations sections

Function examples

Linear equations

Its formula will be of the type a*x+b; the function must be of the type:

function(x) { return m*x+b;	}

Replace the slope m and the y-intercept b with the corresponding values.

The raw unitless value coming from the register is taken as the input parameter x, and the ordinate y is calculated.

For example, if the input value is 123, the slope 2, and the y-intercept 5, the point’s value will be 251

32-bit Floating Point

Function to convert a 32-bit register (or two consecutive 16-bit MODBUS registers) when its content is in single-precision floating point format (1 sign bit, 8 exponent bits, and 23 mantissa bits).

 1    8               23
+-+--------+-----------------------+
|S|  Exp   |  Mantissa             |
+-+--------+-----------------------+
31 30    23 22                    0

function ieee32ToFloat(intval) {
    var fval = 0.0;
    var x;//exponent
    var m;//mantissa
    var s;//sign
    s = (intval & 0x80000000)?-1:1;
    x = ((intval >> 23) & 0xFF);
    m = (intval & 0x7FFFFF);
    switch(x) {
        case 0:
            //zero, do nothing, ignore negative zero and subnormals
            break;
        case 0xFF:
            if (m) fval = NaN;
            else if (s > 0) fval = Number.POSITIVE_INFINITY;
            else fval = Number.NEGATIVE_INFINITY;
            break;
        default:
            x -= 127;
            m += 0x800000;
            fval = s * (m / 8388608.0) * Math.pow(2, x);
            break;
		}
    return fval;
	}

16-bit Floating Point

Function to convert a 16-bit register when its content is in half-precision floating point format (1 sign bit, 5 exponent bits, and 10 mantissa bits).

 1    5      10
+-+-----+----------+
|S| Exp |  Mantissa|
+-+-----+----------+
15 14 10 9        0

function ieee16ToFloat(intval) {
    var exponent = (intval & 0x7C00) >> 10;
        fraction = intval & 0x03FF;
    return (intval >> 15 ? -1 : 1) * (
        exponent ?
        (
            exponent === 0x1F ?
            fraction ? NaN : Infinity :
            Math.pow(2, exponent - 15) * (1 + fraction / 0x400)
        ) :
        6.103515625e-5 * (fraction / 0x400)
    );

Function examples for the Calculations section

Sum of three values

This simple function adds up the value of three points. Unlike the previous functions, which were not intended for calculations, this one takes three input parameters.

function(a,b,c) { return a+b+c;	}

Apparent power

Function with two input arguments, where p is the active power and q the reactive power.

function(p,q) { return Math.sqrt(Math.pow(p,2)+Math.pow(q,2)); }