Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Word to array of byte

arek1996
2022-09-21
2022-09-22
  • arek1996

    arek1996 - 2022-09-21

    Hello,

    First of all I'm new in codesys. My problem is converting values (which I will type on touchscreen) on array of bytes. I have Array of 6 bytes which include 3 parametrs. How can I convert values (word) into bytes? Unfortunately there is no FB for this converting. Please help. I use this for acyclic data in Profinet (wrrec FB).

     
  • paulpotat

    paulpotat - 2022-09-22

    Easiest way to do this is to create a type like this :

    TYPE uWordBytes :
      UNION
        rValue : WORD;    // Needs 2 Bytes in memory
        abValue: ARRAY [0..1] OF BYTE;
      END_UNION
    END_TYPE
    

    Then in your program you can declare your variables like this :

    my_var : uWordBytes;
    my_var_as_word : WORD;
    
    // ...
    
    my_var.rValue := my_var_as_word;
    

    Then it will automatically fill the array my_var.abValue with the corresponding bytes ! You can access those bytes the same way :

    x[0] := my_var.abValue[0]; // First byte
    x[1] := my_var.abValue[1]; // Second byte
    

    Beware of the endianess of the value though, I don't remember if it's little endian or big endian...

    By the way to create a new type you have to do : right-click on application --> Add Object --> DUT...

    Hope this helps !

     
    πŸ‘
    1

Log in to post a comment.