Warm tip: This article is reproduced from serverfault.com, please click

Connecting an PLC Siemens S7-1500 to an SQL Server Database

发布于 2020-06-16 07:55:17

The connection guide is here. I track the guide and do a lot of stuff. The connection between PLC and SQL server is ok, I've login successfully to SQL.
I can insert to tables, update, and execute store procedures.

But when I run a select query I don't know how to retrieve data and read the values.

Tia Portal

I use TIA PORTAL v16.

PLC: S7 1200 siemens

I have a simple table, I wanna simply read an integer, first with one row! then expand my work.

This is my table structure:

CREATE TABLE MyTable(
[id] [int] IDENTITY(1,1) NOT NULL,
[value] [int] NOT NULL,
)
) ON [PRIMARY]

and this is my select query:

SELECT [value]
FROM [dbo].[MyTable]
WHERE id = 12033

expected result:

20

SQL shot

https://support.industry.siemens.com/tf/ww/en/posts/connecting-an-plc-siemens-s7-1500-to-an-sql-server-database/239392/?page=0&pageSize=10#post939400

UPDATE

I answered my own question, Please attention, Siemens recently (11/20/2020) published a document about Connecting an S7-* series to Microsoft SQL Server Database, directly. So the verfied anwser is the classic and normal way to connect PLC to SQL Server and the next answer is the latest native solution. Have fun

Questioner
Peyman Majidi
Viewed
0
Peyman Majidi 2021-01-13 18:49:52

Noticable Update!!!

Siemens recently (11/20/2020) published a document about Connecting S7-* series to Microsoft SQL Server Database, directly and with NO middle-ware like OPC Servers or S7netplus package. It means you can run SQL queries like select and insert via an internal function in PLC.
This is a faster & native solution.

First, via TiaPortal I made a string array in a Program block. sqlcommands array

You should set up connection strings like shown in the picture below, which the IP Address of Microsoft Sql Server is 172.16.62.1:

enter image description here

Then I made a built-in PLC function by following instruction in these pdf published by Simense. setup plc function

I have a bunch of queries in my predefined string array, I choose one of them by its index as an input parameter to the PLC function.
then I downloaded changes to PLC. as a result I have hello world in MSSQL Server. enter code here hello world By far I ran a SQL Server Query by PLC itself!
Let's run another query and the hardest one: select.
I said the hardest because select has a bunch of records with various data type: like varchar, bool, DateTime.

The "select" instruction Using the "select" instruction, you can read data out of a database table and perform other operations on them in your controller. Below, we use an example to show how this instruction works and which modifications you will need to make for your query. The example uses the following query:

Select * from Mytable

The data queried are stored in the "SqlReceive" data block in the data type structure "typeUseCaseSpecificData". You must modify these data types individually for each query. You can do this as follows: 1. Start a Wireshark recording to find the packet length of the metadata. 2. Run the query "Select Amount from PLCDATA_2 where Fruit = $'Apple$'". 3. Stop the Wireshark recording and search for the response frame from the SQL server using the filter "TDS". 4. Select the frame and click on the line "Token – ColumnMetaData".

Ok, Now I wanna choose select query in the sqlqueries array:

SELECT _barcode FROM mytable where id = 4

select

Again I Active/deactive the input then I get my result, here I get 'Peyman Majidi' string by its id.

select result

Output:

'Peyman Majidi'

I can show you how it works by Wireshark by listening to tds protocol.
The Tabular Data Stream protocol (TDS) gives you the ability to establish a direct connection with a Microsoft SQL server. Using TDS, you can log in to an SQL server database and transmit SQL instructions. In this way it is possible to read data from the database, or send them to the database for storage.

wireshark