0% found this document useful (0 votes)
70 views15 pages

User Guide MySQL Library For TIA Portal

The document is a user guide for the MySQL Library compatible with TIA Portal V13 and newer, enabling connection between Siemens S7-1200/S7-1500 PLCs and MySQL or MariaDB databases. It details the library's functions, supported data types, installation instructions, and licensing information. The guide includes examples of function blocks and error handling related to database operations.

Uploaded by

daviddalvikjv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views15 pages

User Guide MySQL Library For TIA Portal

The document is a user guide for the MySQL Library compatible with TIA Portal V13 and newer, enabling connection between Siemens S7-1200/S7-1500 PLCs and MySQL or MariaDB databases. It details the library's functions, supported data types, installation instructions, and licensing information. The guide includes examples of function blocks and error handling related to database operations.

Uploaded by

daviddalvikjv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

MySQL Library for TIA Portal V13 &

Newer

User guide

V1.0.0.0

Author:
Tomáš Krajcar
nám. Mı́ru 1205/9
767 01 Kroměřı́ž
Czech Republic
www.plc2sql.com
mail: [email protected]

Last revision: March 2020

Copyright c by Tomáš Krajcar 2020


All rights reserved
Contents

1 Product description 2
1.1 Range of function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Supported datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Function blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.1 fbMySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.1 fcGetValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.2 fcDtlToString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.3 fcStringToDtl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

2 Installation 8
2.1 Installation MySQL library to TIA Portal . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

3 Example application 11
3.1 Example commands for SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.1.1 tblTestPLC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2 Installation of MySQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

4 Change Log 14

1
Chapter 1

Product description

This library allows you to connect your PLC Siemens S7-1200 or S7-1500 application to MySQL database
or MariaDB. With this library you are able to store and read process data. Read recipes from ERP, save
breakdown report and etc. No more OPC servers and other middleware.

Basic requirement for this library is TIA Portal V13 or higher and MySQL Server (V5 or higher).

MySQL server installation is there: https://dev.mysql.com/downloads/installer/

1.1 Range of function


This library allows to the user connect to MySQL Server V5 and higher.
Supported commands:
• SELECT
• INSERT
• UPDATE

• DELETE
• Execute stored procedures

1.2 Supported datatypes


Supported datatypes are mentioned in chapter 1.4.1. MySQL returns all data in text format. So basically
everything can be converted from STRING to required datatype.

2
1.3 Function blocks
1.3.1 fbMySQL
FB for complete connecting and executing commands with Microsoft SQL from 2005 to 2017.
Author: krajcart
V1.0.0.0

Table 1.1: fbMySQL

Scope Name Type Inital Comment


stConnectData stConnectData structure for login to MySQL
stQuery stQuery structure for stQuery
Input tTimeout Time T#10s timeout
sActivationNumber String activation number received after purchase
interfaceID HW ANY 64 X1(64), X2(72)
xConnectedSuccessfully Bool indication of successfull conection to SQL
dwError DWord error identifier
wRowDoneCnt Word no. of rows done
sStatus String status message
sErrorMessage String error message
Output xLicenseValid Bool info about license
DONE Bool signal that operation was finished OK
BUSY Bool signal that operation is busy
ERR0R Bool signal that operation ended with Error
sVersion String[10] info about current library version
xConnectSQL Bool trigger for login,to SQL
xDisconnectSQL Bool trigger for logout from SQL
xExecuteSQL Bool trigger to execute SQL command executing
InOut
stSqlResponse stSqlResponse structure for SQL Response

Structure stConnectData

Table 1.2: stConnectData

Name Type Initial Comment


UserName STRING user name
Password STRING password
Database STRING name of database
ConnID CONN OUC 10 connection ID in Simatic
usiIP1 USINT 1st octet of IP Address
usiIP2 USINT 2nd octet of IP Address
usiIP3 USINT 3rd octet of IP Address
usiIP4 USINT 4th octet of IP Address
uiPort UINT 3306 port for MySQL

