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

SysMemCpy and number of bytes

gatto
2017-03-13
2017-03-14
  • gatto

    gatto - 2017-03-13

    I have this DUT

    TYPE DUT2 :
    STRUCT
       word1:DWORD;
       dword1:DWORD;
    END_STRUCT
    END_TYPE
    

    and this

    TYPE DUT3 :
    STRUCT
       by1:BYTE;
       wd1:WORD;
    END_STRUCT
    END_TYPE
    

    my pou for test is

    PROGRAM TEST
    VAR
       Src: DUT2 ;
       Dest: DUT2 ;
       N_of_bytes: __XWORD := 6;
    END_VAR
    
    SysMemCpy(
        pDest :=ADR(Dest),  (* destination *)    
        pSrc := ADR(Src),  (* source*)    
        udiCount := N_of_bytes,  (* N of bytes to copy *)    
    );
    

    Here my first question:
    i thinked one word + one doubleword = 6 byte but
    Why if i set N_of_bytes=6 i can't copy the doubleword right? just using 8 ?

    and my second question, using

    PROGRAM TEST
    VAR
       Src: DUT3 ;
       Dest: DUT3 ;
       N_of_bytes: __XWORD := 3;
    END_VAR
    

    i thinked one byte + one word = 3 byte but
    Why if i set N_of_bytes=3 i can't copy the word right? just using 4 ?

     
  • Anonymous - 2017-03-13

    Originally created by: scott_cunningham

    The problem is byte alignment that happens behind the scenes. In your first example, a DWORD can only start on a even, even byte (such as position 0 or 4 or 8, etc). But you list a WORD first and it takes position 0 and 1. A DWORD cannot start at position 2, so the compiler moves it to position 4. In this case your structure is 8 bytes (6 +2 dead bytes).

    Same idea is true for your second example (a WORD cannot start on an odd position).

    To avoid this problem, you should always use SIZEOF(structure) to define the number of bytes to copy. Do this also for "normal" variables to avoid platform specific size problems (not every hardware system has a byte taking only one space...)

    DUT2_BYTE_SIZE : __XWORD := SIZEOF(DUT2);
    
     
  • gatto

    gatto - 2017-03-14

    Great !
    Thanks a lot !
    Cheers.

     

Log in to post a comment.