TwinCAT DB Server: direct database operation from PLC

With the continuous improvement of the performance of automation equipment, enterprises have put forward new requirements for quality tracking, production data statistics and analysis, requiring the control system of the equipment to communicate with the enterprise's local area network.Since Beckhoff PLC is itself a "computer" - with a Windows operating system and one or two Ethernet ports - it can be directly connected to the factory's local area network.

After PLC is connected to the local area network, enterprises must also invest in supporting software to collect PLC data for quality tracking and other statistical analysis.The supporting software may be a third-party configuration software, a specialized program developed in a high-level language, or an enterprise ERP or MES system.The supporting software can analyze PLC data online and respond instantly, but a more common application is to first place PLC data in a database - such as SQL or Oracle - and then analyze offline after the supporting software is installed.

How does PLC data enter the database?Usually, a bridge program is programmed in C# or VC to collect PLC data and send it to SQL or Oracle databases.In the actual project implementation process, IT engineers who write data analysis software or implement MES systems are not familiar with PLC or TwinCAT PLC or TwinCATADS communication, while automation engineers or electrical engineers are not familiar with C# or VC, and even less familiar with database programming in C# or VC. Therefore, whether it is IT engineers or automation engineers who write this bridge program, how to write it to achieve high efficiency and stability and reliability has become a big problem.

To this end, Beckhoff provides the TwinCAT Database Server extension package, which can replace C# or VC high-level language applications, allowing PLC engineers to implement data bridging from TwinCAT PLC to Database without writing high-level language code.

The TwinCAT Database Server has been on the market for at least 10 years and is mature and stable, widely used in various industries.It should be clarified that it is not a database itself, but provides a bridge between TwinCAT PLC and database. Through it, not only can PLC data be sent to common databases such as SQL, Oracle, Access, etc., but also to Compact databases on WinCE systems, Excel or ASCII files, and even XML files can be used as databases.

In addition, for large and super-large enterprises, their production parks may be distributed in different cities or even different countries. With the development of network cloud technology, these large and super-large enterprises can even directly access the data of automation equipment in various factories distributed across the country or even the world through cloud servers, which can not only collect data but also issue instructions, to achieve global production scheduling, automatic docking of factory production with the procurement, warehousing, sales and transportation process of main raw materials and components, key equipment status monitoring and timely maintenance, etc. Of course, this depends on the support of cloud technology by the control system of automation equipment, as well as the construction of cloud platform by the enterprise itself.

Beckhoff also provides a variety of cloud technology solutions, allowing both single controllers to access cloud data, as well as providing cloud transit software TwinCAT IoT Gateway, which allows both Beckhoff controllers and third-party controls to access cloud data through the software. It also provides software TwinCAT IoT Analytics for IoT cloud data analysis.In addition, it provides IoT couplers that allow IO data to be directly uploaded to the cloud.

Although concepts such as cloud technology, Industry 4.0, and smart factories have been popular before 2017, Beckhoff also provides a variety of solutions. However, there are currently not many application cases in China, and the author lacks practical project experience in this area.The more common application in China is the direct communication between PLC and database, namely TwinCATDatatbase Server.This chapter mainly introduces its principle and usage.


The software model of TwinCAT DatabaseServer is as follows:


When the database server starts, the configuration file "C:\TwinCAT\Boot\CurrentConfigDataBase.xml" will be loaded, which contains three parts:XML Database Declaration configures database information, including type, file name, table, and so on;XML Device Declaration configures PLC information, including NetID and Port;

XML SymbolGrp Declaration configuration variable table, including the source PLC and destination DB of the variable.

The XML file also defines whether the DB Server's startup mode is AutoStart or Manual.

In AutoStart mode, the variable table configured in the XML SymbolGrpDeclaration is automatically written to the database periodically.

The PLC program can call a function block to abort or restart this action.Manual mode is called by the PLC program to call different FBs for database operations: inserting records, creating or deleting Database or Table, querying data, or executing arbitrary SQL statements.As can be seen from the figure, the SymbolGrpDeclaration is not required in Manual mode.

When operating DB tables in PLC programs, only the ID number is recognized.  Only tables configured in the XML file can be operated by the PLC.  The "XML Configuration File Editor" in the image above is a visual tool for configuring this XML file.Prior to operation, it is necessary to use this tool to define the ID corresponding to the Database type, path, file, and table.  Optionally, information such as authorized access user names and passwords can be added.

TwinCAT Database Server starts with TwinCATService.  TC2 must be installed on the PC where the DB Server is installed, and its level can be any level: either the 32-bit Demo version or the 64-bit Engineering version.When the DB Server is working, TwinCATService must be in Config or Running mode.

The TwinCAT Database Server, PLC, and DB in the figure can be on the same computer or distributed on multiple computers, but all within a local area network.There are four types of topology structures:

The first type: The database is stored on one PC, and TwinCAT Database is also installed on the same PC.



Used for relatively single-function databases in medium-sized enterprises.

The second option: The database is stored on multiple PCs, and the TwinCAT Database is also installed on a separate PC.


