Codesys 3.5.9.2. I recieve data to an Array. Then i use Sysmemcpy to copy it to a structure .But with real and string it misses a byte. So 16 bytes i have to copy to a string(15). WHY ?? For real i have to copy byte 4-7 to get the right value.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Codesys 3.5.9.2. I recieve data to an Array. Then i use Sysmemcpy to copy it to a structure .But with real and string it misses a byte. So 16 bytes i have to copy to a string(15). WHY ?? For real i have to copy byte 4-7 to get the right value.
It might be because of pack mode, which causes padding bytes between structure variables.
Try to add
{attribute 'pack_mode' := '1'}
above the struct definition. In pack mode 1, there are no padding bytes and the copy should work.
Example (from Beckhoff site, https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/2529746059.html&id=3686945105176987925):
Thanks ! But same problem. if there is a real or string its a problem.
{attribute 'pack_mode' := '1'}
TYPE stNIR_400_Ethernet :
STRUCT
usiDHCP :USINT;
sIP_Adres :STRING(16);
sIP_Sub :STRING(16);
sIP_Gate :STRING(16);
sIP_Server :STRING(16);
sGauge_ID :STRING(65);
usiDiag_Dump :USINT;
sDiag_UDP_Adr :STRING(16);
sProfinet :STRING(65);
arrDummy :ARRAY[1..37] OF USINT;
uiCRC :UINT ;
END_STRUCT
END_TYPE
Remember that STRING takes always one extra byte, as there is always a string end byte. So STRING(16) takes 17 bytes.
Why is there a string end ? I try now with AT %MD Hopefully it will solve my problem.
It's how C-style strings work. The system needs to know somehow when it ends and that is done by adding a 0 byte to the end of the text.
https://en.wikipedia.org/wiki/Null-terminated_string