TKH S7Plus.NET library for communicating with S7 PLCs
$ dotnet add package TKH.S7Plus.NetS7Plus.NET is a .NET library for communicating with Siemens S7-1200 and S7-1500 PLCs using the extended S7 protocol. The extended S7 protocol allows access to "optimized" datablocks. For this to work you need to enable "Secure PG/PC and HMI Communication".
The library is based on the excellent work of Thomas Wiens in the S7CommPlusDriver project. (https://github.com/thomas-v2/S7CommPlusDriver)
The library is available as a NuGet package. You can install it using the following command:
dotnet add package TKH.S7Plus.Net
Create an instance of the S7Driver and connect to the PLC:
using TKH.S7Plus.Net;
using System;
IS7Driver driver = new S7Driver();
driver.SetTimeout(TimeSpan.FromSeconds(5));
await driver.Connect("192.168.178.140", 102);
Write a boolean variable to the PLC:
using TKH.S7Plus.Net.Models;
using TKH.S7Plus.Net.S7Variables;
await driver.SetVariable(new S7AbsoluteAddress(1, 9), new S7VariableBool(true));
Read a variable from the PLC using the symbol name:
List<Datablock> datablocks = await driver.GetDatablocks();
VariableInfo? variableInfo = await driver.GetVariableInfoBySymbol("DB1.TEST_DINT", datablocks);
var variableResult = await driver.GetVariable(variableInfo!.Address);
Read an array variable from the PLC using the symbol name:
variableInfo = await driver.GetVariableInfoBySymbol("DB1.TEST_ARRAY", datablocks);
variableResult = await driver.GetVariable(variableInfo!.Address);
S7VariableDIntArray dintArray = (S7VariableDIntArray)variableResult;
Console.WriteLine($"Value of DB1.TEST_ARRAY: {string.Join(", ", dintArray.Value)}");
Disconnect from the PLC:
driver.Disconnect();
This project is licensed under the LGPL-3.0 License.
This library is provided "as is" without any warranties. Use of this library in industrial environments is at your own risk. The author assumes no liability for damage resulting from incorrect use.
Only use with systems you are authorized to access. Do not bypass security mechanisms.
This project is an independent implementation based solely on publicly available information, academic research, and observed network behavior for the purpose of interoperability.
It is not affiliated with, endorsed by, or connected to Siemens AG. All trademarks belong to their respective owners.