When working with Siemens PLCs, sometimes you will need the ability to save data that does not fit in retentive memory when there is a power cycle. The solution is to use an SD card with a Siemens PLC to periodically write data from a data block to the SD card and then read that data during startup. Data writing intervals can be specified by an HMI setting and manual read/write options are also made available.
To accomplish this task in the PLC code, a duplicate datablock can be created to store on the SD card, data can be split up into readable and writeable chunks, and triggers can be created for reading and writing to the SD card. The steps to implement this solution are below:
Step 1: Creating the datablock that will be stored on the SD card
Duplicate the datablock that is to be periodically saved to the SD card. In the duplicated datablock’s properties, set the attribute for “Only store in load memory” to true. This will make sure the datablock is stored on the SD card. All other options can remain the same.
Step 2: Splitting up your data
The "WRIT_DBL" instruction will be used to write the data in a later step. When using this instruction, data with the datatype “Struct” cannot be written. Only UDTs and arrays can be written to another data block. To accomodate this, split the data into sections that can be written to the SD card, and write each section inside of a datablock writing sequence.
Step 3: Writing the Data to the SD Card
To write the data to the SD card, the source block of the "WRIT_DBL" should be set to the data block that is being backed up; the destination block should be set to the corresponding data block on the SD card. If the data was split into sections, make sure to specify the section being copied and written on both the source and destination parameters.
The busy signal is an important component of the reading and writing process. The sequence should only move on to writing the next section when the busy signal is low. The busy signal can be high for several scans, and each write instruction should be given time to finish before moving onto the next one to avoid problems with the writing process.
Step 4: Reading the Data from the SD Card
Reading data from the SD card works similarly to the writing process. Just use the "READ_DBL" instruction. To trigger a read from the SD card on startup, use the Memory bit for “FirstScan” as one of the triggers for the read sequence. The same guidance from the data writing step on using the busy signal applies to the read sequence.
Step 5: Create the Trigger for Reading and Writing
To make a trigger for the reading or writing of the sequence, the timer, button, or any other kind of trigger should be tied to a Boolean. Use that Boolean to set off the write or read sequence. The active signal latches ON using the busy signal and the sequence trigger to complete the sequence without getting interrupted. If the sequence is running, the “bActive” boolean is ON until the sequence is completed.
Stepping through the sequence is controlled by the busy signal. The final step of the sequence should reset the sequence back to the beginning to wait for the next trigger.
Learn more about our Siemens PLC Programming expertise and contact us today for your next project.