Home > front end >  Beckhoff-PLC: Declaring an array as remanent inside a Function Block
Beckhoff-PLC: Declaring an array as remanent inside a Function Block

Time:09-30

this is my first question on StackOverlfow, so feel free to give me feedback on the problem :)

I'm new to working with controllers from Beckhoff and I'm trying to program a program block for communicating machine data to the PC. To store the data of different types I use an array of T_ARG in the function block "Communication", which is instantiated in "MAIN". At each restart its data are reset, which leads to the fact that I would have to load the information at the start always again into the array, whereby I would have the data then twice on the system.

Code inside the FB "Communication":

VAR
    Values : ARRAY[DataArrayLow..DataArrayHigh] OF T_ARG;
    ValueChanged : ARRAY[DataArrayLow..DataArrayHigh] OF BOOL;
END_VAR

I am using to the array "ValueChanged" to track change of the data by using a setvalue-method, which marks the index in "Values" to be transmitted.

For now i am calling the dunction block in "MAIN" as usual:

VAR
    Communication : FB_Comm;
END_VAR
__________________________________________________________________________
Communication();

I already tried to mark the variables as persistent, which did not work.

I dont really wont to create an external global variable and give it to the function block as input, since it defeats the purpose of capsulation and it would be cluttered.

CodePudding user response:

Normally persistent variables should be saved automatically during proper shutdown or reboot of TwinCAT. So if you just remove power (without UPS), the persistent data will normally not be saved.

What you can do to be sure that the persistent data is stored is to use the WritePersistentData function block. In your case, you shouldn't call this function block all the time, bur rather only do it as soon as any of your variables change.

CodePudding user response:

I don't know Beckoff in depth, but it's very similar to Codesys, and from the documentation here, it follows the same principle:

Declaring a local variable as a PERSISTENT in a function has no effect. Data persistence cannot be used in this way.

From what it seems the correct way for you is to use Globals, maybe you can study a way to modularize your variable structure in order to pass it to your block as VAR_IN_OUT, possibly a ARRAY of STRUCTS can help you achieve this.

  • Related