Pointer adress from another PRG not possible?

hwillems
2020-08-10
2020-08-11
  • hwillems

    hwillems - 2020-08-10

    Hello,
    I have several (PRG) POU's in my Codesys V2.3 program.
    For example PRG1, and PRG2.

    Because PRG1 contains an adress which is a Modbus Holding memory, i want a pointer towards that adress.
    I have a HMI that can read and write that holding memory adress.(For example speed reference)

    I also have PRG2, which i also want to read and write to that adress. So what i did is:

    In PRG1:
    VAR
    testword AT %MW5480 : WORD;
    END_VAR

    In PRG2:
    VAR
    ptr_testvar : POINTER TO WORD;
    END_VAR

    ptr_testvar := ADR(PRG1.testword);


    Somehow this doesn't work, i have used pointers alot (for example iterating over Struct members, when all the members are of the same datatype with sizeof()).

    Is every PRG like a separate instance on Codesys VM? And therefore they can't access each others memory on a pointer level? When you use pointer addressing, and keep the dereferencing inside a single PRG it works fine.

    I also tried to create a pointer address in PRG1, and then dereference it in PRG2. That causes an "Access to illegal address! PLC is stopped." popup.

    I could not find any information about this in the Codesys V2.3 Programming Manual. Also could not find how you can initialize a pointer with ADR() other then to call it in the code itself rather than in the initialization phase.

    Is there any way how to do this?
    I want to create a struct with pointers to several addresses, that i can dereference from other PRG.

    Also found something regarding "INDEXOF" syntax, but could not find any example. When i do var := INDEXOF(PRG1) i get something back... but not sure what to do with it. Couldn't find any example and couldn't find if this could help me.

     

    Last edit: hwillems 2020-08-10
  • aliazzz

    aliazzz - 2020-08-10

    Why don't you declare the TestWord as a VAR_GLOBAL ?
    Then you can easily use it in PRG1 & PRG2 without using pointers. Just be careful with running PRG1 or PRG2 in a different task of each other.

     

    Last edit: aliazzz 2020-08-10
    • hwillems

      hwillems - 2020-08-11

      I thought about that solution, i just am used to avoid global variables. For every "interface" i mostly write a seperate PRG, and because modbus holding registers are like IN_OUT, i wanted to distribute the "address" of the location throughout the rest of my program with a Struct of pointers to that holding register.

      Because i have alot of different information on that modbus, i like to put data that belongs to each other in a Struct which i normally do before consuming it in other places of my program.

      But with "read & write", i wanted to pass around a pointer.


      What i will try next is make a seperate GLOBAL_VAR, and try to make a struct of pointers to that data and see if that works.

      It's a shame i have to work with Codesys V2.3, as Codesys V3.5 is looking so nice regarding structuring the code with many OOP functionalities, pointers to functions etc. Great for making finite state machines in ST. :)

      Anyway, thanks aliazzz for your response!!

       

      Last edit: hwillems 2020-08-11

Log in to post a comment.