How to convert UDINT to array of bytes ?

vformanek
2023-03-06
2023-03-10
  • sedoerr - 2023-03-06

    use UDINT_TO_DWORD and then use Library "SysMem.SysMemCopy" to copy your DWORD onto your Array of Bytes.

     
  • vformanek - 2023-03-06

    could you provide a screenshot solution of the block call ? i seem to get exceptions about access violation when running the program

     
  • sedoerr - 2023-03-06

    Here you go

     

    Last edit: sedoerr 2023-03-06
  • TimvH

    TimvH - 2023-03-08

    You could also create a DUT of the Type Union.
    In the Union add an array of 4 bytes and a UDINT.

    Then create a variable of this type.
    Write to the UDINT of this union variable and read the array of bytes.

    See https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_datatype_union.html

     
    • vformanek - 2023-03-10

      I have tried this approach, but it seems to be inconsistent. Here is the code i have come with:

      TYPE uDINTBytes :
        UNION
          rValue: UDINT; 
          abValue: ARRAY [0..3] OF BYTE;
        END_UNION
      END_TYPE
      

      But here is the output whitch is wrong.

       
  • TimvH

    TimvH - 2023-03-10

    2172731138 = hex 81 81 3F 02
    hex 02 = dec 2
    hex 3F = dec 63
    hex 81 = dec 129

    So this seems okay to me.

     
    • vformanek - 2023-03-10

      nevermind, I did something wrong in the conversion, my bad. Thanks!

       

Log in to post a comment.