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
Geschlecht:
Herkunft: Mexico
Alter: 61
Beiträge: 18
Dabei seit: 07 / 2008
Betreff:

Float variable casting from bytes definition. [Solved]

 · 
Gepostet: 13.08.2023 - 21:04 Uhr  ·  #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
 
Betreff:

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

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

Regards
UPI
Gewählte Zitate für Mehrfachzitierung:   0

Registrierte in diesem Topic

Aktuell kein registrierter in diesem Bereich

Die Statistik zeigt, wer in den letzten 5 Minuten online war. Erneuerung alle 90 Sekunden.
MySQL Queries: 15 · Cache Hits: 14   50   64 · Page-Gen-Time: 0.031014s · Speichernutzung: 2 MB · GZIP: ein · Viewport: SMXL-HiDPI