Functionality: Siemens S7-1200/1500
Functionality: Siemens S7-1200/1500
Siemens S7-1200/1500
TIA Portal (V13, V14, V15, V16)
Functionality
Implementation into user project
User guide
www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
Contents
1 Introduction .....................................................................................................................................................3
1.1 Hardware requirements .............................................................................................................................................. 3
1.2 Software requirements................................................................................................................................................. 3
1.3 Supported MySQL functions ....................................................................................................................................... 3
1.4 Supported MySQL data types ..................................................................................................................................... 4
1.5 Program blocks ................................................................................................................................................................ 5
1.5.1 Function block FB_PDSql_My ............................................................................................................................ 5
1.5.2 Data block DB_PDSql_My..................................................................................................................................... 6
1.6 PLC data types .................................................................................................................................................................. 7
1.6.1 tConnection ............................................................................................................................................................... 7
1.6.2 tCmd ............................................................................................................................................................................. 8
1.6.3 tLogin........................................................................................................................................................................... 8
1.6.4 tStat .............................................................................................................................................................................. 9
1.6.5 tLicense....................................................................................................................................................................... 9
1.6.6 tQuery.......................................................................................................................................................................... 9
1.6.7 tRow_1200 ............................................................................................................................................................. 10
1.6.8 tRow_1500 ............................................................................................................................................................. 10
1.6.9 tColumn_1200 ....................................................................................................................................................... 11
1.6.10 tColumn_1500 .................................................................................................................................................... 11
1.6.11 tSqlTable_1200 .................................................................................................................................................. 11
1.6.12 tSqlTable_1500 .................................................................................................................................................. 12
1.7 Errors ................................................................................................................................................................................ 12
1.8 License / Demo ............................................................................................................................................................. 13
2 Instalation ........................................................................................................................................................ 14
2.1 MySQL Community Server 8.0.19 ......................................................................................................................... 14
2.1.1 Host access settings for your MySQL account ......................................................................................... 21
2.2 Instalation PDSql Library My to TIA Portal .................................................................................................. 23
3 Examples ............................................................................................................................................................ 28
3.1 Create a table ................................................................................................................................................................. 28
3.2 SELECT row from table ............................................................................................................................................. 29
3.3 INSERT row to the table ........................................................................................................................................... 32
3.4 UPDATE row in table ................................................................................................................................................. 34
3.5 DELETE row from table ............................................................................................................................................ 36
2
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
1 Introduction
PDSql Library My allows you to connect your PLC Siemens S7-1200 or S7-1500 system directly
to MySQL Database. With this library you are able to read and write data from/to MySQL Database.
As the communication between the PLC and the MySQL Server takes place directly you don't need
any PC as a broker in the communication between the PLC and MySQL server.
• SELECT
• INSERT
• DELETE
• UPDATE
• Execute Stored PROCEDURE
However, in addition to this basic MySQL commands, other MySQL commands can be also
executed depending on string content in query input. However, with some commands of this
type, the query may end up with an error.
3
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
4
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
The whole concept of this library was built on the ease of implementation into user projects.
Therefore, all functions for communication with MySQL Server are contained in only one
function block FB_PDSql_My. This block is also associated with one instance of the data block
DB_PDSql_My_IDB. The last one is the data block DB_PDSql_My which provides all parameters
and the interface between the user application and the PDSql library. It contains the resulting
SQL table obtained after SELECT command. These blocks are supplied in 2 versions for S7-1200
and S7-1500.
S7-1200 S7-1500
Function block
Instance data block
Interface data block
5
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
The user application can fully control the Connect or Disconnect signals, but it is sufficient to
control only ExecuteQuery signal which ensure the connection automatically if needed.
Data block DB_PDSql_My is composed of all the data types described in chapter 1.6. All
communication between the user application and the MySQL server is done through this data block.
S7-1200 S7-1500
6
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
Function block FB_PDSql_My and data block DB_PDSql_My uses several custom PLC data
types to communicate with user application. The following is a list and description of these data
types used in the library for S7-1200 and S7-1500.
S7-1200 S7-1500
1.6.1 tConnection
7
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
1.6.2 tCmd
1 The bit signal is active in TRUE and triggered on the rising edge. Function block always resets this
bit.
1.6.3 tLogin
Database String '' The name of database (not used / can be empty)
8
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
1.6.4 tStat
This data type provides status signals from the Function block.
1.6.5 tLicense
1.6.6 tQuery
9
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
1.6.7 tRow_1200
This data type provides row data for sql table result (S7-1200 version).
1.6.8 tRow_1500
This data type provides row data for sql table result (S7-1500 version).
10
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
1.6.9 tColumn_1200
This data type provides column data for sql table result (S7-1200 version).
Default
Name Data type Comment
value
Name String '' Column’s name
CharacterSet UInt 0 Character set
ColumnLength UDint 0 Length
1.6.10 tColumn_1500
This data type provides column data for sql table result (S7-1500 version).
Default
Name Data type Comment
value
Name String '' Column’s name
CharacterSet UInt 0 Character set
ColumnLength UDint 0 Length
1.6.11 tSqlTable_1200
This data type provides main parameters and sql table result (S7-1200 version).
Default
Name Data type Comment
value
ColumnCount UInt 0 The number of readed columns
RowCount UInt 0 The number of readed rows
Maximum allowed number of columns.
MaxColumns UInt 10 The value must be less than or equal to
tSqlTable.Columns array size
Maximum allowed number of rows. The
MaxRows UInt 10 value must be less than or equal to
tColumn.Rows array size
Columns Array[1..10] of "tColumn_1200" Array of columns
11
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
1.6.12 tSqlTable_1500
This data type provides main parameters and sql table result (S7-1500 version).
Default
Name Data type Comment
value
ColumnCount UInt 0 The number of readed columns
RowCount UInt 0 The number of readed rows
Maximum allowed number of columns.
MaxColumns UInt 10 The value must be less than or equal to
tSqlTable.Columns array size
Maximum allowed number of rows.
MaxRows UInt 10 The value must be less than or equal to
tColumn.Rows array size
Columns Array[1..10] of "tColumn_1500" Array of columns
The MaxColumns parameter must be less than equal to the array size of SqlTable.Columns and
the MaxRows parameter must be less than equal to the array size of tColumn.Rows
otherwise the PLC may go to
1.7 Errors
Here is the list of errors written to the tStat.Status variable as output from the function block
FB_PDSql. During the error code, a more detailed description of the error is written to the
tStat.Message.
Table 1.3
Code Error message Tips
W#16#0000 No error
W#16#8001 Cannot connect to the remote server Check IP address and port
W#16#8002 Conection timeout Check IP address and port
W#16#8003 Login failed See details in tStat.Message
Something is wrong with ethernet
W#16#8004 Disconection timeout
connection
W#16#8005 MySQL Command execution timeout MySQL Server does not respond
W#16#8006 MySQL Packet error See details in tStat.Message
W#16#8007 MySQL Command execution error See details in tStat.Message
W#16#8080 Invalid license key Check the license key
12
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
13
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
2 Instalation
→ „Download“
14
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
15
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
→ „Next >“
→ „Next >“
16
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
Select High Availability mode (e.g. Standalone MySQL Server ...) → „Next >“
Check TCP/IP, select Port number (default 3306), check Open Windows Firewall ports
for network access → „Next >“
17
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
18
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
→ „Execute“
19
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
→ „Next >“
→ „Finish“
20
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
You will need to run MySQL Workbench to be able to connect and manage the MySQL
Server.
21
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
To access the MySQL server through a MySQL account from any host you must set up the
following:
22
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
❷
❶
23
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
24
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
➢ Drag & Drop Function Block from library to your Project / Program blocks
This action will also automatically transfer all necessary data types from library to
Project / PLC data types
25
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
➢ Drag & Drop Functions from library to your Project / Program blocks
26
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
➢ Drag & Drop Master copies / S7-1200 data blocks from library to your Project / Program
blocks
27
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
3 Examples
• or
• MySQL Server
• PDSql Library My.
28
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
Table: „test_table“
In the TIA environment you must add a dollar ($) sign before each single quote in
the query string !
29
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
Id = 1
Datetime =
“2019-07-01-23:42:18”
FruitName = “Apple”
30
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
Quantity = 10
Weight = 2.54
CitrusType = FALSE
31
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
In the TIA environment you must add a dollar ($) sign before each single quote
in the query string !
"DB_PDSql_1200".Query.Query[1] := #str;
"DB_PDSql_1200".Query.Query[2] := '';
32
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
33
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
In this example we will update a record without specifying a key in the where
clause, so we need to set SQL_SAFE_UPDATES = 0
SET SQL_SAFE_UPDATES = 0;
UPDATE plcdb.test_table SET Quantity = 32, Weight = 12.68 WHERE FruitName = 'Mango'
In the TIA environment you must add a dollar ($) sign before each single quote
in the query string !
#sFruitName := 'Mango';
#iQuantity := 32;
#rWeight := 12.68;
"DB_PDSql_1200".Query.Query[1] := #str;
"DB_PDSql_1200".Query.Query[2] := '';
34
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
35
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
In this example we will delete a record without specifying a key in the where
clause, so we need to set SQL_SAFE_UPDATES = 0
SET SQL_SAFE_UPDATES = 0
In the TIA environment you must add a dollar ($) sign before each single quote
in the query string !
#sFruitName := 'Lemon';
"DB_PDSql_1200".Query.Query[1] := #str;
"DB_PDSql_1200".Query.Query[2] := '';
36
PDSql Library My v1.3.4 www.plcdirectsql.com
MySQL Functionality for Siemens S7-1200/1500 (TIA Portal V13, V14, V15, V16)
37
PDSql Library My v1.3.4 www.plcdirectsql.com