PlcLibrary 1.0.0
See the version list below for details.
dotnet add package PlcLibrary --version 1.0.0
NuGet\Install-Package PlcLibrary -Version 1.0.0
<PackageReference Include="PlcLibrary" Version="1.0.0" />
<PackageVersion Include="PlcLibrary" Version="1.0.0" />
<PackageReference Include="PlcLibrary" />
paket add PlcLibrary --version 1.0.0
#r "nuget: PlcLibrary, 1.0.0"
#:package PlcLibrary@1.0.0
#addin nuget:?package=PlcLibrary&version=1.0.0
#tool nuget:?package=PlcLibrary&version=1.0.0
PlcLibrary
PLC 数据采集库,提供连接池管理、定时采集调度、数据分发管道。
- 协议无关驱动接口,一行注册新协议
- 连接池 + Polly 弹性策略(重试、超时、断路器),每设备独立隔离
- 设备配置热更新,差量 reconcile
- Channel 管道 fan-out 到多个
IDataHandler - 主动读写 + 自动采集双模式
安装
dotnet add package PlcLibrary
S7 驱动额外引用:
dotnet add package PlcLibrary.S7
快速开始
using Microsoft.Extensions.Logging;
using PlcLibrary.Controller.Interfaces;
using PlcLibrary.DriverDomain.Models;
using PlcLibrary.Extensions;
using PlcLibrary.General.Configuration;
using PlcLibrary.Pipeline.Interfaces;
using PlcLibrary.S7;
var builder = Host.CreateApplicationBuilder(args);
var devices = new[]
{
new DeviceConfiguration
{
Id = "plc-001",
Name = "1号线",
Protocol = "S7",
ConnectionString = "host:10.38.103.107;port:102;timeout:3000;rack:0;slot:0;cpu:S71200;",
TagPoints = new[]
{
new TagPointConfiguration { TagId = "t1", Address = "DB21.DBX10.2", DataType = "System.Boolean" },
new TagPointConfiguration { TagId = "t2", Address = "DB21.DBX10.0", DataType = "System.Boolean" },
},
CollectionInterval = TimeSpan.FromSeconds(1),
},
};
builder.Services
.AddPlcLibrary()
.AddDriver<S7Driver>("S7")
.AddSingleton<IDataHandler, ConsoleHandler>();
var host = builder.Build();
await host.Services.GetRequiredService<ITaskScheduler>().ApplyDevicesAsync(devices);
await host.RunAsync();
internal sealed class ConsoleHandler(ILogger<ConsoleHandler> logger) : IDataHandler
{
public ValueTask HandleAsync(DriverResult result, CancellationToken ct)
{
logger.LogInformation("[{DeviceId}] {Address} = {Value} ({Status})",
result.DeviceId, result.Address, result.Value, result.Status);
return ValueTask.CompletedTask;
}
}
设备多时可用
BackgroundService+IConfiguration从 appsettings.json 读取,参考下文 JSON 配置方式。
使用 JSON 配置
// 绑定 Options 到 appsettings.json
builder.Services.AddPlcLibrary();
builder.Services.Configure<PoolOptions>(builder.Configuration.GetSection("DriverPool"));
builder.Services.Configure<PipelineOptions>(builder.Configuration.GetSection("Pipeline"));
{
"Devices": [
{
"Enabled": true,
"Id": "plc-01",
"Protocol": "S7",
"ConnectionString": "host:192.168.1.1;port:102;rack:0;slot:1;cpu:S71200",
"CollectionInterval": "00:00:01",
"TagPoints": [
{ "TagId": "temp", "Address": "DB1.DBD0", "DataType": "Real" },
{ "TagId": "pressure", "Address": "DB1.DBD4", "DataType": "Real" }
]
}
]
}
internal sealed class DeviceLoader(IConfiguration config, ITaskScheduler scheduler) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken ct)
{
var devices = config.GetSection("Devices").Get<DeviceConfiguration[]>();
if (devices is { Length: > 0 })
await scheduler.ApplyDevicesAsync(devices, ct);
}
}
连接字符串
格式 key:value;key:value,大小写不敏感。
S7
| 字段 | 默认值 | 说明 |
|---|---|---|
| host | 127.0.0.1 | PLC 地址 |
| port | 102 | 端口 |
| rack | 0 | 机架 |
| slot | 0 | 插槽 |
| timeout | 3000 | 超时 (ms) |
| cpu | S71200 | CpuType |
示例:host:192.168.1.1;port:102;rack:0;slot:1;cpu:S71500
配置
驱动池
{
"DriverPool": {
"MaxConnectionsPerDevice": 2,
"MaxRetryAttempts": 3,
"RetryDelay": "00:00:01",
"CircuitBreakerMinimumThroughput": 5,
"CircuitBreakerDuration": "00:00:30",
"OperationTimeout": "00:00:10"
}
}
管道
{
"Pipeline": {
"Capacity": 10000,
"MaxHandlerParallelism": 4,
"HandlerTimeout": "00:00:30"
}
}
API
核心接口
| 接口 | 说明 |
|---|---|
ITaskScheduler |
推送设备配置,差量 reconcile |
IDataHandler |
接收采集推送 |
IDeviceAccessor |
主动读写设备 |
IProtocolDriver |
协议驱动实现 |
IDriverFactory |
驱动工厂(通常用 AddDriver<T> 替代) |
IDataPipeline |
数据管道(通常不需要直接使用) |
主动读写
public class MyService(IDeviceAccessor accessor)
{
public async Task ReadDevice(DeviceConfiguration device)
{
var values = await accessor.ReadAsync(device, device.TagPoints);
}
public async Task WriteDevice(DeviceConfiguration device)
{
var points = new Dictionary<TagPointConfiguration, object>
{
[device.TagPoints[0]] = 123.45
};
await accessor.WriteAsync(device, points);
}
}
自定义驱动
实现 IProtocolDriver、IDisposable、IAsyncDisposable:
public sealed class ModbusDriver : IProtocolDriver, IDisposable, IAsyncDisposable
{
public DriverStatus DriverStatus { get; private set; }
public Task ConnectAsync(CancellationToken ct = default) { ... }
public Task DisconnectAsync(CancellationToken ct = default) { ... }
public Task<bool> TryReconnectAsync(CancellationToken ct = default) { ... }
public Task<DriverResult[]> ReadAsync(TagPointConfiguration[] points, CancellationToken ct = default) { ... }
public Task<DriverResult[]> WriteAsync(IReadOnlyDictionary<TagPointConfiguration, object> values, CancellationToken ct = default) { ... }
public void Dispose() { ... }
public ValueTask DisposeAsync() { ... }
}
注册:
services.AddDriver<ModbusDriver>("Modbus");
如果连接 Key 需要自定义(同 IP 不同端口共用池):
services.AddDriver<ModbusDriver>("Modbus", cs => {
var c = ModbusConfig.Parse(cs);
return $"{c.Host}:{c.Port}";
});
许可证
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Bcl.AsyncInterfaces (>= 10.0.9)
- Microsoft.Extensions.Hosting (>= 10.0.9)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.9)
- Polly (>= 8.7.0)
- System.Threading.Channels (>= 10.0.9)
NuGet packages (8)
Showing the top 5 NuGet packages that depend on PlcLibrary:
| Package | Downloads |
|---|---|
|
PlcLibrary.Modbus
工业 PLC 数据采集基础库,支持 Modbus/S7/OPC UA 协议,含连接池、重试熔断、数据管道。 |
|
|
PlcLibrary.S7
工业 PLC 数据采集基础库,支持 Modbus/S7/OPC UA 协议,含连接池、重试熔断、数据管道。 |
|
|
PlcLibrary.Mitsubishi
工业 PLC 数据采集基础库,支持 Modbus/S7/OPC UA 协议,含连接池、重试熔断、数据管道。 |
|
|
PlcLibrary.OpcUa
工业 PLC 数据采集基础库,支持 Modbus/S7/OPC UA 协议,含连接池、重试熔断、数据管道。 |
|
|
PlcLibrary.AllenBradley
工业 PLC 数据采集基础库,支持 Modbus/S7/OPC UA 协议,含连接池、重试熔断、数据管道。 |
GitHub repositories
This package is not used by any popular GitHub repositories.