How can I convert Modbus 485 data received as 2 bytes into Float AB CD?

I received the DEC values 16800 and 0 using Modbus 485. When converted to HEX, they become 0x41 0xA0 0x00 0x00. After performing a bit shift, they become 0100 0001 1010 0000 0000 0000 0000 0000. Then, I can obtain the final float32 value of 20.0. However, how can I implement the Float AB CD logic in ST? Do you have any sample code?

Hi,

just memcopy the both UINTs in the right order into a REAL variable.

Best regards!

Thank you. It’s work well!
I wrote the code to convert float32 myself, but my code was very inefficient. Thank you very much for your code.

1 Like

This reminds me about one of my colleague work. :slight_smile:

Another efficient method is by “pointer”:

VAR
	MyReal : REFERENCE TO REAL;
	MyDec : ARRAY[0..1] OF UINT := [0,16800]; 
END_VAR
PROGRAM _INIT
	(* Insert code here *)
	MyReal ACCESS ADR(MyDec);
	
END_PROGRAM

PROGRAM _CYCLIC
	(* Insert code here *)
	 
END_PROGRAM

PROGRAM _EXIT
	(* Insert code here *)
	 
END_PROGRAM

Result:
image

Everything will change on the fly without copy and save processing power:
image

4 Likes

Hello, xyz
This is really good idea! Thank you!