STRUCT to BYTE ARRAY

necati
2023-07-07
2023-07-10
  • necati - 2023-07-07

    Hi everybody,
    I have a struct below and would like to copy in an array (8) but I have a problem because of bools use 1 byte area in memory. This results data loss in array. How can I copy all struct value to array without data loss?
    Do you have any method?

    ~~~~~~~~~~~~~~~~~~~~~~~~STRUCT~~~~~~~~~~~~~~~~~~~~~~~~~

    TYPE test :
    STRUCT
    bool1: ARRAY[0..1] OF BOOL;
    bool2: ARRAY[0..2] OF BOOL;
    bool3: BOOL;
    bool4: BOOL;
    bool5: BOOL;
    bool6: ARRAY[0..5] OF BOOL;
    bool7: BOOL;
    bool8: BOOL;
    word1: WORD;
    word2: WORD;
    byte1: BYTE;
    byte2: BYTE;
    END_STRUCT
    END_TYPE

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    ~~~~~~~~~~~~~~~~~~~~~~~~ARRAY~~~~~~~~~~~~~~~~~~~~~~~~~
    byte_array: ARRAY [0..7] OF BYTE;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

     
  • ph0010421 - 2023-07-07

    the BIT datatype actually takes 1 bit, but it can't be in an ARRAY. That may work for you?

     
  • necati - 2023-07-10

    When I convert only one BOOL variable to BIT, it allocates 1 bit area in memory.It works as I need. But when tried to convert ARRAY OF BOOL to ARRAY OF BIT, it is not allowed by Codesys. I need same usage for ARRAYs.

     

Log in to post a comment.