In the course of developing a Modbus interface, I encountered the problem of not being able to load four separate byte definitions directly into a float variable. In my short investigation, I came up with the following, and I would like to share it with you as it might be useful to you in your projects. The LongAsFloat function was crucial to solving the problem.
When the bytes arrive serially in the order b1 (A), b2 (B), b3 (C), and b4 (D), they are rearranged according to AvrCo's float variable format using Float order - Mid-Little Endian (CDAB).
function bytes2Float(A, B, C, D : byte) : float;
var
f : float;
lwValue : longWord;
begin
lwValue := (longWord(C) shl 24) or (longWord(D) shl 16) or (longWord(A) shl 8 ) or longWord(B);
f := LongAsFloat(lwValue);
return(f);
end;
reference: https://www.scadacore.com/tool…converter/
When the bytes arrive serially in the order b1 (A), b2 (B), b3 (C), and b4 (D), they are rearranged according to AvrCo's float variable format using Float order - Mid-Little Endian (CDAB).
function bytes2Float(A, B, C, D : byte) : float;
var
f : float;
lwValue : longWord;
begin
lwValue := (longWord(C) shl 24) or (longWord(D) shl 16) or (longWord(A) shl 8 ) or longWord(B);
f := LongAsFloat(lwValue);
return(f);
end;
reference: https://www.scadacore.com/tool…converter/