Structure Bug

2021-09-20
2021-09-22
  • Reinier Geers - 2021-09-20

    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.

     
  • Quirzo - 2021-09-21

    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):

    {attribute 'pack_mode' := '1'} 
    TYPE ST_MyStruct:  
    STRUCT 
        bEnable         : BOOL;
        nCounter        : INT; 
        nMaxSize        : INT;
        bMaxSizeReached : BOOL;
        bReset          : BOOL;
    END_STRUCT
    END_TYPE 
    
     
    • Reinier Geers - 2021-09-21

      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

       
  • Quirzo - 2021-09-21

    Remember that STRING takes always one extra byte, as there is always a string end byte. So STRING(16) takes 17 bytes.

     
    • Reinier Geers - 2021-09-21

      Why is there a string end ? I try now with AT %MD Hopefully it will solve my problem.

       
  • Quirzo - 2021-09-22

    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

     

Log in to post a comment.