User Guide MSSQL Library For TIA Portal V13 V14 V15
User Guide MSSQL Library For TIA Portal V13 V14 V15
& Newer
User guide
V1.5.3.0
Author:
Tomáš Krajcar
nám. Mı́ru 1205/9
767 01 Kroměřı́ž
Czech Republic
www.plc2sql.com
mail: [email protected]
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 fbMsSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.1 fcGetBOOL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.2 fcGetDINT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.3 fcGetSTRING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.4 fcGetREAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.5 fcGetDATETIME . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.6 Licensing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Installation 8
2.1 Installation MsSQL 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 Microsoft SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.1 Enable SQL authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.2 Create database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2.3 Create new user . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.2.4 Create new table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.2.5 Test connection to the database with new user . . . . . . . . . . . . . . . . . . . . 18
3.2.6 Setup firewall permission for SQL Server . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.7 SQL Server configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4 Change Log 28
1
Chapter 1
Product description
This library allows you to connect your PLC Siemens S7-1200 or S7-1500 application to Microsoft SQL
database. 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 Microsoft SQL Server (2005 or
higher).
• INSERT
• UPDATE
• DELETE
2
1.3 Function blocks
1.3.1 fbMsSQL
FB for complete connecting and executing commands with Microsoft SQL from 2005 to 2017.
Author: krajcart
V1.5.3.0
3
1.4 Functions
1.4.1 fcGetBOOL
FC for converting raw SQL data to datatype BOOL.
Allowed data types: 16#32(TDS DATA BIT1) or 16#68(TDS DATA BITN)
Author: krajcart
V1.5.0.0
1.4.2 fcGetDINT
FC for converting raw SQL data to datatype DINT.
Allowed data types: 16#26(TDS DATA INTN) or 16#38(TDS DATA INT4)
or 16#7F(TDS DATA INT8) or 16#34(TDS DATA INT2)
Author: krajcart
V1.5.1.0
1.4.3 fcGetSTRING
FC for converting raw SQL data to datatype STRING.
Allowed data types: 16#E7(TDS DATA NVARCHAR) or 16#A7(TDS DATA BIGVARCHAR)
Author: krajcart
V1.5.1.0
4
1.4.4 fcGetREAL
FC for converting raw SQL data to datatype REAL(4 bytes) and LREAL(8 bytes) only S7-1500.
Allowed data types: 16#6D(TDS DATA FLOATN) or 16#3E(TDS DATA FLOAT8)
or 16#3B(TDS DATA FLOAT4)
Author: krajcart
V1.5.1.0
1.4.5 fcGetDATETIME
FC for converting raw SQL data to datatype DTL
Allowed data types: 16#3D(DATETIME8) or 16#6F(DATETIMEN)
Author: krajcart
V1.5.0.0
5
1.5 Errors
List of errors stored in value dwError as output from fbMsSql.
6
Table 1.8: Error table convert functions
1.6 Licensing
MsSQL library for TIA Portal V13, V14, V15 and V15.1 is licensed per runtime license. Each license
is binded to SN of PLC. Price for one runtime license is 199e without VAT. Without valid license
library works only 2 hours after startup. Link for purchase: https://www.plc2sql.com/products/
mssql-library-for-tia-portal-v13-v14-v15/#license
7
Chapter 2
Installation
Function Blocks
• fbMsSQL (S7-1200 & S7-1500)
Functions
• fcGetBOOL (S7-1200 & S7-1500)
• fcGetDINT (S7-1200 & S7-1500)
• fcGetSTRING (S7-1200 & S7-1500)
• stLogin
• stQuery
• stSqlResponse
8
This library package contains:
9
Limits of data to read from SQL
fbMsSQL has internally setup array bound for data able to read from MsSQL. 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.
10
Chapter 3
Example application
On the picture 3.1 is main program in OB1 with fbMsSQL and function block. All example files are in
Global Library. Master copies S7-1200 or S7-1500.
11
3.1 Example commands for SQL Server
3.1.1 tblTestPLC
In this table we have 6 collumns:
• Bools [bit]
• String [nvarchar(50)]
• ID [int]
• Real [float]
• DoubleInt [bigint]
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
) ON [PRIMARY]
GO
SELECT [Bools]
,[String]
,[ID]
,[Real]
,[DoubleInt]
FROM [test].[dbo].[tblTestPLC]
Table 3.1: Example of values, which you can add to your tblTestPLC table
12
3.2 Installation of Microsoft SQL Server
How to install Microsoft SQL Server is shown on this video: https://www.youtube.com/watch?v=
QFyetK805bo
In subsections below are shown necessary steps which has to be done on SQL Server side to be accessible
from PLC.
Connect to your database with Microsoft SQL Management Studio. You will login with Windows
authentication.
13
After successful connection to the SQL Server. Right click on the SQL server icon and choose
Properties.
In Server Properties choose page Security, and choose SQL Server and Windows Authentica-
tion mode.
14
3.2.2 Create database
Now we need to create test database. Right click on tab Database, a choose New Database.
In this window we will set only Database name: in our case name will be test and press OK.
15
In this window we will fill these columns:
• Login name - name of new user
Another tab is User Mapping. In this tab we will set permissions for this user. We will map our
database test. And for this database we will set these permissions:
16
3.2.4 Create new table
Left click on our database test in tab Databases and then choose New Query.
Into the new window we will insert code from figure: 3.4. And after press F5 to execute command.
17
/****** This command is for db test ******/
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
) ON [PRIMARY]
GO
18
In the login screen we will change Windows authentication to SQL Server Authentication,
and fill Login and Password of our new user.
Next step is to try if permission which we set earlier to our user. So in tab Databases -> Test ->
Tables -> tblTestPLC right click and choose Edit Top 200 Rows. And this command will open window
where we can modify data in our table.
In table 3.2 are shown sample data which you can put into this table.
Table 3.2: Example of values, which you can add to your tblTestPLC table
19
Last step is to try which data are in the table. Example of Select command is here: 3.2.
20
3.2.6 Setup firewall permission for SQL Server
Open Windows Firewall with Advanced Security. Open Run and insert command wf.msc
On left side choose Inbound Rules by left click. This will open list with all Inbound rules set for
your PC. On right side choose New Rule.
This wizard will help us to set correct firewall rule for our PC. First we have to choose Port and
press Next.
21
Here we will specify on which port is our SQL server running, default port is 1433. It’s adjustable
in settings of SQL Server.
22
In this last windows we will choose for which types of zones will be rule applied.
Note: this is just basic example for enable firewall!!
23
3.2.7 SQL Server configuration
First thing is to open Computer management. Easiest way how to do that is to open Run: insert
command compmgmt.msc
On left side choose Services and Applications -> SQL Server Configuration Manager -> SQL Native
Client 11.0 Configuration -> Client Protocols. All 3 protocols has to be Enabled.
Note: SQL Native Client 11.0 Configuration naming is different in every version of SQL Server
and also version(32 or 64 bits)!!
24
By right click on TCP/IP you can edit Properties:
• Default Port - port on which will be your SQL server reachable across network
• Enabled - Enabling of TCP/IP protocol
25
Another settings will be made in SQL Server Network Configuration. Click on Protocols for SQL-
EXPRESS. Also all 3 protocols has to be Enabled. We need to check if port is set correctly right click
on TCP/IP and choose tab IP Addresses.
Here is set the default port for SQL Server and also all IP addresses for connection to the SQL
Server. And click yes to parameter Enabled on each IP address which you want to have active for SQL.
We activated 127.0.0.1(localhost). So for your ip 192.168.1.1 you have to scroll up and find the right
interface.
26
In case you have 64 bit SQL Server you have to also Enable Protocols in 64 bits client.
27
Chapter 4
Change Log
V 1.0.0.0
• Released
V 1.2.0.0
• UPDATE - Updated library to TIA V14 SP1.
• UPDATE - Performance improvement.
• BUG - Check string for null value.
V 1.2.1.0
• BUG - Check string for length 0.
V 1.2.2.0
• BUG - Reading errors from Server.
• UPDATE - Updated library to TIA V13 SP2.
V 1.3.0.0
• UPDATE - Modified library to work without external datablock for memory movement(DINT,STRING),
optimized performance and created TIA Global library.
V 1.3.1.0
• UPDATE - Modified library to work without external datablock for memory movement for REAL
datatype.
V 1.3.2.0
• UPDATE - Length of bytes readed from SQL for one row was increased from 50 bytes to 300
bytes.
28
V 1.4.4.0
• UPDATE - Add variable interfaceID, which allows to set which interface will be used for MsSQL
Library connection.
• UPDATE - Changed structure stQuery. Now you it possible to send query to database through
asQuery : Array[0..9] of String.
• UPDATE - In/Out Variables xConnectSQL, xExecuteSQL, xDisconnectSQL reacts on Rising
Edge of this signal.
• BUG - dwError and sError will show error after unsuccesfull Connection.
V 1.4.11.0
• UPDATE - Add variables DONE, BUSY, ERROR.
• UPDATE - Add function fcGetDATETIME.
• BUG - sError show message in correct step call.
• UPDATE - Library for S7-1200 temporarily disabled.
V 1.4.12.0
• BUG - Wrongly interpreted new row Token.
• BUG - Wrongly readed token about finished procedure inside of readed data.
• UPDATE - Added sVersion on output.
• UPDATE - Library for S7-1200 is now enabled but without demo, due missing system function
in S7-1200.
V 1.5.0.0
• UPDATE - Refactoring of whole library, delete unused variables and improve performance.
V 1.5.1.0
• UPDATE - Library disconnects from SQL when connection is lost.
• UPDATE - Convert function for DINT, REAL was updated for S7-1200 to show error 16#07
when it’s 8 byte variable which PLC cannot handle.
• UPDATE - Convert function for STRING is updated to shorten string, if it’s longer than 255
bytes.
V 1.5.2.0
• UPDATE - Library has now implemented skiping tokens TABNAME and COLINFO. This feature
is neccesary for some advanced stored procedures.
V 1.5.3.0
• UPDATE - Added skiping not-implemented TOKENS (RETURN STATUS, DONEPROC, DONEIN-
PROC) from SQL Server.
29