mirror of
https://github.com/RRQM/TouchSocket.git
synced 2025-12-21 02:46:43 +08:00
添加modbus示例
This commit is contained in:
@@ -248,11 +248,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RealityProxyDmtpRpcConsoleA
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Hosting", "Hosting", "{0E116F60-01DE-4648-B4EE-5B944EED42AA}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostingWorkerService", "Hosting\HostingWorkerService\HostingWorkerService.csproj", "{866798F5-FD69-49D2-B4FA-279DC7188A54}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HostingWorkerService", "Hosting\HostingWorkerService\HostingWorkerService.csproj", "{866798F5-FD69-49D2-B4FA-279DC7188A54}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Serial", "Serial", "{8911AC2E-B951-436C-A76D-7E994017069B}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SerialPortClientConsoleApp", "Serial\SerialPortClientConsoleApp\SerialPortClientConsoleApp.csproj", "{EBB7E090-7309-49D6-A0C9-CD704E7FEA31}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialPortClientConsoleApp", "Serial\SerialPortClientConsoleApp\SerialPortClientConsoleApp.csproj", "{EBB7E090-7309-49D6-A0C9-CD704E7FEA31}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modbus", "Modbus", "{0F95EA5A-0E08-4516-89D4-C3BFC88789F9}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModbusClientConsoleApp", "Modbus\ModbusClientConsoleApp\ModbusClientConsoleApp.csproj", "{999A4AD8-D8C7-411E-9F9D-766B6BBB110C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -560,6 +564,10 @@ Global
|
||||
{EBB7E090-7309-49D6-A0C9-CD704E7FEA31}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EBB7E090-7309-49D6-A0C9-CD704E7FEA31}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EBB7E090-7309-49D6-A0C9-CD704E7FEA31}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{999A4AD8-D8C7-411E-9F9D-766B6BBB110C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{999A4AD8-D8C7-411E-9F9D-766B6BBB110C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{999A4AD8-D8C7-411E-9F9D-766B6BBB110C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{999A4AD8-D8C7-411E-9F9D-766B6BBB110C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -670,6 +678,7 @@ Global
|
||||
{C94B9E69-3B48-4AAB-B3C3-80AA190640C2} = {995183D4-74FD-4EDD-8FDF-8B6BC0410A06}
|
||||
{866798F5-FD69-49D2-B4FA-279DC7188A54} = {0E116F60-01DE-4648-B4EE-5B944EED42AA}
|
||||
{EBB7E090-7309-49D6-A0C9-CD704E7FEA31} = {8911AC2E-B951-436C-A76D-7E994017069B}
|
||||
{999A4AD8-D8C7-411E-9F9D-766B6BBB110C} = {0F95EA5A-0E08-4516-89D4-C3BFC88789F9}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {DB787235-A13A-4A3D-B5A8-5DFEB6511EEE}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="TouchSocket.Modbus" Version="2.0.0-beta.245" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
10
examples/Modbus/ModbusClientConsoleApp/Program.cs
Normal file
10
examples/Modbus/ModbusClientConsoleApp/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace ModbusClientConsoleApp
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
}
|
||||
221
handbook/docs/modbusclient.mdx
Normal file
221
handbook/docs/modbusclient.mdx
Normal file
@@ -0,0 +1,221 @@
|
||||
---
|
||||
id: modbusclient
|
||||
title: Modbus客户端
|
||||
---
|
||||
|
||||
import Tag from "@site/src/components/Tag.js";
|
||||
|
||||
### 定义
|
||||
|
||||
命名空间:TouchSocket.Modbus <br/>
|
||||
程序集:[TouchSocket.Modbus.dll](https://www.nuget.org/packages/TouchSocket.Modbus)
|
||||
|
||||
|
||||
## 一、说明
|
||||
|
||||
Modbus是OSI模型第7层上的应用层报文传输协议,它在连接至不同类型总线或网络的设备之间提供客户机/服务器通信。
|
||||
|
||||
自从 1979 年出现工业串行链路的事实标准以来,Modbus使成千上万的自动化设备能够通信。目前,继续增加对简单而雅观的Modbus结构支持。互联网组织能够使TCP/IP栈上的保留系统端口502 访问Modbus。
|
||||
|
||||
所以总结来说Modbus是一个请求/应答的总线协议。
|
||||
|
||||
所以我们开发了这个组件,方便大家使用。
|
||||
|
||||
## 二、特点
|
||||
|
||||
- 简单易用。
|
||||
- 内存池支持
|
||||
- 高性能
|
||||
- 易扩展。
|
||||
- **支持全数据类型的读写**。
|
||||
|
||||
## 三、产品应用场景
|
||||
|
||||
- 所有Modbus使用场景:可跨平台使用。
|
||||
|
||||
## 四、可配置项
|
||||
|
||||
无单独配置项。
|
||||
|
||||
## 五、支持插件
|
||||
|
||||
无单独支持插件。
|
||||
|
||||
## 六、创建
|
||||
|
||||
目前TouchSokcet.Modbus支持Tcp、Udp、Rtu、RtuOverTcp、RtuOverUdp等协议。下面会一一介绍创建过程。
|
||||
|
||||
#### 6.1 创建ModbusTcpClient
|
||||
|
||||
```csharp showLineNumbers
|
||||
public IModbusTcpClient GetClient()
|
||||
{
|
||||
var client = new ModbusTcpClient();
|
||||
|
||||
client.Connect("127.0.0.1:502");
|
||||
return client;
|
||||
}
|
||||
```
|
||||
|
||||
#### 6.2 创建ModbusUdpClient
|
||||
|
||||
```csharp showLineNumbers
|
||||
public IModbusClient GetClient()
|
||||
{
|
||||
var client = new ModbusUdpClient();
|
||||
client.Setup(new TouchSocketConfig()
|
||||
.UseUdpReceive()
|
||||
.SetRemoteIPHost("127.0.0.1:502"));
|
||||
client.Start();
|
||||
return client;
|
||||
}
|
||||
```
|
||||
|
||||
#### 6.3 创建ModbusRtuClient
|
||||
|
||||
```csharp showLineNumbers
|
||||
public IModbusClient GetClient()
|
||||
{
|
||||
var client = new ModbusRtuClient();
|
||||
client.Setup(new TouchSocketConfig()
|
||||
.SetSerialPortOption(new SerialPortOption()
|
||||
{
|
||||
BaudRate = 9600,
|
||||
DataBits = 8,
|
||||
Parity = System.IO.Ports.Parity.Even,
|
||||
PortName = "COM2",
|
||||
StopBits = System.IO.Ports.StopBits.One
|
||||
}));
|
||||
client.Connect();
|
||||
return client;
|
||||
}
|
||||
```
|
||||
|
||||
#### 6.4 创建ModbusRtuOverTcpClient
|
||||
|
||||
```csharp showLineNumbers
|
||||
public IModbusClient GetClient()
|
||||
{
|
||||
var client = new ModbusRtuOverTcpClient();
|
||||
client.Connect("127.0.0.1:502");
|
||||
return client;
|
||||
}
|
||||
```
|
||||
|
||||
#### 6.5 创建ModbusRtuOverUdpClient
|
||||
|
||||
```csharp showLineNumbers
|
||||
public IModbusClient GetClient()
|
||||
{
|
||||
var client = new ModbusRtuOverUdpClient();
|
||||
client.Setup(new TouchSocketConfig()
|
||||
.UseUdpReceive()
|
||||
.SetRemoteIPHost("127.0.0.1:502"));
|
||||
client.Start();
|
||||
return client;
|
||||
}
|
||||
```
|
||||
|
||||
## 七、读写操作
|
||||
|
||||
### 7.1 原生接口操作
|
||||
|
||||
所有的Modbus客户端都支持以下两种原生接口操作:
|
||||
|
||||
```csharp showLineNumbers
|
||||
//同步发送Modbus请求,并等待响应
|
||||
IModbusResponse SendModbusRequest(ModbusRequest request, int timeout, CancellationToken token);
|
||||
|
||||
//异步发送Modbus请求,并等待响应
|
||||
Task<IModbusResponse> SendModbusRequestAsync(ModbusRequest request, int timeout, CancellationToken token);
|
||||
```
|
||||
|
||||
以读线圈操作为例:
|
||||
|
||||
```csharp showLineNumbers
|
||||
ModbusRequest modbusRequest = new ModbusRequest(FunctionCode.ReadCoils);
|
||||
modbusRequest.SetSlaveId(1);//设置站号。如果是Tcp可以不设置
|
||||
modbusRequest.SetStartingAddress(0);//设置起始
|
||||
modbusRequest.SetQuantity(1);//设置数量
|
||||
//modbusRequest.SetValue(false);//如果是写入类操作,可以直接设定值
|
||||
|
||||
var response = client.SendModbusRequest(modbusRequest, 1000, CancellationToken.None);
|
||||
|
||||
bool[] bools = response.CreateReader().ToBoolensFromBit().ToArray();
|
||||
```
|
||||
|
||||
### 7.2 快捷扩展实现
|
||||
|
||||
因为Modbus的操作一般比较固化,所以ModbusClient扩展了以下快捷操作:
|
||||
|
||||
读取线圈(FC1)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
bool[] bools = client.ReadCoils(0, 1);
|
||||
```
|
||||
|
||||
读取离散输入(FC2)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
bool[] bools = client.ReadDiscreteInputs(0, 1);
|
||||
```
|
||||
|
||||
读取保持寄存器(FC3)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
var response = client.ReadHoldingRegisters(0, 1);
|
||||
var reader = response.CreateReader();
|
||||
var value=reader.ReadInt16();
|
||||
```
|
||||
|
||||
读取输入寄存器(FC4)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
var response = client.ReadInputRegisters(0, 1);
|
||||
var reader = response.CreateReader();
|
||||
var value=reader.ReadInt16();
|
||||
```
|
||||
|
||||
写入单个线圈(FC5)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
client.WriteSingleCoil(0, true);
|
||||
```
|
||||
|
||||
写入单个寄存器(FC6)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
client.WriteSingleRegister(0, (short)100);
|
||||
```
|
||||
|
||||
写入多个线圈(FC15)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
client.WriteMultipleCoils(0, new bool[] { true, false, true });
|
||||
```
|
||||
|
||||
写入多个寄存器(FC16)。
|
||||
|
||||
```csharp showLineNumbers
|
||||
using (var writer = new ValueByteBlock(1024))
|
||||
{
|
||||
writer.Write(short.MaxValue, EndianType.Big);//ABCD端序 2字节
|
||||
writer.Write(int.MaxValue, EndianType.Little);//DCBA端序 4字节
|
||||
writer.Write(long.MaxValue, EndianType.BigSwap);//BADC端序 8字节
|
||||
writer.Write(double.MaxValue, EndianType.LittleSwap);//CDAB端序 8字节
|
||||
|
||||
client.WriteMultipleRegisters(0, writer.ToArray());
|
||||
}
|
||||
```
|
||||
|
||||
:::tip 提示
|
||||
|
||||
以上扩展方法还有更多重载。例如:站号、超时时间、可取消令箭等参数。
|
||||
|
||||
:::
|
||||
|
||||
## 八、更多写入与读取
|
||||
|
||||
|
||||
[本文示例Demo](https://gitee.com/RRQM_Home/TouchSocket/tree/master/examples/Tcp)
|
||||
|
||||
@@ -634,11 +634,15 @@ module.exports = {
|
||||
type: "doc",
|
||||
label: "18、XmlRpc组件",
|
||||
id: "xmlrpc"
|
||||
}
|
||||
,
|
||||
},
|
||||
{
|
||||
type: "doc",
|
||||
label: "19、通用主机(Hosting)",
|
||||
label: "19、Modbus客户端",
|
||||
id: "modbusclient"
|
||||
},
|
||||
{
|
||||
type: "doc",
|
||||
label: "20、通用主机(Hosting)",
|
||||
id: "generichost"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user