#8 Interface to manage mappings of "ANY" datatype

Unlicensed
nobody
None
2024-02-05
2020-09-25
Ingo
No

There is a nice article, called The wonders o ANY. While I learnt how to use this datatype, I wanted to share an example interface with you.

The idea is, that I want to pass a list of variable mappings, containing informations about the variables, to a function block, that can process them.

use-cases:

  • communication protocols
  • parsers or generators for data exchange formats (XML, JSON, YAML, ...)
FUNCTION_BLOCK mappingEntry
VAR
    // the type of the actual parameter
    typeclass : __SYSTEM.TYPE_CLASS ;
    // the pointer to the actual parameter
    pvalue : POINTER TO BYTE;
    // the size of the data, to which the pointer points
    diSize : DINT;
END_VAR

METHOD FB_Init: BOOL
VAR_INPUT
    bInitRetains: BOOL; // TRUE: the retain variables are initialized (reset warm / reset cold)
    bInCopyCode: BOOL;  // TRUE: the instance will be copied to the copy code afterward (online change)   
    map : ANY;
END_VAR

typeClass := map.TypeClass;
pValue := map.pValue;
diSize := map.diSize;
FUNCTION_BLOCK MyMappingFB
METHOD FB_Init: BOOL
VAR_INPUT
    bInitRetains: BOOL; // TRUE: the retain variables are initialized (reset warm / reset cold)
    bInCopyCode: BOOL;  // TRUE: the instance will be copied to the copy code afterward (online change)   
    mapping : POINTER TO ARRAY [1..0] OF mappingEntry;
    size : DWORD;
END_VAR

And the usage can then be:

VAR_GLOBAL
    hugo : DWORD;
    egon : BYTE;
    balder : STRING;
END_VAR
PROGRAM PLC_PRG
VAR
    mappingList : ARRAY [0..2] OF mappingEntry[
        (GVL.hugo),
        (GVL.egon),
        (GVL.balder)
    ];
    myMappingInst : MyMappingFB(ADR(mappingList), SIZEOF(mappingList) / SIZEOF(mappingEntry));
END_VAR

myMappingInst();

What you get in your "MyMappingFB" is:

  • A list of ANY variables (type: mappingEntry)
  • Each member contains info about:
    • Variable Type
    • Size
    • Pointer to the variable

Discussion

  • David

    David - 2022-01-13

    Hello Ingo, nice example, thank you.

     
  • bnewman - 2024-02-05

    I don't understand the point of this. Isn't just better to use __SYSTEM.AnyType instead of making your own type?

    Also if you wanted to make it an object, shouldn't you just wrap a FB around __SYSTEM.AnyType and then have some conversion Methods?

    Maybe I am missing the purpose of this code. Maybe add more details on how you would use MyMappingFB

     
    👍
    1

    Last edit: bnewman 2024-02-05

Log in to post a comment.