PlcSimulator.Core 1.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package PlcSimulator.Core --version 1.0.2
                    
NuGet\Install-Package PlcSimulator.Core -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PlcSimulator.Core" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PlcSimulator.Core" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="PlcSimulator.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PlcSimulator.Core --version 1.0.2
                    
#r "nuget: PlcSimulator.Core, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PlcSimulator.Core@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PlcSimulator.Core&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=PlcSimulator.Core&version=1.0.2
                    
Install as a Cake Tool

PLC Simulator - 工业级全协议仿真器

这是一个基于 .NET 8 开发的高性能 PLC 仿真服务器,旨在为工业自动化领域的软件开发、测试和教学提供一个轻量级、跨平台的仿真环境(完美支持 MacOS)。

🌟 核心特性

  • 全协议支持:内置西门子 (S7)、三菱 (MC/A-1E)、欧姆龙 (FINS)、罗克韦尔 (CIP)、Modbus TCP 及 BACnet/IP 仿真。
  • 高性能架构:单核支持高达 10,000+ RPS 的请求处理能力。
  • 统一内存模型:所有协议共享同一块 64KB x N 的寄存器空间。支持协议间数据透明互通。
  • 生产级兼容性:支持西门子 S7-200 SMART/1200/1500 模型,无 DB 块编号 10000 上限限制。
  • 跨平台运行:原生支持 MacOS、Linux 和 Windows。

🛡️ 安全特性

  • 报文校验: 所有协议服务器均校验报文长度和 count 上限(MC/FINS 最大 2048 点),防止截断攻击和 DoS。
  • 全局异常处理: CLI/UI 均配置 UnhandledException 处理,防止静默崩溃。
  • 敏感信息保护: .qwen/ 目录已加入 .gitignore,防止 API Key 泄漏。

🧪 测试覆盖

  • 测试框架: xUnit + FluentAssertions
  • 测试数量: 505 个正式测试(382 Core + 123 Gateway)+ 20 个协议边界测试
  • 测试策略: 通过反射调用 private 方法,无网络依赖,109ms 完成
  • 覆盖率: 核心协议报文解析边界校验 100%

运行测试:

# 单元测试(无网络依赖)
dotnet test src/PlcSimulator.Test/PlcSimulator.Test.csproj \
  --filter "FullyQualifiedName~PlcAddressHelper|FullyQualifiedName~PlcMemory|FullyQualifiedName~S7ServerTests|FullyQualifiedName~ModbusAddressResolver|FullyQualifiedName~MelsecMcServer|FullyQualifiedName~OmronFinsServer"

# 集成测试(需要网络)
dotnet test src/PlcSimulator.Test/PlcSimulator.Test.csproj

📐 工程化规范

  • 代码风格: .editorconfig 统一 C# 命名约定、缩进、换行、模式匹配偏好
  • SDK 版本: global.json 锁定 .NET 10.0,防止版本漂移
  • 构建属性: Directory.Build.props 统一 Nullable/ImplicitUsings/版本号

📁 项目结构

  • src/PlcSimulator.Core: 核心逻辑、统一内存模型 (PlcMemory) 和服务器接口。
  • src/PlcSimulator.Protocols: 各类工业协议的具体实现。
  • src/PlcSimulator.Cli: 控制台交互式启动程序。
  • src/PlcSimulator.Test: 基于 HslCommunication 的全自动化测试套件。

🚀 快速开始

1. 编译项目

使用 .NET 8 SDK 进行编译:

dotnet build src/PlcSimulator.sln

1.1 UI Linux 桌面支持开关(重要)

src/PlcSimulator.UI/PlcSimulator.UI.csproj 增加了 EnableLinuxDesktopSupport 开关:

  1. 默认值:false
    • 默认正式版构建不引入 Linux X11 桌面链路(避免额外 DBus/X11 依赖风险)。
  2. 需要 Linux 桌面运行时,显式开启:
dotnet build src/PlcSimulator.UI/PlcSimulator.UI.csproj -v minimal /p:EnableLinuxDesktopSupport=true
  1. 运行时行为:
    • macOS:UseAvaloniaNative()
    • Windows:UseWin32()
    • Linux:仅在启用开关的构建下走 UseX11()

2. 启动仿真服务器

dotnet run --project src/PlcSimulator.Cli/PlcSimulator.Cli.csproj

启动后在 PlcSim> 提示符下输入 start 即可开启所有协议监听。

3. 运行自动化测试

我们提供了基于 HslCommunication 的压力与兼容性测试:

dotnet run --project src/PlcSimulator.Test/PlcSimulator.Test.csproj

🛠 协议与端口映射 (MacOS 兼容)

协议 端口 内存映射地址示例
西门子 S7 102 DB1.0, M100, I0, Q0
三菱 MC 3E 5000 D100, M0
三菱 MC 1E 5001 D0 (旧款 A 系列)
欧姆龙 FINS 9600 D100 (映射至 DB1)
Modbus TCP 502 00001, 10001, 30001, 40001
BACnet/IP 47808 AnalogValue:1 (映射至 DB1 偏移 4)
Allen-Bradley EtherNet/IP 44818 TAGA / CIP 映射

Modbus 地址区支持说明

当前 Modbus TCP 仿真已支持标准四大地址区:

地址区 功能码 说明 内部映射
0xxxx 01 / 05 / 15 Coil,支持读/写 DB3 偏移 0 起的位区
1xxxx 02 Discrete Input,只读 DB3 偏移 1024 起的位区
3xxxx 04 Input Register,只读 DB2 偏移 0
4xxxx 03 / 06 / 16 Holding Register,支持读/写 DB1 偏移 0

说明:

  • 3000140001 是相互独立的两块寄存器区,不共享同一内存。
  • 0000110001 也是独立位区,不共享同一字节偏移。
  • UI/工作台地址输入也支持 HR1 / IR1 / COIL1 / DI1 这类前缀形式,便于和不同主站软件的习惯保持一致。
  • 若使用低位端口 502,在 macOS/Linux 上仍需管理员权限或改用高位端口。

注意:在 MacOS/Linux 上,102/502 属于特权端口。无管理员权限时请使用 sudo,或通过 CLI 参数改用高位端口(如 --s7-port 1102 --modbus-port 1502;如需多实例也可使用 --melsec-port/--melsec-a1e-port/--omron-port/--ab-port/--bacnet-port 覆写端口)。

🧱 扩展新协议

由于采用了高度抽象的 IPlcServer 接口和统一的 PlcMemory,扩展新协议非常简单:

  1. Protocols 项目下新建类并实现 IPlcServer
  2. Start() 中开启监听并解析报文。
  3. 通过 _memory.Read()_memory.Write() 与全局内存交互。
  4. Program.cs 中注册该服务器即可。

📚 补充文档

  • CLI 语义命令与 JSON 协议:Docs/PlcSimulator.Cli-语义命令与JSON协议.md
  • CLI-Anything 使用说明:agent-harness/cli_anything/plcsimulator/USAGE.zh-CN.md
  • AI 评选叙事建议:Docs/14-AI评选叙事与落地价值.md

坚持简洁、高效、稳定。本项目是从电检项目演化而来的全领域工控仿真工具。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 48 6/5/2026
1.0.2 60 6/5/2026
1.0.1 57 6/4/2026
1.0.0 57 6/4/2026