ScadaPack Floating Point Values

T

Thread Starter

Tom Van den Bon

I'm writing a c program which will be loaded onto the scadapack controller. But I need to write floating point values into registers. I understand that I need to write to two registers. How can I calculate the two values I need to write into the registers from a floating point value I have in my plc ?
 
You need to define a structure to split the data bits of the FP number up. This code goes just below where you type the global variables before the main body:

typedef union
{
unsigned short intValue[2];
float floatValue;
}
UF_UNION;

Now you need a function to load the registers with the given float value. This chunk of code goes just below Union def before main body:

void setRegFromFloat(unsigned short reg, float data)
{
UF_UNION value;
value.floatValue = data;
setdbase(MODBUS,reg,value.intValue[1]);
setdbase(MODBUS,reg+1,value.intValue[0]);
}

Now call the function from the main body like this:

request_resource(IO_SYSTEM);
setRegFromFloat(40001,1.2345);
release_resource(IO_SYSTEM);

Of course, you can use variables or pointers in the function call rather than hard numbers for the reg and data parameters. I've used this a lot in Micro-16s as well as Scadapack32s. Just remember to explicitly declare 16 bit integers as unsigned or signed short for the SP32 or you will get wild results.
 
Top