Skip to content

Import / Export of S7 DB

Data blocks in a Siemens controller are used as interfaces for communication with external applications. They are the "data contract" between the two communication partners (PLC and PC system) and must therefore be available on both sides for planning, programming and configuration. Usually, the data structures of the data blocks, in the format of Siemens S7 STL source files, are created and provided by the data acquisition tools (Adam-Software).

Importing of a S7 data block as STL source file into the TIA Project

Goal: a S7 data block created externally as STL (Statement list) source file (text file with the extension of .db) has to be imported into the Siemens TIA project. The source file has to comply with the Siemens S7 STL (“Statement list”, former AWL from German “Anweisungsliste”) data format. Following is a short example, including also a PLC data type (UDT) named "Characteristic" in the same file:

TYPE "Characteristic"
VERSION : 0.1
   STRUCT
      MES : Real;   // Measured value
      ATT : Int;   // Attribute
      NOM : Real;   // Nominal value
      LSL : Real;   // Lower specification limit
      USL : Real;   // Upper specification limit
      EVT : Int;   // Event
      NOK : Bool;   // Event: this characteristic was NOK
      REW : Bool;   // Event: this characteristic was reworked
   END_STRUCT;
END_TYPE

DATA_BLOCK "ProcessData"
{ S7_Optimized_Access := 'FALSE' }
VERSION : 1.0
   STRUCT 
      Handshake : Struct
         TriggerDataReady : Bool;   // from PLC
         DataStored : Bool;   // from PC
      END_STRUCT;
      ProcessCharacteristics : Struct
         Screw_1_Torque : "Characteristic";   // Torque Screw 1
         Screw_1_Angle : "Characteristic";   // Angle Screw 1
      END_STRUCT;
   END_STRUCT;
BEGIN
END_DATA_BLOCK

By double clicking on "Add new external file" under "External sources" under the PLC… [CPU…] of the open project, you can open a source as a file with the extension .db (or .AWL), see Fig 1.

Read source file
Figure 1: Importing the source file of a data block.

After importing, the file appears under "External sources" and with a right click on it, you can select the option "Generate data blocks from source" from the context menu, see Figure 2.

Generate from source
Figure 2: Generate the data block from source.

A message appears that warns you that existing data blocks (with the same name) are overwritten, s. Figure 3. If you want to prevent this, you should rename the existing data blocks to protect them from overwriting.

Warning: Overwrite?
Figure 3: A warning could appear that existing data blocks with the same name will be overwritten.

After successfully generating the DB, it will appear under "Program blocks", in this example “ProcessData [DB1]”. If the file included one or more UDT, you can find them under “PLC data types”, as you can see the imported UDT “Characteristic” in Figure 4.

Generated DB
Figure 4: Generated data block and UDT.

The DB number was automatically set to the next free DB number (in this example DB1) so that you now have to set the DB number to the desired number under "Manually" in the "General" tab of the “Properties…” of the DB, shown in Figure 5:

Generated DB
Figure 5: Change the DB number

Subsequent changes to the names of individual structures and symbols should be avoided in order to avoid any additional efforts caused by losing further using of these symbols in the PLC program during a new import, in case of necessary changes or extensions.

Imported DB
Figure 6: A data block containing a UDT named “Characteristic” imported from external source file.

Exporting a S7 data block as STL source file from the TIA Project

Goal: a source file is to be generated and exported from an existing data block in the S7 program. By right-clicking on the data block and selecting "Generate source from data blocks -> Including dependent blocks" in the context menu, a file with the extension .db is generated.

Create source
Figure 7: Generate the source file of a data block including UDT (User Defined Types).

When the “Save as…” file dialog opens, you should rename the file as follows: in addition to the description of the data block, the file name must also contain, enclosed between square brackets, the text "DB" followed by the DB number, according to the following pattern. This is necessary because the DB number is not contained in the file itself.

<Data block description>[DB<Number>].db

For example: „ProcessData[DB701].db“.

This file will contain the definition of the data block itself as well as the definitions of any included user defined types (UDT).

The content of the exported file will look similar to the following. In this example, the UDT named "Characteristic" was used several times:

TYPE "Characteristic"
VERSION : 0.1
   STRUCT
      MES : Real;   // Measured value
      ATT : Int;   // Attribute
      NOM : Real;   // Nominal value
      LSL : Real;   // Lower specification limit
      USL : Real;   // Upper specification limit
      EVT : Int;   // Event
      NOK : Bool;   // Event: this characteristic was NOK
      REW : Bool;   // Event: this characteristic was reworked
   END_STRUCT;
END_TYPE

DATA_BLOCK "ProcessData"
{ S7_Optimized_Access := 'FALSE' }
VERSION : 1.0
   STRUCT 
      Handshake : Struct
         TriggerDataReady : Bool;   // from PLC
         DataStored : Bool;   // from PC
      END_STRUCT;
      ProcessCharacteristics : Struct
         Screw_1_Torque : "Characteristic";   // Torque Screw 1
         Screw_1_Angle : "Characteristic";   // Angle Screw 1
      END_STRUCT;
   END_STRUCT;
BEGIN
END_DATA_BLOCK

S7, TIA are registered Trademarks of Siemens AG