Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

LREAL from four WORDs?

hajduk
2022-11-17
2022-11-18
  • hajduk

    hajduk - 2022-11-17

    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
  • paulpotat

    paulpotat - 2022-11-17

    I would create a type like this :

    TYPE LREAL_WORD :
      UNION
        value : LREAL;    // Needs 8 Bytes in memory
        regs : ARRAY [0..3] OF WORD;
      END_UNION
    END_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...

    Hope this helps !

     

    Related

    Talk.ru: 1
    Talk.ru: 2
    Talk.ru: 3

  • ignat - 2022-11-18

    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)

     

Log in to post a comment.