MAS.Communication
1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package MAS.Communication --version 1.0.2
NuGet\Install-Package MAS.Communication -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="MAS.Communication" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MAS.Communication" Version="1.0.2" />
<PackageReference Include="MAS.Communication" />
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 MAS.Communication --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MAS.Communication, 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 MAS.Communication@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=MAS.Communication&version=1.0.2
#tool nuget:?package=MAS.Communication&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MAS.Communication
✨ 项目特性
一个面向工业自动化场景的 多协议通信管理框架
- 多协议统一抽象:Modbus TCP、MC Protocol、等协议采用一致的调用模型
- 多实例管理:支持同协议多实例并存,适用于一机多 PLC / 多设备通信场景
- 统一生命周期控制:统一创建、复用、查询与释放通信实例,避免资源失控
- 依赖注入集成:基于
Microsoft.Extensions.DependencyInjection,便于工程化接入 - 易于扩展:可按统一规范扩展新的通信协议,而无需改动上层业务逻辑
- 面向工业项目落地:适用于 WPF、WinUI、后台服务等现代 .NET 应用
🎯 项目定位
MAS.Communication 是一个面向工业自动化的 .NET 通信框架,
用于在现代 .NET 应用中 统一接入、管理和复用多种工业通信协议实例
- 统一的通信抽象
- 多实例与多设备管理能力
- 一致的生命周期控制
- 面向依赖注入的工程化集成方式
适合构建中大型上位机系统中的通信基础设施层
⚠️ 使用前提
在使用本项目之前,请确认你的应用满足以下条件:
- 使用 现代 .NET 项目
- 目标框架为 .NET 8.0 及以上
- 使用 Microsoft.Extensions.DependencyInjection 进行服务注册与解析
- 不支持仍运行在 .NET Framework 上的项目
- 仅适合在 WPF / WinUI / Worker / ASP.NET Core 中集成
🚀 快速开始
注册服务
在应用启动阶段注册通信服务:
using Microsoft.Extensions.DependencyInjection;
using MAS.Communication;
IServiceCollection services = new ServiceCollection();
services.AddCommunication();
通信配置
在使用具体协议前,需要先提供对应的通信配置对象
框架已经为不同协议提供了统一的配置接口,调用方无需自行定义新的协议配置接口,只需要根据实际项目实现对应接口即可
例如:
- MC 协议使用:
IMcCommunicationConfig - Modbus 协议使用:
IModbusCommunicationConfig
以上接口均继承自 ICommunicationConfig,调用方实现对应接口后,即可将配置对象传入 IProtocolManager,用于创建或获取协议实例
使用方式
通信框架通过IProtocolManager提供统一入口,推荐通过依赖注入方式获取并使用:
public class MainViewModel {
private readonly IProtocolManager _protocolManager;
public MainViewModel(IProtocolManager protocolManager) {
_protocolManager = protocolManager;
}
public async Task RunAsync() {
// 创建配置(示例:Modbus)
IModbusCommunicationConfig config = new ModbusCommunicationConfig {
// 填写你的实际配置
// 例如:Ip、Port、StationNumber 等
};
// 获取或创建协议实例
IProtocol protocol = _protocolManager.GetOrCreate(config);
// 建立连接
await protocol.ConnectAsync();
// 示例:执行读写(具体方法见 API 文档)
// await protocol.ReadAsync(...);
// await protocol.WriteAsync(...);
}
}
- 所有协议实例生命周期由
IProtocolManager统一管理 - 相同配置会自动复用已有连接实例(基于配置唯一标识)
- 支持创建多个不同配置的协议实例(例如多个 PLC)
- 当不再需要使用某个实例时,请调用
IProtocolManager.Remove(IProtocol)或IProtocol.Dispose()方法释放资源 - 不建议多个线程高并发共享一个实例做高频通信,虽然目前来说它们暂时是线程安全的
- 有关更多信息,请参阅API 文档
| Product | Versions 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.
-
net8.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
一个面向工业自动化场景的多协议通信管理框架