Skip to main content

Retain Data in Shared Memory

Some products do not include an automatic connection of retain data from the application in the controller (due to a lack of generally valid storage options). However, there is an existing mechanism in the runtime system by means of a connection via shared memory (SHM) which can be used for this purpose with a respective configuration.

Usage is possible, for example, with the following CODESYS Control SL products:

  • CODESYS Control​ for Linux

  • CODESYS Control for Linux ARM

  • CODESYS Control for Linux ARM64

  • CODESYS Control for Raspberry Pi

  • CODESYS Control for BeagleBone

Version note: The minimum version of the CODESYS Control SL product is V4.4.0.0 (CODESYS Control version 3.5.17.30)

Basically, "Retains" are enabled via SHM by the following settings in /etc/CODESYSControl_User.cfg on the target device:

[CmpApp]
 RetainType.Applications=InSHM
[CmpRetain]
 Retain.SHM.Size=0x1FFFF        ; Total size of remanent memory in bytes
 Retain.SHM.Name=MyRetainMemory ; Optional (Default: "RetainMemory")

After a restart, the runtime system creates a shared memory file with the configured name under /dev/shm. By mapping this file, the memory can be redirected to any non-volatile file, for example, by the following commands:

sudo touch /dev/shm/MyRetainMemory
sudo mount -o bind /var/opt/MyRetainMemory /dev/shm/MyRetainMemory

Important

The file is accessed cyclically according to the cycle time set in the PLC application. The SHM file should therefore never be redirected to a file in a flash-based file system, otherwise there is a risk that the memory will be destroyed.

Retain Data double-buffered in shared memory

Optionally, double buffering can be enabled for retain data in shared memory. By doing this, the retain data is stored redundantly in two memory areas.

The following additional setting in the file /etc/CODESYSControl_User.cfg is used to enable this feature:

[CmpRetain]
Retain.DoubleBufferEnabled=1

With this setting, the runtime system publishes two new events which can be used via the even mechanism of the Extension Package.

For more information about the event mechanism, see the following: Event Mechanism

The Python script retain_mirror.py included in the Extension Package Example shows how a single-channel double-buffer mechanism could be controlled via events.