It depends on whether the PLC has a battery backed up memory and if the battery is OK.
With the CODESYS Control RTE on PC based systems many tiimes there is no battery backup possible. In that case it is possible to save the retain data to a file and restore it after starting up again. Maybe that also works in your case.
You could try the following:
Add the following libraries to your project:
Use the following code. In this case you have to manually set the xSave bit to TRUE to save it (could be a SAVE button which you create in your visualisation).
Then do a warm reset and start the PLC again. Then you can test if the counter value is restored.
xStartUp : BOOL := TRUE;
pApplication : POINTER TO CmpApp.APPLICATION;
diMyResult : DINT;
ton1 : TON;
IF xStartUp THEN
pApplication := CmpApp.AppFindApplicationByName('Application', ADR(diMyResult));
xStartUp := FALSE;
ton1(IN:= NOT(ton1.Q), PT:= T#2S, Q=> , ET=> );
IF ton1.Q THEN
iCounter := iCounter + 1;
IF xSaveRetain THEN
xSaveRetain := FALSE;