This type of database system is complex in structure, and large enterprises often have dedicated software departments to maintain these databases, with complex data analysis software.

The third method: Install a small database and TwinCAT Database Server or TwinCATDatabase Server CE on the PLC, which can be used either standalone or networked with multiple computers.

The database of such system is usually used to store some local production data, and the database files can be transmitted and stored for future analysis.The fourth type: a large database independent of the PLC, with TwinCAT Database Server or TwinCAT Database Server CE installed on each PLC.

The database of such system is usually used to store some local production data, and the database files can be transmitted and stored for future analysis.
The fourth type: a large database independent of the PLC, with TwinCAT Database Server or TwinCAT Database Server CE installed on each PLC.




For security or cost reasons, large enterprises cannot provide IPC for installing TwinCAT software and TwinCATDatabase Server.In fact, DBServer is installed on each controller, and the software cost is not low.
For all the following operation steps and routines, see Chapter 12 of TwinCAT 2.0 from Introduction to Proficiency in WeChat official account Main Menu | Network Resources | Personal Sharing.Only the directory and important information are listed here, excluding screenshots and descriptive text.

Function blocks provided by TcDatabase.libAfter installing TwinCAT Database Server on the PC, there will be a TcDatabase.Lib file in the TwinCAT\PLC\Lib directory.  Users can also copy the file from the supporting documentation and reference it in the PLC program, as shown in the figure below:


The most commonly used ones are the function blocks FB_DBRecordInsert_EX for inserting data records through SQL statements and FB_DBRecordSelect_EX for querying.If the TwinCAT Database Server uses AutoStart, the PLC program also needs to use FB_DBCyclicRdWrt to start and stop automatic continuous data insertion.


For formal databases such as SQL Server and Oracle, you can also predefine multiple procedures with parameters, and the PLC can trigger the execution of these procedures.Related FBs include:

FB_DBStoredProcedures: Trigger Procedure,

FB_DBStoredProceduresRecordArray: Trigger Procedure and receive multiple records returned

FB_DBStoredProceduresRecordReturn: triggering Procedure and receiving one record returned

Whether to return one or multiple records depends on the predefined functionality of the Procedure.  When generating this Procedure in the database, you can specify its Input, Output, and InOut parameters.


In addition, there are FBs that implement the following functions:

Open and close the connection to the database: FB_DBConnectionOpen and FB_DBConnectionClose

Create database and create data table: FB_DBCreate and FB_DBTableCreate

All the FBs in the figure above can be explained in the TwinCAT Information System:


FB triggering database operations through SQL statements

Insert records into the database function block

Function block for reading database records

Start the predefined Procedure on the DB side from the PLC

For the official database SQL Server and Oracle, you can also predefine multiple procedures with parameters, and the PLC can trigger the execution of these procedures.Note that TwinCAT Database Server cannot generate or modify procedures in the database.

FBs related to Procedure are:

FB_DBStoredProcedures: Trigger Procedure, no data returned

FB_DBStoredProceduresRecordArray: triggering Procedure and receiving multiple records returned


The most commonly used ones are the function blocks FB_DBRecordInsert_EX for inserting data records through SQL statements and FB_DBRecordSelect_EX for querying.If the TwinCAT Database Server uses AutoStart, the PLC program also needs to use FB_DBCyclicRdWrt to start and stop automatic continuous data insertion.

For formal databases such as SQL Server and Oracle, it is also possible to predefine multiple procedures with parameters, and the PLC can trigger the execution of these procedures.Related FBs include:

FB_DBStoredProcedures: 

Trigger Procedure,

FB_DBStoredProceduresRecordArray: Trigger Procedure and receive multiple records returned

FB_DBStoredProceduresRecordReturn: triggering Procedure and receiving 1 record returnedWhether to return one or multiple records depends on the predefined functionality of the Procedure.  When generating this Procedure in the database, you can specify its Input, Output, and InOut parameters.

There are also FBs that implement the following functions:Open and close the connection to the database: FB_DBConnectionOpen and FB_DBConnectionCloseCreate database and create data table: FB_DBCreate and FB_DBTableCreateAll FBs in the figure above can be explained in the TwinCAT Information System:

 FB triggering database operations through SQL statementsInsert records into the database function blockFunction block for reading database recordsWhether to return one or multiple records depends on the predefined function of Procedure.Procedure is generated in the database.When FB calls Procedure, it needs to specify parameters, which can be declared as Input, Output, and InOut types.  Such complex SQL instructions can be pre-edited in the database, and TwinCAT Database Server only needs to trigger it.

The number of elements in the interface parameter array ArrParalist determines the number of PLC variables that can be sent to the database.  If the value of a variable is assigned to a field, inserting a record with dozens of fields is much more efficient than using SQL statements.  Moreover, the values are transferred rather than converted to ASCII.

The advantage of this method is high transmission efficiency, but the disadvantage is that it is only supported by formal databases, and users need to understand the method of generating procedures in the database and generate them in advance.