MasterServer.Lib.Plc.Modbus
1.0.7
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 MasterServer.Lib.Plc.Modbus --version 1.0.7
NuGet\Install-Package MasterServer.Lib.Plc.Modbus -Version 1.0.7
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="MasterServer.Lib.Plc.Modbus" Version="1.0.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasterServer.Lib.Plc.Modbus" Version="1.0.7" />
<PackageReference Include="MasterServer.Lib.Plc.Modbus" />
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 MasterServer.Lib.Plc.Modbus --version 1.0.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MasterServer.Lib.Plc.Modbus, 1.0.7"
#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 MasterServer.Lib.Plc.Modbus@1.0.7
#: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=MasterServer.Lib.Plc.Modbus&version=1.0.7
#tool nuget:?package=MasterServer.Lib.Plc.Modbus&version=1.0.7
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Automation.Lib.Plc.Modbus
Modbus TCP通信库,基于NModbus实现与Modbus设备的通信功能。 作者邮箱:dengjianhua1999@qq.com
特性
- 支持Modbus TCP协议
- 基于NModbus库的稳定实现
- 支持线圈、离散输入、输入寄存器、保持寄存器
- 支持单个和批量读写操作
- 自动连接管理和重连
- 集成日志记录
- 支持多种数据类型转换
支持的数据类型
- 布尔型 (bool): 线圈和离散输入
- 短整型 (short): 16位有符号整数
- 无符号短整型 (ushort): 16位无符号整数
- 整型 (int): 32位有符号整数
- 无符号整型 (uint): 32位无符号整数
- 单精度浮点 (float): 32位浮点数
- 双精度浮点 (double): 64位浮点数
- 字符串 (string): 字符串数据
核心组件
1. ModbusConnectInfo
Modbus连接信息类,包含:
- Host: Modbus设备IP地址
- Port: 端口号(默认502)
- SlaveId: 从站ID
- TimeOut: 超时时间
- ReConnectSeconds: 重连间隔
2. ModbusItem<T>
单个数据项操作类,支持:
- 读取指定地址的数据
- 写入指定地址的数据
- 地址格式:功能码.地址(如1.00001表示线圈1)
3. ModbusArray<T>
数组数据操作类,支持:
- 批量读取连续地址的数据
- 批量写入连续地址的数据
- 数据变化检测
使用方式
与MasterServer.Lib.Plc使用方式基本相同。(所有MasterServer.Lib.Plc的子项目,使用方式都与Plc项目基本相同)。
1. 定义PLC配置(案例)
与MasterServer.Lib.Plc使用方式完全相同。
using Automation.Lib.Plc;
public class PlcConfig : BasePlcConfig
{
/// <summary>
/// 心跳
/// </summary>
[XmlElement]
public DataItem<short> Heartbeat { get; set; }
//**************************下面是单个数**********************************//
[XmlElement]
public DataItem<short> TestShort { get; set; }
[XmlElement]
public DataItem<float> TestFloat { get; set; }
[XmlElement]
public DataItem<bool> TestBool { get; set; }
//********************************下面是数组******************************//
[XmlElement]
public ArrayNode<short> TestShortArray { get; set; }
[XmlElement]
public ArrayNode<float> TestFloatArray { get; set; }
[XmlElement]
public ArrayNode<float> TestFloatArray2 { get; set; }
[XmlElement]
public ArrayNode<bool> TestBoolArray { get; set; }
[XmlElement]
public DataItem<string> TestString { get; set; }
2.连接和操作
与MasterServer.Lib.Plc使用方式略有不同。Installer.Install("Modbus");参数修改为Modbus,其他完全相同。
string ConfigDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config");
Installer.Install("Modbus"); //这里改成了Modbus
XmlSerializer ser = new XmlSerializer(typeof(PlcConfig), Installer.ExtraXmlTypes.ToArray());
using (var reader = new StreamReader(Path.Combine(ConfigDir, "PlcConfig.xml")))
{
string xml = reader.ReadToEnd();
PlcConfig = Serializer.FromXml<PlcConfig>(xml, ser);
}
PlcConfig.InitializeArrayNodes();
// 写入数据
PlcConfig.TestShort.Write(123);
// 订阅数据变化
plcConfig.TestShortArray.StartPolling(TimeSpan.FromMilliseconds(200));//开始轮询
plcConfig.TestShortArray.DataChanged.Subscribe(newValue =>
{
Console.WriteLine($"数据变化: {newValue}");
});
3. 数组操作
与MasterServer.Lib.Plc使用方式完全相同。
// 读取数组
float[] values = PlcConfig.MyArray.Read();
// 写入数组(参数也可以用List<T>)
PlcConfig.MyArray.Write(new float[] { 1.0f, 2.0f, 3.0f });
4. xml样例
与MasterServer.Lib.Plc使用方式略有不同。
- 把所有的
xsi:type按下面的方式进行修改 - Connection 下面的方式进行修改
<?xml version="1.0" encoding="utf-8"?>
<PlcConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Connection xsi:type="ModbusConnectInfo">
<Id>plc1</Id>
<Address>127.0.0.1</Address>
<SlaveId>1</SlaveId>
<Port>502</Port>
<Endian>0</Endian>
</Connection>
<Heartbeat xsi:type="ModbusItemOfInt16" StartAddress="40" />
<TestShort xsi:type="ModbusItemOfInt16" StartAddress="1" />
<TestFloat xsi:type="ModbusItemOfSingle" StartAddress="2" />
<TestBool xsi:type="ModbusItemOfBoolean" StartAddress="4" />
<TestShortArray xsi:type="ModbusArrayOfInt16" StartAddress="5">
<DataItem xsi:type="ModbusItemOfInt16" Name="x" Index="0" />
<DataItem xsi:type="ModbusItemOfInt16" Name="y" Index="1" />
<DataItem xsi:type="ModbusItemOfInt16" Name="z" Index="2" />
</TestShortArray>
<TestFloatArray xsi:type="ModbusArrayOfSingle" StartAddress="8">
<DataItem xsi:type="ModbusItemOfSingle" Name="x" Index="0" />
<DataItem xsi:type="ModbusItemOfSingle" Name="y" Index="1" />
<DataItem xsi:type="ModbusItemOfSingle" Name="z" Index="2" />
</TestFloatArray>
<TestBoolArray xsi:type="ModbusArrayOfBoolean" StartAddress="16">
<DataItem xsi:type="ModbusItemOfBoolean" Name="x" Index="0" />
<DataItem xsi:type="ModbusItemOfBoolean" Name="y" Index="1" />
<DataItem xsi:type="ModbusItemOfBoolean" Name="z" Index="2" />
</TestBoolArray>
<TestString xsi:type="ModbusItemOfString" StartAddress="14" />
</PlcConfig>
安装
通过NuGet安装:
Install-Package Automation.Lib.Plc.Modbus
依赖
- NModbus
- Automation.Lib.Plc
- Automation.Lib.Logging</content>
| 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- MasterServer.Lib.Plc (>= 1.0.8)
- NModbus (>= 3.0.81)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MasterServer.Lib.Plc.Modbus:
| Package | Downloads |
|---|---|
|
MasterServer.Lib.Plc.ModbusRtu
命名空间和包名称统一,引用的包更新至最新稳定版 |
GitHub repositories
This package is not used by any popular GitHub repositories.