Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 865
Peyman Majidi Avatar asked Jun 16 '20 07:06

Peyman Majidi


People also ask

How do I connect to a SQL Server database?

Connect to a SQL Server instanceStart SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine. For Server type, select Database Engine (usually the default option).

What is SQL in PLC?

Structured Query language or SQL is the language of choice for most modern multi-user relational databases. This is due to the fact that SQL provides the syntax and language you need to talk to, or query, a relational database. The most common SQL databases in manufacturing are Microsoft SQL Server and MySQL.

How do I connect to a local network in SQL Server?

In SQL Server Configuration Manager, expand SQL Server Network Configuration, and then click on the server instance you want to configure. In the right pane, double-click TCP/IP. In the TCP/IP Properties dialog box, click the IP Addresses tab. In the TCP Port box of the IPAll section, type an available port number.


Video Answer


1 Answers

After a lot of searches and reading documents, I found out Siemens still developing the MSSql Server Module, and now, it isn't completed. The data will be inside the Receivebuffer: InstLsql_Microsoft.statRcvData.bytes[]. But you have to search for your information.

But still, the best way to read and write on PLC Registers is using S7netplus library (A .NET Library for Siemens S7 Connectivity). S7.Net Plus is a continuation of the work done on the S7.Net project by Juergen1969. I found the library simple and effective, but the project has languished unchanged since late 2009.

Download and install on C# Project:

PM> Install-Package S7netplus -Version 0.4.0

To create an instance of the driver you need to use this constructor:

public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)

Example:

This code creates a Plc object for a S7-300 plc at the IP address 127.0.0.1, for a plc in rack 0 with the cpu in slot 2:

Plc plc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);

Connecting to the PLC

public void Open()

For example this line of code open the connection:

plc.Open();

Disconnecting from the PLC

public void Close()

For example this closes the connection:

plc.Close();

Read bytes / Write bytes

The library offers several methods to read variables. The basic one and the most used is ReadBytes.

public byte[] ReadBytes(DataType dataType, int db, int startByteAdr, int count)

public void WriteBytes(DataType dataType, int db, int startByteAdr, byte[] value)

This reads all the bytes you specify from a given memory location. This method handles multiple requests automatically in case the number of bytes exceeds the maximum bytes that can be transferred in a single request.

dataType: you have to specify the memory location with the enum DataType

public enum DataType
{
    Input = 129,
    Output = 130,
    Memory = 131,
    DataBlock = 132,
    Timer = 29,
    Counter = 28
}

db: the address of the dataType, for example, if you want to read DB1, this field is “1”; if you want to read T45, this field is 45. startByteAdr: the address of the first byte that you want to read, for example, if you want to read DB1.DBW200, this is 200. count: contains how many bytes you want to read. Value[]: array of bytes to be written to the plc. Example: This method reads the first 200 bytes of DB1:

var bytes = plc.ReadBytes(DataType.DataBlock, 1, 0, 200);

More Documention is here:
S7.Net documentation

like image 173
Peyman Majidi Avatar answered Oct 10 '22 00:10

Peyman Majidi