Skip to content

Best practice for storing vital values in PLC

Hi,

I am looking for a way to store vital variable values in AXCx152 PLCs.

These are typical hour counters or kWh-counters and so forth. They are accumulated during the life of the product, and should in no circumstances be lost or corrupted.

Below are a few options I have considered, with their pros and cons:


  1. Retain memory. Pros: One checkbox-implementation. Cons: I do not trust retain memory in PLCnext Technology devices. Too easily wiped with no warning, and will sometimes be corrupted (e.g. random values) when downloading new program. (Retain would be the most obvious choice in other PLCs, but due to the "less than ideal" retain handling in PLCnext, this is not a solution I can trust. Refer to my rant in this thread: https://www.plcnext-community.net/forum/#/discussion/3633)
  2. Database. Pros: Easily configurable in PLCnext engineer with no coding. Cons: somewhat overkill for simply storing values which are not timeseries or events. I have simple values which change over time, but I am only interested in the latest value - not the hour counter value of yesterday. Also, the database is not easily accessible by the PLC program, and operators would have to extract the sqlite db file and interpret it in a database browser to get values from it.
  3. File writing. Pros: More reliable than the retain handling. Cons: File writing is messy. Would have to write at set intervals, and read the file upon reboot. Must create backup files and a method to delete older files etc.

Are there other methods which are reliable for storing values?


My current method is a mix of 1 and 3:

  • Store value in volatile memory
  • Each hour, copy value to retain memory
  • Each midnight, store value to new file
  • Each day, delete files older than 5 days (which means I have 4 backup files at any given time)
  • Upon reboot, load the largest and newest backup file and compare values with retain memory to check for data loss.
  • If data loss is detected (i.e. an hour counter suddenly has a lower value), import values from file and put them into retain memory. Also create an alarm for the operator as this may have created some data loss as file backups are only done daily.

This is 400 lines of code and I hate it.

Comments

  • Hello!

    You are dive in really deep)

    Can suggest you to take a look at PBCL_ParamBackupMngt_1/PBCL_ParamBackup_1 FB of PLCnextBase library https://www.plcnextstore.com/world/app/2390. It can be alternative to file writing option 3.

    Pros: Can write data to file on change from the box. Reduce steps for file interaction in compare to other methods.

    Cons: Have to tell to fb does it need recover or backup data at startup or some kind of event. PBCL_Setup_1 fb have to be processed after startup to make it all work properly. File not human readable for some data types/structures.

    Best regards.

  • I will investigate, thanks Oleksander :)

  • edited January 2024

    For my proposes I often use FunctionBlock: PBCL_SysLinuxShell_1 from PLCnextBase library. And transfer data to regular files or SQLite via cli. And then parse received string to extract data I need.

    For instance,

    >Command to create SQLite DB

    'echo "CREATE TABLE w (DT_val INT, CMNT TEXT, rVAL1 REAL, rVAL2 REAL, rVAL3 REAL);" | sqlite3 /mnt/tmpfs/test_db'

    >Command to write some data to SQLite file

    'echo "INSERT INTO w VALUES (1704974170, \'COMMENT_169\', 1.8675, 2.8515, 3.1841);" | sqlite3 /mnt/tmpfs/test_db'

    >Command to receive latest row

    'echo "SELECT * FROM w ORDER BY DT_val DESC LIMIT 1" | sqlite3 /mnt/tmpfs/test_db'

    >result

    2024-01-11 13:50:32|COMMENT_774|1.8314|2.3616|3.731

    Backup of file can be organized via crontab.

    Please, find sample attached. It create and write to SQLite and regular file.

    Path to the file need to be modified, and PLCnextBase library need to be attached.

  • That's next level 😉

Sign In or Register to comment.