Convert REAL to 2 WORDS, modbus

2014-02-19
2014-05-12
  • urbanlarsson - 2014-02-19

    Hi!

    I need tips on how to convert a REAL to 2 WORDS.

    I have some real variables I want to put into my modbus-slave-config, but it only supports Words, are there any built functions in codesys to do the convertion?

    /U

     
  • TimvH

    TimvH - 2014-02-19

    You could do it by using pointers. See below for an example to put the real value in a DWORD.
    Then you can split your dword into two single words and put them on the bus.
    I believe there is a library with a function which can do this more userfriendly, but I cannot find it that quickly. Maybe somebody else will respond with a better suggestion.

    VAR
    dwDword: DWORD;
    pDword: POINTER TO DWORD;
    rReal1: REAL;
    pReal: POINTER TO REAL;
    rReal2: REAL;
    END_VAR

    pDword := ADR(rReal1);
    dwDword := pDword^;

    (
    conversion back, just for testing
    pReal := ADR(dwDword);
    rReal2 := pReal^;
    )

     
  • ndzied1 - 2014-02-19

    There is an open source library by Oscat that has the
    REAL_TO_DW function which is written the same way as TimvH's example.

    There is a lot of good stuff in the library
    http://www.oscat.de/

    Another way I have done this is with overlapping marker addresses.

    VAR
    Β  Β MyReal AT %MW0: REAL;
    Β  Β MyBytes AT %MW0: ARRAY [0..3] OF BYTE;
    END_VAR
    

    Because both variables above are declared at the same marker address, you can write to MyReal with a real value and then send the array MyBytes to the modbus function as an array of byte values.

    You must make sure that your slave will understand that the real value is packed into 4 bytes. Also, I have had to use trial and error to determine which Word or Byte swapping the Modbus devices use. You may need to change the order of the byte elements so that the slave will understand them.

     
  • t.lundahl - 2014-02-20

    Check in the manual for: UNION

    I think this was made for two variable types to share same memory space.

    /TorbjΓΆrn Lundahl

     
  • Vuli - 2014-05-12

    Hey
    I was also dealing with Modbus and words only.

    Overlapping addresses is a general approach ... but you should let the system take care of addresses for you.

    Think in bytes :

    WORD 2 bytes
    DWORD 4 bytes
    REAL 4 bytes
    LREAL 8 bytes

    REAL to 2 WORD or 4 BYTES:

    RealValue : REAL;
    WordArray : ARRAY[0..1] OF WORD; (* array of 2 words *)
    MEMCPY( ADR(WordArray[0]), ADR(RealValue), 4); (* where 4 is number of bytes for REAL *)
    

    LREAL to 4 WORD or 8 BYTES:

    RealValue : LREAL;
    WordArray : ARRAY[0..3] OF WORD; (* array of 4 words *)
    MEMCPY( ADR(WordArray[0]), ADR(RealValue), 8); (* where 8 is number of bytes for LREAL *)
    
     

Log in to post a comment.