In IEC 61131-3 implementations (e.g. CoDeSys, TwinCAT, Unity), what actually happens when you use the assignment operator on variables of Function Block type? I've looked at the TwinCAT and CoDeSys docs without finding any kind of clear description; I assume it's defined by the 61131-3 spec, but I don't have a copy of that.
E.g:
VAR
fbA, fbB : TON;
END_VAR
// Function Block assignment
fbB := fbA;
In this case, memory is statically allocated for both the fbA and fbB instances before the assignment is executed. Does the assignment operator just copy the VAR_INPUT, VAR, and VAR_OUTPUT internal state variables from fbA's memory area to fbB's? Does anything else happen?
What if fbA or fbB are instances of a Function Block type that implements FB_init(), FB_reinit() or FB_exit()? Does assigning fbB := fbA result in calls to any of those methods?
I wrote a bit of test code (TwinCAT 3.1.4024.44)
MAIN.TcPOU:FB_Test.TcPOU:Running the main program in the TC debugger shows that
MAIN.nInitsstarts at 3 (one each for the statically allocatedfbA,fbBandfbCinstances). TogglingbRundoes not increment the init, reinit, or exit counters; so clearly theFB_init(),FB_reinit()andFB_exit()methods are not invoked when one FB instance's data is overwritten by that of another.Inspecting the behaviour of
fbA,fbBandfbCin the debugger shows that the assignment operation definitely copies all of the FB'sVAR_INPUT,VAR, andVAR_OUTPUTstate when the FB is assigned.Toggling
bCallStackFBdoes increment bothMAIN.nInitsandMAIN.nExits, so creating a temporary instance of an FB as a stack variable, such as aVARin aFUNCTION,METHODorPROPERTYimplementation, definitely calls bothFB_init()andFB_exit(). (Note: TwinCAT versions before 3.1.4022 and CoDeSys versions before V3.5 SP9 Patch 8 do not callFB_exit()for stack-allocated FB instances.)