# S7CommPlusV3Driver **Repository Path**: lircy/S7CommPlusV3Driver ## Basic Information - **Project Name**: S7CommPlusV3Driver - **Description**: 此通讯驱动基于.Net Framework4.0开发,它能在不启用TLS场景下与西门子 S7-1200、S7-1500实现通讯,支持在线或从博途工程文件(.ap10~.ap21,.zap10~.zap21)浏览全局符号变量树,通过符号变量名批量读取变量值,支持通过符号变量写入值 - **Primary Language**: C# - **License**: LGPL-2.1 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-05 - **Last Updated**: 2026-11-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # S7CommPlusDriver A .NET driver for the S7CommPlus protocol, communicating with Siemens S7-1200 / S7-1500 series PLCs. Shipped as a prebuilt library with a console demo, a WinForms demo, and a native-library test demo. - Target framework: .NET Framework 4.0+ (buildable with VS2017+) - Namespace: `S7CommPlusDriver` - Default port: TCP 102 - AnyCPU build: a single output runs on both x86 and x64. --- ## ⚠️ Important Notices 1. **This project is based on [thomas-v2/S7CommPlusDriver](https://github.com/thomas-v2/S7CommPlusDriver).** Credit for the original protocol implementation goes to Thomas Wiens. 2. **The non-TLS authentication native library (HarpoS7Native) is based on [bonk-dev/HarpoS7](https://github.com/bonk-dev/HarpoS7).** 3. **This driver is still under active development and testing. DO NOT use it in production environments.** The protocol implementation may contain unknown defects, and production use could result in equipment downtime, data loss, or other severe consequences. --- ## Features - S7CommPlus protocol stack (ProtocolVersion V1 / V2 / V3) - TLS and non-TLS connections, auto-detected; supports PLCSIM Advanced (soft PLC) and physical S7-1200/S7-1500 - Automatic session key management - **Offline TIA Portal symbol loading**: V10.5–V21 project files (`.apXX`) and project archives (`.zapXX`) - Dynamic PLC variable tree browsing (nested UDT / FB / Struct / multi-dimensional arrays / Failsafe DBs) - Batch read/write with automatic PDU-size-aware fragmentation - Subscription and alarming support - PLC operating state control (RUN / STOP) - Tag comment retrieval (XML format, multi-language) - Password legitimation ## Package Layout ``` S7CommPlusDriver.sln Solution file ├─ Lib\ Prebuilt assemblies + native dependencies │ ├─ S7CommPlusDriver.dll Driver library (namespace: S7CommPlusDriver) │ ├─ HarpoS7Wrapper.dll Non-TLS auth wrapper │ ├─ HarpoS7Native_x64.dll / _x86.dll Native auth libraries │ ├─ libcrypto-3*.dll / libssl-3*.dll OpenSSL 3.x (TLS) │ ├─ TiaProjectParser.dll TIA Portal project parser │ ├─ BouncyCastle.Crypto.dll / Newtonsoft.Json.dll / System.ValueTuple.dll / zlib.net.dll │ └─ Net4x.Extensions.Logging.Abstractions.dll ILogger abstraction dependency ├─ TestS7CommPlusDriver.Console\ Console demo ├─ TestS7CommPlusDriver.GUI\ WinForms demo └─ TestHarpoS7Native\ Native-library test demo ``` ## Quick Start ```csharp using S7CommPlusDriver; var driver = new S7CommPlusConnection(); // driver.Logger = new MyLogger(); // optional: inject Microsoft.Extensions.Logging.ILogger bool ok = driver.Connect("192.168.0.1", 102, "user", "password"); // true = success // Offline TIA Portal project (.apXX / .zapXX), or online from the PLC. // LoadSymbolsFromFile returns 0 on success. bool loaded = driver.LoadSymbolsFromFile("project.ap17") == 0; // or: driver.LoadSymbolsFromPLC() // Browse the tree: null = top level; pass a node's SymbolPath to expand it List groups = driver.Browse(null); List leaves = driver.LoadAllSymbolicInfo(); // all leaf symbols // Batch read var names = new List { "DB1.Start", "DB1.Speed" }; List values; ok = driver.Read(names, out values); // values[i].ToString() shows the value // Write (by text, auto-parsed to the symbol type; or typed value) ok = driver.WriteByText("DB1.Start", "true"); ok = driver.Write("DB1.Speed", new ValueReal(12.5f)); driver.Disconnect(); // idempotent; Dispose() is equivalent ``` ## API Reference ### S7CommPlusConnection — Methods | Member | Description | |---|---| | `bool Connect(string host, int port = 102, string user = "", string password = "", int timeoutMs = 5000)` | Connect (TLS / non-TLS auto-detected); `true` on success | | `void Disconnect()` | Disconnect and release the link (idempotent; `Dispose()` is equivalent) | | `bool LoadSymbolsFromPLC()` | Load the symbol tree online from the PLC | | `int LoadSymbolsFromFile(string path)` | Load the symbol tree from a TIA Portal project (`.apXX`) or archive (`.zapXX`); returns `0` on success | | `List Browse(string symbolPath)` | Expand one level of the tree (`null` = top level; lazy loading) | | `List LoadAllSymbolicInfo()` | Fully expand and return all symbols | | `bool Read(List names, out List values)` | Batch read | | `bool Write(string name, PValue value)` | Single typed write (e.g. `new ValueReal(12.5f)`) | | `bool Write(List names, List values)` | Batch write | | `bool WriteByText(string name, string text)` | Write by text, auto-parsed to the symbol type | ### S7CommPlusConnection — Properties | Member | Description | |---|---| | `bool IsConnected` | Connection state | | `DeviceInfo DeviceInformation` | Device info (available after Connect) | | `int LastErrorCode` / `string LastErrorText` | Per-instance result of the last operation | | `ILogger Logger` | Optional logging injection (NullLogger when unset) | | `LogLevel MinimumLogLevel` | Minimum log level forwarded to `Logger` | | `bool UseSecureCommunication` | Whether TLS secure communication is in use | | `ReconnectOptions Reconnect` | Auto-reconnect config: `Enabled` / `MaxRetries` / `InitialDelayMs` / `MaxDelayMs` / `Multiplier` | | `int ReconnectAttempts` | Reconnect attempt counter | | Events `ConnectionLost` / `Reconnecting` / `Reconnected` | Reconnect notifications (background thread; UI must marshal) | ### SymbolNode (tree node / symbol) | Member | Description | |---|---| | `string Name` / `string SymbolPath` | Node name / full symbol path | | `VarType Datatype` | Data type | | `bool IsContainer` | `true` for groups / structs / arrays that can be expanded further | | `AccessRight Access` | Access right (read / write / none) | ## Building Open `S7CommPlusDriver.sln` in Visual Studio 2017+ and build; or from the command line: ``` MSBuild S7CommPlusDriver.sln /p:Configuration=Debug ``` All demo projects reference the prebuilt assemblies in `Lib\`. The native libraries (HarpoS7Native, OpenSSL 3.x) are shipped in `Lib\` and copied to the output directory automatically — no C++ toolchain or separate native build is required. ## Test Demos **Console** (`TestS7CommPlusDriver.Console.exe`): ``` TestS7CommPlusDriver.Console.exe [port=102] [user] [password] [projectPath] ``` Connects to the PLC, loads the symbol tree (offline TIA Portal project when `projectPath` is given, otherwise online from the PLC), prints the top-level symbol groups, sample-reads up to 8 readable leaf variables, then disconnects. Read-only demo — write examples are shown as comments at the end of `Program.cs`. **GUI** (`TestS7CommPlusDriver.GUI.exe`): enter host/port/credentials in the connection area -> load a TIA Portal project or fetch the online tree -> pick variables in the tree (lazy expansion) -> single-point read/write. Driver logs are written to the log area through the injected ILogger. ## License This project is licensed under the [GNU Lesser General Public License v3](https://www.gnu.org/licenses/lgpl-3.0.html), consistent with the original project. ``` S7CommPlusDriver Copyright (C) 2023 Thomas Wiens, th.wiens@gmx.de This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ``` ## Acknowledgments - [thomas-v2/S7CommPlusDriver](https://github.com/thomas-v2/S7CommPlusDriver) — Original S7CommPlus protocol .NET implementation - [bonk-dev/HarpoS7](https://github.com/bonk-dev/HarpoS7) — S7CommPlus non-TLS authentication native library