# S7NetPlusDev **Repository Path**: EricTeTe920/s7-net-plus-dev ## Basic Information - **Project Name**: S7NetPlusDev - **Description**: s7netplus魔改二开版本 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-07-29 - **Last Updated**: 2026-08-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # s7netplus #### A .NET Library for Siemens S7 Connectivity ## Overview S7.Net Plus is a continuation of the work done on the [S7.Net](http://s7net.codeplex.com/) project by [Juergen1969](http://www.codeplex.com/site/users/view/juergen1969). I found the library simple and effective, but the project has languished unchanged since late 2009. I was doing some automation work already and saw a few places where the code base could be improved. Because Juergen did not respond to my request for committing code, I decided to pick up where he left off here on GitHub. ## Documentation Check the Wiki and feel free to edit it: https://github.com/killnine/s7netplus/wiki ## Supported PLC + Compatible S7 PLC (S7-200, S7-300, S7-400, S7-1200, S7-1500) ## Supported frameworks + .NET Framework 4.5.2 and higher + .NET Standard 1.3 (.NET Core 1.0, UWP 10.0, Xamarin, ...) + .NET Standard 2.0 (.NET Core 2.0, .NET Framework 4.6.1) ## Compile You need at least Visual Studio 2017 (you can download the Community Edition for free). ## Nuget PM> Install-Package S7netplus ## Latest build (Appveyor) [![Build status](https://ci.appveyor.com/api/projects/status/ousjt8sn9b1w43p6?svg=true)](https://ci.appveyor.com/project/mesta1/s7netplus) [https://ci.appveyor.com/project/mesta1/s7netplus](https://ci.appveyor.com/project/mesta1/s7netplus) ## Running the tests Unit tests use Snap7 server. On Windows, the DLL is included with the test project. On other platforms, Snap7 must be installed manually before running tests. # S7NETPLUS_EX 魔改版 ## 二次开发——暴露返回值完成总结 ### 一、新增文件 #### 1. `WriteResult.cs` — 返回值模型 设计了 **`WriteStatusCode` 枚举** 和 **`WriteResult` 类**,用于明确标识写入成功或失败的具体原因: | 状态码 | 含义 | |--------|------| | `Success` | 写入成功 | | `ConnectionError` | PLC 连接未建立或已断开 | | `AddressOutOfRange` | 地址越界 | | `TypeMismatch` | 数据类型不匹配 | | `PlcRejectedWrite` | PLC 拒绝写入(对象不存在、数据类型不一致等) | | `CommunicationError` | 通信异常(网络中断、超时等) | | `InvalidAddress` | 地址格式无效 | | `ValueIsNull` | 写入值为 null | | `Canceled` | 操作被取消 | | `UnknownError` | 未知错误 | #### 2. `WriteSafeTests.cs` — 单元测试 + 调用示例 包含 **20 个测试用例**,全部通过,覆盖: - 正常写入(Bit/Byte/Word/DWord)并回读验证 - 类型不匹配(如向 Bit 地址写入 22.55 浮点数) - 连接断开、地址无效、值为 null 等异常场景 - 批量写入混合结果(部分成功、部分失败) - 同步 + 异步版本 ### 二、修改文件 #### 3. `PlcSynchronous.cs` — 同步方法 新增了以下核心方法: - **`VarTypeToCSharpType(VarType)`** — S7 VarType 到 C# 类型的映射关系表(含详尽中文注释) - **`ValidateValueType(VarType, object, count)`** — 类型强校验逻辑,在写入前拦截类型不匹配 - **`MapExceptionToStatusCode(Exception)`** — 将底层异常映射为友好的 WriteStatusCode - **`WriteSafe(string variable, object value)`** → `WriteResult` — 单点位安全写入 - **`WriteBatchSafe(IDictionary)`** → `WriteResult[]` — 批量安全写入 #### 4. `PlcAsynchronous.cs` — 异步方法 - **`WriteSafeAsync(string, object, CancellationToken)`** → `Task` - **`WriteBatchSafeAsync(IDictionary, CancellationToken)`** → `Task` ### 三、类型映射关系 | VarType | C# 类型 | 说明 | |---------|---------|------| | Bit | `bool` 或 `int`(0/1) | 特殊处理:接受 0/1 整数 | | Byte | `byte` | 8 位无符号 | | Word | `ushort` | 16 位无符号 | | Int | `short` | 16 位有符号 | | DWord | `uint` | 32 位无符号 | | DInt | `int` | 32 位有符号 | | Real | `float` | 32 位浮点 | | LReal | `double` | 64 位浮点 | | String/S7String/S7WString | `string` | 字符串 | | Timer/Counter | `ushort` | 定时器/计数器 | | DateTime/Date/DateTimeLong | `DateTime` | 日期时间 | | Time | `TimeSpan` | 时间间隔 | ### 四、核心设计特点 1. **类型强校验**:写入前校验 value 与目标地址 VarType 严格匹配,类型不匹配时直接返回 `TypeMismatch`,**不会写入错误数据**(如文档要求的"22.55 写入 Bit"会被拒绝) 2. **友好的返回值**:所有底层异常(`PlcException`、`SocketException` 等)都被捕获并转化为 `WriteResult`,不抛出异常 3. **批量写入独立容错**:某个点位失败不影响其他点位,每个点位独立返回结果 4. **详尽中文注释**:所有方法包含完整的 XML 文档注释,解释类型映射和校验逻辑 --- ## .NET Framework 4.8 编译结果 ### 修改内容 在 `S7.Net.csproj` 中新增了 `net48` 目标框架: - `TargetFrameworks` 增加 `net48` - `NET_FULL` 条件编译符号覆盖 `net48`(支持 `[Serializable]` 等 .NET Framework 特性) - `System.Memory` NuGet 包自动引用(提供 `Span` / `Memory` 支持) - 修复了文件开头的双重 BOM 编码问题 ### 输出文件 DLL 已输出到 `Release\NET48\` 文件夹,包含以下文件: | 文件 | 大小 | 说明 | |------|------|------| | `S7.Net.dll` | 110 KB | 主库(含二开新增的 WriteSafe 接口) | | `S7.Net.xml` | 125 KB | XML 文档(含中文注释) | | `S7.Net.pdb` | 35 KB | 调试符号 | | `System.Memory.dll` | 142 KB | 依赖(Span/Memory 支持) | | `System.Buffers.dll` | 21 KB | 依赖 | | `System.Numerics.Vectors.dll` | 116 KB | 依赖 | | `System.Runtime.CompilerServices.Unsafe.dll` | 17 KB | 依赖 | ### 验证结果 通过反射验证 DLL: - **目标框架**: `.NETFramework,Version=v4.8` ✓ - **公开类型数**: 46 个(含新增 `WriteResult`、`WriteStatusCode`)✓ - **WriteSafe 方法**: `WriteSafe(string, object)` + `WriteSafeAsync(string, object, CancellationToken)` ✓ 在 .NET Framework 4.8 项目中引用 `Release\NET48\S7.Net.dll`(连同同目录下的 4 个 `System.*.dll` 依赖)即可使用二开新增的安全写入接口。