3
1.4 Functions
1.4.1 fcGetValue
FC for converting raw SQL data to datatype STRING. Not supported type will be notified.
Allowed data types: TINY,DECIMAL, SHORT, LONG, INT24, LONGLONG, VAR STRING, STRING,
VARCHAR, DECIMAL, NEWDECIMAL, FLOAT, DOUBLE, DATETIME, TIMESTAMP, DATE,
TIME.
Author: krajcart
V1.0.0.0

Table 1.3: fcGetValue

Scope Name Type Inital Comment


iCol Int index of selected column
Input
iRow Int index of selected row
stSqlResponse stSqlResponse response structure
eError Word error variable look into tab.1.7.
InOut
sValue String return value
uiLength Uint length of string variable

"dbSQL_data".diValue := STRING_TO_DINT("dbSQL_data".sValue);
"dbSQL_data".rValue := STRING_TO_REAL("dbSQL_data".sValue);
"dbSQL_data".xValue := INT_TO_BOOL(STRING_TO_INT("dbSQL_data".sValue));

Figure 1.1: Example how to convert string value to other datatype

1.4.2 fcDtlToString
FC for converting datetime to string for inserting to SQL.
Allowed data types: DTL
Author: krajcart
V1.0.0.0

Table 1.4: fcDtlToString

Scope Name Type Inital Comment


Input dtlIn DTL datetime insert
Output fcDtlToString STRING with string value

4
1.4.3 fcStringToDtl
FC for converting string to DTL.
Allowed data types: STRING
Author: krajcart
V1.0.0.0

Table 1.5: fcDtlToString

Scope Name Type Inital Comment


Input sValue STRING datetime insert
Output fcStringToDtl DTL

5
1.5 Errors
List of errors stored in value dwError as output from fbMySql.

Table 1.6: Error table

Code Error message Advice


16#400000 No error.
Remote server unreachable, try fbPing
16#400001 Timeout destination server is unreachable.
and firewall.
Wrong login or security settings on
16#400002 Login failed.
server side.
16#400003 Command failed. Executed command failed.
MYSQL-ERROR: Invalid or unexpected re-
16#400007 My SQL error.
sponse from server.
16#400009 No command to execute. Write some SQL command.
Result set has more columns then defined in Increase c iNoOfColumns constant in
16#400010
”c iNoOfColumns”. global parameters.
MYSQL-WARNING: Number of received
Increase c iNoOfRows constant in
16#400011 rows exceed maximum of ”c iNoOfRows” -
global parameters
Some rows are ignored”.
MYSQL-WARNING: Number of received
Increase gc iLengthOfDataArray con-
16#400012 rows exceed maximum of ”c iNoOfRows” -
stant in global parameters.
Some rows are ignored”.
16#400014 License is not valid! Demo expired. Buy the license.
16#400016 stConnectData is not filled completely. Fill all variables in sqlLogin.
Request 4.0 athentication, old we will not im-
16#400019
plement, s≥ 4.1 only.
MYSQL-ERROR: Unsupported value for
16#400021 ”column count”, provided as 64-bit value(8 My SQL error.
bytes) in ”Result-Set-Header-Packet”.
MYSQL-ERROR: Unsupported value for
16#400022 ”column count”, provided as 24-bit value(3 My SQL error.
bytes) in ”Result-Set-Header-Packet”.
MYSQL-ERROR: Unsupported value for
16#400023 ”column count”, provided as 16-bit value(2 My SQL error.
bytes) in ”Result-Set-Header-Packet
MYSQL-ERROR: Invalid or unexpected re-
16#400024 My SQL error.
sponse packet from server.
Look in which step it failed and try to
16#400025 State machine ended on timeout.
increase timeout.
MYSQL-ERROR: Received more data than
16#400027 My SQL Error.
declared in message header.
MYSQL-ERROR: Greeting message contains
16#400029 My SQL Error.
wrong data.

6
Table 1.7: Error table convert functions

eError Error message Advice


