I am trying to read the floating variable via MODBUS RTU.
The read variables are in a word array. How can i convert 2 word into float/real?
Moreover I need to swap order of these words.
I know that I can do it via memory allocations but there is too many variables.
Thank for help.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
FUNCTIONTCDWordSwapped : DWORDVAR_INPUTrl : REAL;END_VARVARMyUnion : TypeCastDWReal;END_VARMyUnion.rl :=rl; // Input to function is REALTCDWordSwapped :=ROL(MyUnion.dw,16); // Function returns DWORD, needs word swap for ModbusTCP register space
Then I step through the modbus data array via pointer and byte offset, and pass the dwords/reals into the function block.
Hello,
I am new to the programming PLC field.
I am using TwinCat 2 (very similar to Codesys).
I am trying to read the floating variable via MODBUS RTU.
The read variables are in a word array. How can i convert 2 word into float/real?
Moreover I need to swap order of these words.
I know that I can do it via memory allocations but there is too many variables.
Thank for help.
Hi,
Maybe you should try to search this forum for "word to real".
There are more then several postings on this specific problem.
Good luck ;-)
https://forge.codesys.com/forge/talk/search/?q=word+to+real
Last edit: aliazzz 2020-08-15
I do this quite a bit. I use a union, and the ROL function depending on endianness.
The union. I called it 'TypeCast' because that was the equivalent function in LabVIEW.
Then I have two function that use the union depending on the direction you want the conversion done.
Then I step through the modbus data array via pointer and byte offset, and pass the dwords/reals into the function block.
This is reading a real input to my server.
This is writing real into the data array outbound on my client...
UNION is not supported in CODESYS v23 (TwinCAT2).