Displaying value below than 1 in Codesys calculation

Hi, I have a problem here. I already got the raw value from PLC and trying to convert it to the actual value of the sensor with scaling value given. When I calculated manually, the value that I received is around 0.65 means below than 1. But the Codesys assume it automatically as 0. Can someone help me with this problem? I am currently using LReal as my datatype. Is there any other suitable datatype to read value “0.65”. Thanks.

BR,
Umair

Hi Umair, To scale your value to an LREAL or REAL value, you also have to convert all values in this calculation to LREAL, which will look like this:

Best Regard
Michael

1 Like

(As mentionned by Michael, the trick is in the .0 in the divider)

1 Like

Hi Michael, how do I actually convert all values in this calculation to LREAL ? I am currently using Continuous Function Chart (CFC) to calculate the scale.

BR,
Umair

What do you mean by .0?

to convert the Word value wAI1_IP201 to an LREAL you can type this in the input-block:
TO_LREAL(wAI1_IP201) this command will convert the value.
an to tell the programm to use an fixed value for example 4000 as an LREAL, you have to ad .0 so that the value is 4000.0.
This works with all IEC programming languages.

Great! It really works! Thanks for this new knowledge!

And another question, why am I facing this problem on for values below than 1? Is it because the datatype LREAL cannot deal with values below than 1? Because my other sensor can read perfectly without needing to convert to LREAL and so on.

I recommend using the SCALE_R library (from OSCAT).

FUNCTION SCALE_R : REAL
VAR_INPUT
	X : REAL;
	I_LO : REAL;
	I_HI : REAL;
	O_LO : REAL;
	O_HI : REAL;
END_VAR

SCALE_R := (O_HI - O_LO) / (I_HI - I_LO) * (LIMIT(I_LO, X, I_HI) - I_LO) + O_LO;

image