16#00 No error.
16#01 iRow is 0. Set variable iRow bigger than 0.
Set variable iRow between 1 and stSqlRe-
16#02 iRow is out of range.
sponse.uiRowCount.
16#03 iColumn is 0. Set variable iColumn bigger than 0.
Set variable iRow between 1 and stSqlRe-
16#04 iColumn is out of range.
sponse.uiColumnCount.
16#05 Unsupported datatype. Please look into sValue for more info .

1.6 Licensing
MySQL library for TIA Portal V13, V14, V15, V15.1, V16 is licensed per runtime license. Each license
is binded to SN of PLC. Price for one runtime license is 149e without VAT. Without valid license
library works only 2 hours after startup. Link for purchase: https://www.plc2sql.com/products/
mysql-library-for-tia-portal-v13-newer/#license

7
Chapter 2

Installation

2.1 Installation MySQL library to TIA Portal


Open example project for TIA Portal. Copy these block into your TIA Portal projects1 :

Function Blocks
• fbMySQL (S7-1200 & S7-1500)

Functions
• fcGetValue (S7-1200 & S7-1500)

PLC data types


• astColData
• astRowDet
• stColumn

• stConnectData
• stQuery
• stSqlResponse

Recommended FW
• S7-1200 from FW4.2 and higher is recommended
• S7-1500 from FW2.0 and higher is recommeded

1 Numbers of fb, fc and db can be arbitrary

8
This library package contains:

• Library for TIA Portal V14, V15, V162


• Example project S7-1200 & S7-1500
• User guide

Figure 2.1: Loading Global Library

2 Library can be always converted to newer TIA enviroment

9
Limits of data to read from SQL
fbMySQL has internally setup array bound for data able to read from MySQL. Because this is about
definition of arrays and memory in PLC. In default you are able to read max 10 collumn, 10 rows and
each row can contain max 300 bytes of data.

Figure 2.2: Constants in fbMySQL Library

10
Chapter 3

Example application

On the picture 3.1 is main program in OB1 with fbMySQL and function block. All example files are in
Global Library. Master copies S7-1200 or S7-1500.

Figure 3.1: fbMySQL call in OB1(for S7-1512)

11
3.1 Example commands for SQL Server

CREATE DATABASE test /*!40100 COLLATE ’utf8_general_ci’ */

Figure 3.2: Create database test

3.1.1 tblTestPLC
In this table we have 6 collumns:
• Bools [bit]
• String [nvarchar(50)]
• ID [int]
• Real [float]
• DoubleInt [bigint]
• dateTime [datetime]

CREATE TABLE test.tblTestPLC ( Bools BOOLEAN NULL ,


String VARCHAR(50) NULL ,
ID SMALLINT NULL ,
RealVal REAL NULL ,
DoubleInt INT NULL,
dateTime DATETIME NULL )
ENGINE = InnoDB

Figure 3.3: Create tblTestPLC

SELECT Bools, String,ID, RealVal, DoubleInt, dateTime FROM tblTestPLC

Figure 3.4: SELECT from tblTestPLC

Table 3.1: Example of values, which you can add to your tblTestPLC table

[rows] Bools String ID Real DoubleInt dateTime


1 0 Test 12 456 13.2 445568 2020-03-29 11:46:36
2 1 Hello youtube 78964 0.4 44556 2020-03-29 11:46:36

INSERT INTO tblTestPLC (Bools, String,ID, RealVal, DoubleInt, dateTime)


VALUES (0, $’Test 12$’, 456, 13.2, 445568,$’2020-03-29 11:46:38$’)

Figure 3.5: INSERT to tblTestPLC

Note If you insert STRING, DATETIME and other datatypes from PLC to SQL you have to put
Dollar Sign($) before Apostrofe(´).

12
3.2 Installation of MySQL Server
Here is video how to install MariaDB: https://www.youtube.com/watch?v=Nvt7S9XVeXg

Here is video how to install MySQL: https://www.youtube.com/watch?v=WuBcTJnIuzo

13
Chapter 4

Change Log

V 1.0.0.0
• Released

14

You might also like