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
- Create a DUT og type UNION with a Word array of size 2 and a REAL value.
- Copy the two modbus words (per real value) into the unions array.
- Read the REAL value out of the UNION directly.
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; myWORD_HIghByte := ModbusArray; myReal; // This will be the REAL Value
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.