84 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Modbus TCP 客户端模块
基于libmodbus库的Modbus TCP客户端实现提供完整的Modbus功能支持。
## 功能特性
- 基于libmodbus的稳定实现
- 支持所有标准Modbus功能码
- 线程安全的连接管理
- 数据类型转换工具32位整数、浮点数
- 完整的错误处理和状态报告
- 跨平台支持Windows/Linux
## 支持的Modbus功能
- 读取线圈0x01
- 读取离散输入0x02
- 读取保持寄存器0x03
- 读取输入寄存器0x04
- 写单个线圈0x05
- 写单个保持寄存器0x06
- 写多个线圈0x0F
- 写多个保持寄存器0x10
## API接口
### 基本操作
```cpp
ModbusTCPClient client("192.168.1.100", 502);
if (client.connect()) {
// 连接成功
}
```
### 读操作
```cpp
std::vector<bool> coils;
client.readCoils(0, 10, coils);
std::vector<uint16_t> registers;
client.readHoldingRegisters(0, 10, registers);
```
### 写操作
```cpp
client.writeSingleCoil(0, true);
client.writeSingleRegister(0, 1234);
std::vector<bool> coils = {true, false, true};
client.writeMultipleCoils(0, coils);
std::vector<uint16_t> registers = {1234, 5678};
client.writeMultipleRegisters(0, registers);
```
## 编译说明
### 依赖
- Qt 5.x 或更高版本
- C++17 编译器
- libmodbus源码已包含在VrNets模块中
### 构建
```bash
qmake ModbusTCPClient.pro
make
```
### 使用
该模块编译为静态库,在其他项目中包含:
```pro
LIBS += -L../Module/ModbusTCPClient -lModbusTCPClient
INCLUDEPATH += ../Module/ModbusTCPClient/Inc
```
## 错误处理
所有API调用返回bool值指示操作是否成功可通过getLastError()获取详细错误信息:
```cpp
if (!client.readCoils(0, 10, coils)) {
qDebug() << "Error:" << client.getLastError();
}
```