Float variable casting from bytes definition. [Solved]

What is the best way to load a float variable whose definition is split over four bytes?

JAG
Benutzer
Avatar
Gender:
Location: Mexico
Age: 59
Posts: 18
Registered: 07 / 2008
Subject:

Float variable casting from bytes definition. [Solved]

 · 
Posted: 13.08.2023 - 21:04  ·  #1
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/
UPI
 
Avatar
 
Subject:

Re: Float variable casting from bytes definition. [Solved]

 · 
Posted: 14.08.2023 - 15:40  ·  #2
Hi JAG,
thank you for sharing your knowledge with us.
Insights and information are what make a forum.

Regards
UPI
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   50   64 · Page-Gen-Time: 0.023536s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI