STRUCT AT %MW1000

paulorb
2024-08-13
2024-08-13
  • paulorb - 2024-08-13

    Hi I am trying to use STRUCT to parse commands from Modbus/TCP, Modbus client will fill the bytes related to a specific command (type, parameters), then PLC (Codesys) will access the command parameters. A STRUCT will simplify a lot the process of parsing the data, so I don't need to parse byte per byte, address will be mapped to a struct and I can use it for parsing the data.

    From what I understood, the compiler does not allow me to specify the location of the struct for a %MW register.

    VAR_COMMAND AT %MW1002: structGenericModbusCommand;

    This will throw:

    Bad declaration, use '%ML' for 'structGenericModbusCommand' variable

    But If I change it to %ML, when you go online you see that it is actually allocating the struct in another memory address.

    When we go online, on GVL we can see:

    VAR_COMMAND.     structGenericModbusCommand     %ML1002
         commandType        COMMANDTYPE             %MB8016
         ...                                        %MB8020
    

    I am not specifying a pointer or a reference it is a struct type, why is it mapping to a %MW location (random one). How can I control where it is mapping to?

    I need a consistent way to map STRUCT to specific modbus address. Is it possible?

    Thanks,
    Paulo

    Note I am using a Codesys OEM: Schneider Machine Expert Logic Builder (M241)

     

    Last edit: paulorb 2024-08-13
  • This is normal and correct since in CoDeSys static addressing of variables uses the IEC method. A LONG tag has the size of 8 bytes, so %ML0 coincides with %MB0 ... %MB7; %ML1000 with %MB8000 ... %MB8007. (Similar reason for word and other formats).
    Also the reason the compiler won't let you map the structure to a %MW depends on the fact that the individual base elements are manipulated to 64 bits regardless of the smaller size.

    That said, it makes me strange that you cannot write a parser without making use of static memory allocation.

     

    Last edit: Gustavo Antonio Banchetti Brucatori 2024-08-13

Log in to post a comment.