Unpacking an array to MODBUS addresses

2018-06-13
2018-06-19
  • JoshuaChenault - 2018-06-13

    hello, new to CODESYS.

    I've learned how to pack an array with addresses so i can indirectly address them a use them in loops. the problem i have now is that i dont understand how to unpack the array to an output address.

    example:
    {putting stuff in an array}
    CH0ReadDataArray AT %IW12 : ARRAY[0..127] OF BYTE;
    WordArray : ARRAY[0..127] OF WORD

    {pack Bytes into Words}
    for i:0 to 127 do
    WordArray[i]:=(BYTE_TO_WORD(CH0ReadDataArray[i])*256)+BYTE_TO_WORD(CH0ReadDataArray[i]);
    end for;

    Now how do i fill %QW406 through %QW469 with the contents of WordArray[0..127]??????

    PS. i want to do this quickly and efficiently. i don't want to:
    %QW406 := WordArray[0];
    %QW407 := WordArray[1];
    .
    .
    .
    etc.

     

    Related

    Talk.ru: 1

  • Anonymous - 2018-06-19

    Originally created by: scott_cunningham

    Something like the code below should work. Double check that your pointer math is working correctly and you don’t go one too many points in the loop, etc.

    PWrd : POINTER TO WORD;
    pWrd := ADR(%QW406);
    FOR J := 0 TO 127 DO
    Β  Β pWrd^ := WordArray[J];
    Β  Β pWrd := pWrd + SIZEOF(WORD);
    END_FOR
    

    Code not tested and from memory!

    PS execution time is actually longer than if you explicitly list out all of the assignments like you don’t want to.

     

Log in to post a comment.