TYPEDUT2:STRUCTÂ Â word1:DWORD;Â Â dword1:DWORD;END_STRUCTEND_TYPE
and this
TYPEDUT3:STRUCTÂ Â by1:BYTE;Â Â wd1:WORD;END_STRUCTEND_TYPE
my pou for test is
PROGRAMTESTVARÂ Â Src:DUT2;Â Â Dest:DUT2;Â Â N_of_bytes:__XWORD:=6;END_VAR
SysMemCpy(  pDest:=ADR(Dest), (*destination*)    pSrc:=ADR(Src), (*source*)    udiCount:=N_of_bytes, (*Nofbytestocopy*)  );
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
PROGRAMTESTVARÂ Â 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 ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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);
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I have this DUT
and this
my pou for test is
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
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 ?
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...)
Great !
Thanks a lot !
Cheers.