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

Structure to Dword, Dword to structure, using pointers

FAn
2013-07-02
2013-07-03
  • FAn - 2013-07-02

    I want to prepare a function block that will process different structures (every structure containing up to 32 bools)

    Now I'm trying to do something like this:
    StructIn : Struct;
    StructInPt: pointer to Struct;
    StructInDw: Dword;
    StructOutDw: Dword;
    StructOutPt: pointer to DWORD;
    StructOut: Struct;

    StructInPt := adr StructIn;
    StructInDw:= StructInPt^;
    till here is fine, then I call the function block where I process the data as Dword and get output as dword
    StructOutPt:= adr StructOutDw;
    StructOut:= StructOutPt^;

    I've done it with FBD so maybe I have some syntax error in the above example but anyway conversion from structure to Dword seems to work fine, but when I convert back from Dword to structure I get some bools that have wrong values. What am I doing wrong?
    Any other ways to solve this?

     
  • shooter - 2013-07-02

    why done with all pointers, please stay in normal programming unless it is impossible.
    you can use a filter like WORD AND 000010000 etc.
    and you can use WORD ADD 000100 etc.

    you have to declare the structure in local VAR list.
    and then use structname.varname

    and you can make a structure with 32 bools, or use an array of bools.
    more posibilities but for now enough.

     
  • FAn - 2013-07-02

    Oh I guess what I'm trying to do doesn't work because each bool of the struct is using 1 byte, so
    To clarify:
    the structures (Vlv_Flt, DrvOnOff_Flt, VVVF_Flt, AnMeas_Flt...) contains the fault bits (CbTrip, ToutRun, TOutReach, EmStp...) that are different from structure to structure.
    The FltIn structure is then processed (bitwise and, or, not...) by a FB to get Flt and FltNAck structures.

    In Siemens this can be done using input type ANY and then indirect addressing (pointers).
    In Rockwell I can achieve it by copying the content of the structure to DINT using instruction CPS and then copy back from DINT to structure.
    In Schneider I don't remember exactly but it's either copy like in Rockwell or copy + use of arrays.

    Of course I could copy bit by bit using StructName.VarName but that is time consuming (I have many FBs), error prone, screen cluttering (the evaluation fb would have 65 inputs and 64 outputs, probably doesn't fit in 1 screen) and not elegant. I would rather get rid of the structure and replace it with a dword (I will lose some readability but easy to use and faster execution).

     
  • shooter - 2013-07-02

    export your program and we will have a look at it,
    please tell what you want to achieve.
    and no i am NO fan of pointers.
    everything is possible with normal programming.

     
  • Strucc - 2013-07-02

    Zitat:
    and no i am NO fan of pointers.
    everything is possible with normal programming.

    I did not know, pointers were abnormal... Addressing "something" is a basis of programming. The purpose of pointers is to address "something"... I don't think it's a matter of like or dislike: elegant and safe programs can be written with pointers as well...

     
  • Strucc - 2013-07-02

    FAn hat geschrieben:
    I want to prepare a function block that will process different structures (every structure containing up to 32 bools)
    ....
    I've done it with FBD so maybe I have some syntax error in the above example but anyway conversion from structure to Dword seems to work fine, but when I convert back from Dword to structure I get some bools that have wrong values. What am I doing wrong?
    Any other ways to solve this?

    As far as I know, in CODESYS a BOOL variable takes a byte, not a BIT.
    (So, your structure takes 32 bytes instead of 4 bytes = 32 bits. Try to check the result of dwSize := SIZEOF(yourstructure)...

    There are many ways to store these 32 bits in a DWORD (create and Enum as mask, use global constants as mask or use the {bitaccess...} pragma, see online help. (correction: enum is always int in CoDeSys, so you can't create 32 bit masks...)

    In V3, you have the BIT datatype, and the mapping to a DWORD can be done via UNION.

     
  • shooter - 2013-07-03

    strucc is correct, bits are in processor bytes, but you can put them in a DWORD, by using pack and unpack. in util.lib
    see also w www.oscat.de w for more functions.
    I would use the struct.name as this is clear and despite a big text the program is small, as these are simple VAR and taking only one byte.
    try using pointers and using the normal ST or FBD variant, let it run for 1 million times and check the speed, it will not make much difference.
    but the software is easy readable and in a few years the service is still possible without peeking and poking.

    why having so many structures doing the same make one stuct containing all structtypes used.

     

Log in to post a comment.