This is my first post on stack overflow, so here goes ^^.
I am trying to pass over from the "conventional" type of PLC programming to OOP (still very new). New project I have will have 3 different programs which will run on a single runtime, all are run in a single main loop, the code is not too big, since there is a seperate system that has most of the configurtaion and only passes data to PLC via ADS protocol.
My question is, in these 3 programs, I have many function block instances which will trigger persistent data saving (FB_WritePersistentData). I only want a single FB_WritePersistent instance - no need for more instances and more instances may just cause writing at at the same time, which I would want to avoid.
Each of the programs has multiple function blocks which use the interface for writing the persistent data.
What is the proper way to pass a single FB definition that implements that interface to all my function blocks that want to use the interface? My first try was as follows:
- define the fbWritePersistentData(implements I_PersistentDataStorage) in MAIN
- have the same FB as VAR_IN_OUT in the 3 programs that will want to use this function block
- the FBs that use the interface have method .FB_Init which provides the reference to the interface - dependency injection
- the persistent FB is passed to other FBs in programs via the VAR_IN_OUT, but this issues a warning in TwinCAT as well as, well, some page faults, probably addressing error by using the VAR_IN_OUT
How I avoided this is by simply passing the persistent FB to the ones that implement the interface like this : fb1 : FB_1(main.fbSavePersistent) But this is not what I want to have as it feels, well, wrong :)
Should I have used the REFERENCE_TO instead? Thanks for your help in advance.