I have made a simple yet flexible function which uses the pointer to memory trick, it uses a user defined struct for it to work.
The code is made in CodesysV2.3 but will also work in Codesys V3 (but in V3 a Union is shorter though)
TYPE ST_BYTE_TO_REAL :
REAL_OUT : POINTER TO REAL;
The function fills the STRUCT and returns the correct decimal number from the byte array with use of a pointer.
(* The function assumes that the 4 bytes are allready formatted according to IEEE Standard 754 Short Real Number *)
FUNCTION BYTES_TO_REAL : REAL
(* Initialise POINTER to address of B0 *)
CONVERT.REAL_OUT := ADR(CONVERT.B0);
(* Fill bytes *)
(* Return ptr^ (value at adress) *)
BYTES_TO_REAL := CONVERT.REAL_OUT^;
(* END *)
The function can be adapted long reals (8 Bytes) or any other user specific need.