MiyaModbus.Core
1.1.2
dotnet add package MiyaModbus.Core --version 1.1.2
NuGet\Install-Package MiyaModbus.Core -Version 1.1.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="MiyaModbus.Core" Version="1.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MiyaModbus.Core" Version="1.1.2" />
<PackageReference Include="MiyaModbus.Core" />
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 MiyaModbus.Core --version 1.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MiyaModbus.Core, 1.1.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 MiyaModbus.Core@1.1.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=MiyaModbus.Core&version=1.1.2
#tool nuget:?package=MiyaModbus.Core&version=1.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MiyaModbus
安装
Install-Package MiyaModbus.Core -Version 1.0.9
简易使用方法
创建设备
- 创建ModbusTcp设备
DeviceFactory.CreateModbusTcpDevice(1, "127.0.0.1", 502);
- 创建ModbusRtu设备
DeviceFactory.CreateModbusRtuDevice(0x01, "COM1", 9600, 8, StopBits.One, Parity.None);
- 创建使用TCP协议的ModbusRtu设备
DeviceFactory.CreateModbusRtuOverTcpDevice(1, "127.0.0.1", 502);
- 创建使用串口的文本通讯设备
DeviceFactory.CreateModbusRtuOverTcpDevice("COM1", 9600, 8, StopBits.One, Parity.None);
还有其他类似的设备创建方式 也可以直接指定设备类型、通道和网络来创建设备,这种创建方式 自由度更改,可以任意组合。
读写数据
//创建设备
var device = DeviceFactory.CreateModbusRtuOverTcpDevice(1, "127.0.0.1", 502);
//启动设备
await device.StartAsync();
//写入短整型
await device.WriteShortAsync(0, d);
//读取短整型
var v = await device.ReadShortAsync(0);
//写入浮点数
await device.WriteFloatAsync(0, f);
//读取浮点数
var v = await device.ReadFloatAsync(0);
//写入无符号整数
await device.WriteUIntAsync(0, d);
//读取无符号整数
var v = await device.ReadUIntAsync(0);
//停止设备
await device.StopAsync();
高级用法
1. 更改字节序
当前很多不同的PLC使用不同的字节序,MiyaModbus支持使用简单的设置的方式更改设备读写的字节序 以适应不同的设备
//短整型是否反序
device.Options.ShortReverse = true;
//浮点型数据字节序
device.Options.FloatOrder = ByteOrder.ABCD;
//双精度浮点型字节序
device.Options.DoubleOrder = LongByteOrder.ABCDEFGH;
//整型字节序
device.Options.IntOrder = ByteOrder.ABCD;
//长整型字节序
device.Options.LongOrder = LongByteOrder.ABCDEFGH;
2. 多个设备使用同一个网络通道
如果在一条串口线上连接了多个ModbusRtu设备,每个设备使用不同的站号来区分,那么 我们可以创建多个设备使用同一个网络,不同设备发送的数据在网络通道内会自动排序, 并且线程安全,但是多个设备使用同一个网络通道会导致通信速率下降。
var network = new SerialNetwork("COM1", 9600, 8, StopBits.One, Parity.None);
var channel = new DefaultChannel(network);
var device1 = new ModbusRtuDevice(channel,options=>
{
options.AddParams("stationid", 0x01);
});
var device2 = new ModbusRtuDevice(channel,options=>
{
options.AddParams("stationid", 0x02);
});
仅在使用DefaultChannel的情况下 此时deivce1和device2发送的数据都会经过同一个通道,并在通道内自动 按顺序发送并获取返回结果,两者互不干扰。 如果使用DirectChannel的情况下 两者之间是可能会互相干扰的,但是通讯速度更快,是DefaultChannel的 三倍以上。
| 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
- System.IO.Ports (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
将数据点位类型从short改为ushort以能读取更加多的范围