floating point - PLC Structured text, convert decimal to real(float). Not getting the value I expect. (IEEE-754) -
i have hardware communicating plc on profibus dp, sends me 4 bytes of data in hex "44 79 ff ff" , in plc program have declared byte array input data. problem in plc data receive in byte array "66 121 255 255" decimal value of hexdecimal, goal convert value real , when i'm not getting value expected.
i have created dword(4bytesdata
) insert bytes into. before insert 4 bytes 4bytesdata
bytes are: in1 = 68
, in2 = 121
, in3 = 255
& in4 = 255
4bytesdata := (shl(shl(shl(byte_to_dword(in1), 8) or byte_to_dword(in2), 8) or byte_to_dword(in3), 8) or in4); realvalue := dword_to_real(4bytesdata);
where in1, in2, in3 & in4 byte 0-3.
the value i'm getting is; 4bytesdata
= 1148846079 & realvalue
= 1.148846e+009
the value i'm expecting realvalue
= 9.9999993896484375e2
if use website ( ieee754 analyzer/converter ) , converting hex value (4479ffff) i'm getting value want to, , if insert decimal value (1148846079) i'm getting same value receive in plc.
i hope understand problem, many in advance.
you've figured out, seems don't realize it. conversion dword_to_real
taking integer (hex) value stored in 4bytesdata
, converting ieee754 real format.
this not want do. 4479ffff
already in ieee754 real format - you're taking value, interpreting real dword, , converting dword value real. short answer don't need conversion - 4bytesdata
in correct format.
edit
follow on comments :
here functionblock2 delclares rin(real), rout(real) , sets rout:=rin;
. gets around forced typecasting of st. nothing changing data stored in d2000
here - it's same binary data. top function block storing memory , bottom 1 reading memory. difference top rung interpreting dword (for display purposes) , bottom rung interpreting real.
edit
i've been reading beckhoff manual. seems may have other options. try perhaps declaring dword , real @ same memory location. if not, perhaps pointers let it (not sure if type restrictions apply pointers beckhoff?)
Comments
Post a Comment