I need to take 4 registers and assemble them to a LREAL. I don't know bit order so I may been have to invert order.
What's the best approach to this? Tried and don't think Example_LREAL AT %IW4 : LREAL will read in the 4 WORDs.. Do I create an array, address that memory pointer and read it in that way?
Looking for advice/example, have never manipulated LREALs before..
Last edit: hajduk 2022-11-17
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
TYPE LREAL_WORD : UNION value : LREAL; // Needs 8 Bytes in memory regs : ARRAY [0..3] OF WORD; END_UNIONEND_TYPE
Then in your program you can declare your variables like this :
my_var:LREAL_WORD;reg_1:WORD;reg_2:WORD;reg_3:WORD;reg_4:WORD;// ...// Maybe you'll have to change the order here...my_var.regs[0]:=reg_1;my_var.regs[1]:=reg_2;my_var.regs[2]:=reg_3;my_var.regs[3]:=reg_4;
Then it will automatically update my_var.value to the corresponding LREAL value (Beware of the endianess of the value though).
It should work but I've never tested this with WORD, only did it with BYTE...
if it is about modbus you could try first the following:
1) in the settings of the channel set length of reading 4. So in the Mapping it shows like ARRAY[0..3] OF WORD
2) when map this value directly to LREAL variable.
Must be tested, but it works for me for REAL values. In the channel settings like ARRAY [0..1] OF WORD but can be directly mapped to REAL var (Codesys > 3.5.12)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I need to take 4 registers and assemble them to a LREAL. I don't know bit order so I may been have to invert order.
What's the best approach to this? Tried and don't think Example_LREAL AT %IW4 : LREAL will read in the 4 WORDs.. Do I create an array, address that memory pointer and read it in that way?
Looking for advice/example, have never manipulated LREALs before..
Last edit: hajduk 2022-11-17
I would create a type like this :
Then in your program you can declare your variables like this :
Then it will automatically update
my_var.value
to the corresponding LREAL value (Beware of the endianess of the value though).It should work but I've never tested this with WORD, only did it with BYTE...
Hope this helps !
Related
Talk.ru: 1
Talk.ru: 2
Talk.ru: 3
if it is about modbus you could try first the following:
1) in the settings of the channel set length of reading 4. So in the Mapping it shows like ARRAY[0..3] OF WORD
2) when map this value directly to LREAL variable.
Must be tested, but it works for me for REAL values. In the channel settings like ARRAY [0..1] OF WORD but can be directly mapped to REAL var (Codesys > 3.5.12)