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

REAL to BYTE[],BYTE[] to REAL.

listenyang
2013-05-11
2017-02-23
  • listenyang - 2013-05-11

    hello, all

    how i can convert and save REAL to Byte[]? also from byte[] to REAL?
    I tested with SysMemCpy(), it goes in, but if the address of Byte[] elements are not continuous, then it might problem.

    br/
    listenyang

     
  • shooter - 2013-05-13

    you are talking about two different things
    real to byte is just REAL_TO_BYTE
    however you want to have the bytes this is done with pointers.

     
  • ndzied1 - 2013-05-21

    If you are just looking to pack the real data into an array of bytes, the easiest path seems like it would be to create your real and your byte array at the same marker address. Then they are actually at the same place in memory.

     
  • RudolfAtITD - 2013-05-22

    The most robust way is to use a UNION!

    TYPE uRealBytes :
      UNION
        rValue : REAL;    // Needs 4 Bytes in memory
        abValue:  ARRAY [0..3] OF BYTE;
      END_UNION
    END_TYPE
    

    Then you kann fill in a real by "uRealByte.rValue := <some real="">;
    In "uRealByte.abValue[n] you have the Bytes to copy to ...
    The conversion back is done in the same way.</some>

     
  • ndzied1 - 2013-05-22

    Are there Unions in V2? My brain doesn't think in V3 yet

     
  • listenyang - 2013-05-23

    shooter hat geschrieben:
    you are talking about two different things
    real to byte is just REAL_TO_BYTE
    however you want to have the bytes this is done with pointers.

    thanks your reply, REAL need 4 bytes, so i need fill in a BYTE[0..3], NOT into a single BYTE.

     
  • listenyang - 2013-05-23

    RudolfAtITD hat geschrieben:
    The most robust way is to use a UNION!

    TYPE uRealBytes :
      UNION
        rValue : REAL;    // Needs 4 Bytes in memory
        abValue:  ARRAY [0..3] OF BYTE;
      END_UNION
    END_TYPE
    

    Then you kann fill in a real by "uRealByte.rValue := <some real="">;
    In "uRealByte.abValue[n] you have the Bytes to copy to ...
    The conversion back is done in the same way.</some>

    thanks you reply, this is great!

     
  • CME - 2013-06-11

    I’m not familiar with UNION. But I like to test this.
    Where do I define this Union type?
    I tried under “Data Types” but then I get the error “unknown type:UNION”

     
  • shooter - 2013-06-11

    union does exist in V3 not in V2.3

     
  • jzhvymetal - 2013-06-20

    This can easily be done be using fixed mapping to your variables. Most CoDeSys controllers have fixed memory address. If you map a REAL and Byte array to the same memory location it will all you read the REAL as individual bytes. This method will work in all versions of CoDeSys. Below is an example how:

    myReal AT %MD0 : REAL;
    myByteArary AT %MB0 : ARRAY[0..3] OF BYTE;

    To give you a better understanding how the memory map works refer to my other post for an excel sheet.

    l viewtopic.php?f=2&t=5045#p8228 l

     
  • hermsen

    hermsen - 2017-02-23

    I have made a simple yet flexible function which uses the pointer to memory trick, it uses a user defined struct for it to work.
    The code is made in CodesysV2.3 but will also work in Codesys V3 (but in V3 a Union is shorter though)

    TYPE ST_BYTE_TO_REAL :
    STRUCT
       B0:   BYTE;
       B1:   BYTE;
       B2:   BYTE;
       B3:   BYTE;
       REAL_OUT : POINTER TO REAL;
    END_STRUCT
    END_TYPE
    

    The function fills the STRUCT and returns the correct decimal number from the byte array with use of a pointer.

    (* The function assumes that the 4 bytes are allready formatted according to IEEE Standard 754 Short Real Number *)
    FUNCTION BYTES_TO_REAL : REAL
    VAR_INPUT
       B3:   BYTE;
       B2:   BYTE;
       B1:   BYTE;
       B0:   BYTE;
    END_VAR
    VAR
       CONVERT: ST_BYTE_TO_REAL;
    END_VAR
    (* Initialise POINTER to address of B0 *)
    CONVERT.REAL_OUT := ADR(CONVERT.B0);
    (* Fill bytes *)
    CONVERT.B0:=B0;
    CONVERT.B1:=B1;
    CONVERT.B2:=B2;
    CONVERT.B3:=B3;
    (* Return ptr^ (value at adress) *)
    BYTES_TO_REAL := CONVERT.REAL_OUT^;
    (* END *)
    

    The function can be adapted long reals (8 Bytes) or any other user specific need.
    Good luck!

     
  • shooter - 2017-02-23

    the problem was in 2013, however it is a solution.
    have a look at oscat.de for more functions.

     

Log in to post a comment.