Retain variable reset to zero

Hi everybody,

I noticed that (apparently) randomly the 2 retain variables I defined in my projects are set to 0 after a software transfer.

It happened on different CPUs, and I never changed transfer method between transfers, so the conditions should be the same.

These variables have no default values and are never set to 0 by the code, since they are general counters for machine diagnostic and report.

image

in the logger I don’t see any message which says that there was a memory reset, so apparently there’s no reason for them to be reset.

Any idea or troubleshooting suggestion?

thanks

Hi Valerio

In the transfer settings [click on the gear wheel in the transfer dialog box] you can specify the option ‘keep PV values‘ before transferring.

Otherwise it will be on ‘execute init/exit‘ which leads to the reinitialization of the variables.

Transfer Dialog

installation settings

Good luck!

Jan

it’s already set like that. keep pv values. infact the issue happened only a couple of time over many sw transfers..

anyway, even if I execute init/exit, these variables are retain, so why they should be set to zero?

According to this table the installation of new SW should not initialize the variable when Keep PV is YES.

If you don’t see anything in the logger like 26263 it might be worth checking with your local support what the cause could be.

I’m pretty sure a rebuild can reinitialize retain PV (as I understand in the help). To make it sure it’s only reinitialized during intial installation I would recommend to pass the PV from retain to permanent.

1 Like

I will try that, thanks

Were they local or global? I’ve previously seen global retain variables reset during a transfer.

The theory I and my US support team had come up with is that the memory fragmentation had made it impossible for the changes i was making to global memory layout, by adding/removing variables, to make the copy of current values into new memory locations. It happened in frequently. There was a line in the help that seemed to indicate this wsas possible in rare circumstances, but I don’t remember where.

But we were also dealing with some extremely large memory structures.

1 Like

Yes you are right, if you playing too much with adding / removing variables on the project without a rebuild. The memory could fragment then result in reset some variable due to memory reorganization by the compiler.

1 Like

thanks for pointing this out.

they are global indeed.

I only noticed it happening about twice per year, but many sw transfer in between..

Anyway.. apparently, the only solution to this issue seems to be using permanent variables. It’s also difficult to replicate the issue, so I guess it’s just a matter of waiting and see if it happens again..

Support and I had a theory that perhaps making sure the Global PV size allocated be 2x or more larger than what was actually used, minimized this. I.e. the larger make this area the more AR can re-organize it.

1 Like

I will perform some tests with all your ideas and get back if there’s any improvement.

thanks!

just for info, if anyone will have the same problem. after talking with local support, we found out that:

  • retain variables are actually not retain if a rebuild is done. considering how often I need to rebuild a project because of weird machine behavior, it’s not so rare. so retain variables are actually useless in my use-case.
  • an alternative solution would be to use a permanent variable, but my cpu does not support it. I honestly don’t understand how a 600€ cpu is not able to store ONE VARIABLE.

final solution will be to use a file with library dataObj and just store the value there.

1 Like

Instead of building your own library/program check the ‘mappRecipe’ for saving machine configuration. B&R Online Help

Think you should also dive in to the reason you need a ‘rebuild’ for weird machine behavior. A rebuild is only needed if your change big things like AR/AS versions and not for code things (And even then its not mandatory) . So if the weird things start to happen during a normal update double check your pointer/memory initialization.

I see your point. I just didn’t have time to dive into detailed analysis of these erratic behaviors. I just rebuild and that’s it.

I already use mappRecipe but it’s not the solution I need for my case because the variable I need to save it’s a machine-specific counter, while configuration files could be used in different machines.

We use many Retain variables, and have observed that their values sometimes reset during a normal transfer, not only after a full rebuild.

What we’ve found is that if we perform a transfer followed by a reboot, the Retain values almost never reset when making small software changes. Larger rebuilds or major changes can still cause resets.

It’s not the most convenient method, but if preserving Retain values is critical, it has proven reliable.

1 Like

ok but I don’t understand the value of using retain variables if their value is not always preserved. actually they seems quite “fragile”.. probably it’s just me, but I don’t see the point. infact there are also permanent variables (not on all CPUs)!

so I have normal variables, not so permanent ones, and permanent :joy:

1 Like

If you need to save and read back on startup counter, I would recommend to use something like this json serializer / deserializer to save it on exit routine and load it when your plc is started.

I’ve never use this specific lib but I’ve used a similar one. You should be able to save / load a structure so if your counters are all in the same structure you can convert to string / load from string in 1 cycle time. Then you just have to handle the string to be write or read from a file.

1 Like

yeah I used a similar solution with dataObj library

1 Like

B&R Online Help According to this is might keep some retained variables during a rebuild.

This is what i had previously experienced, especially when we were passing around archives of projects or not using the same file paths, if the temp/binaries aren’t at the same place or don’t exist, that’s basically a rebuild. So sometimes as my colleagues and I passed projects around, sometimes we’d have a rebuild and sometimes not, and sometimes we’d lose retained data and sometimes not. There definitely seemed to be a something related to Global PV and Global PV remanent sizes that related to if the data was retained. I.e. if there was enough room for the entire memory area to be cleanly copied into new contigous memory, we didn’t loose data.

We started using a hybrid process utitlizing timestamp for the first 4 bytes of a structure for retained data and mpRecipe storing the strucutre. We would update the timestamp efvery time we updated the strucutre and put a delay of like 30 seconds after the time stamp changed to trigger the save, or the machine successfully producting product. If on reboot we just loaded the mapp recipe if the timestamp was 0’d.

1 Like

Are you using pointers?

I have managed to reduce occurrences, but not eliminate them by checking pointers before using them

in c, if (pointer) *pointer = 123;