Port Variables Initialisation

Hello, what is the way to initialize the Port Variable after a Cold/Hot/Warm Start of the PLC? I have seen that after a start of the PLC the variable myFirstInt is not always initialise to 0, sometimes it has random values. I know about some Components Method that are executed after Starting the PLC but the variable is declared out of the scope of these methods. What’s the best way to do it? public: //Ports

//=====
//Ports are defined in the following way:
//#port
//#attributes(Output)
//#name(Q_intTest)
int16 myFirstInt = 0; Angel

Hi Angel, Your example shows the correct way to initialise variables - not just port variables. The behaviour during each type of start is:

                                               * Cold Start: The variable will be initialised to the value you assigned in the constructor.
                                               * Warm Start: The variable will be initialised to the value you assigned in the constructor. Then, any variable with the "Retain" attribute will immediately be over-written with the retained value (i.e. the value of the variable just before the Warm Start was executed).
                                               * Hot Start: All variables - not just "retain" variables - keep the same value they had immediately before the Hot Start was executed.

If you do not provide an initial value for any variable, then you may see random values for those variables. This is standard behaviour for C++ variables, and different to the C# and IEC 61131 standards, which guarantee default values for uninitialised variables. If you initialise the variable as shown in your code snippet, then no random values should occur. If they do, then please provide a simple example that demonstrates the problem, including:
* CLI version
* SDK version
* Eclipse version
* C++ project source code
* PLCnext Engineer project (if used)
* PLCnext Engineer version
* PLC firmware version
… and we will investigate. - Martin.

Hi Martin, the customer could test it again and he can not reply its mentioned behaviour but the behaviour you are meaning. Thanks a lot!