How to read Real Value on Modbus for codesys v3.5

Hello!
I have a device that I communicate with modbus tcp. The data it sent me is of real type. How should I read this data in codesys by converting?
Data is read word by word in codesys 3.5

  1. Create a DUT og type UNION with a Word array of size 2 and a REAL value.
  2. Copy the two modbus words (per real value) into the unions array.
  3. Read the REAL value out of the UNION directly.
2 Likes

1 Like

Why you use pointer ?

Another approach that uses overlapped memory, using the compiler to convert between WORD and REAL. Tip: make sure that the REAL is on an even byte boundary in the server.

VAR
   myWORD_LowByte AT %MW0 : WORD;
   myWORD_HighByte AT %MW1 : WORD; 
   myREAL AT %MW0 : REAL;
END_VAR
 
myWORD_LowByte := ModbusArray[0];
myWORD_HIghByte := ModbusArray[1];
myReal; // This will be the REAL Value
1 Like

I want to ask one more thing. What should I do when the incoming data is big endian or little endian?

You can map it to the byte level with the same approach, and change the mapping as needed.

Pl. check the different methods