本页内容

QModbusTcpServer Class

QModbusTcpServer 类表示一个 Modbus 服务器,该服务器使用 TCP 服务器与 Modbus 客户端进行通信。更多内容...

头文件: #include <QModbusTcpServer>
CMake: find_package(Qt6 REQUIRED COMPONENTS SerialBus)
target_link_libraries(mytarget PRIVATE Qt6::SerialBus)
qmake: QT += serialbus
继承自: QModbusServer

公共函数

QModbusTcpServer(QObject *parent = nullptr)
virtual ~QModbusTcpServer()
void installConnectionObserver(QModbusTcpConnectionObserver *observer)

信号

void modbusClientDisconnected(QTcpSocket *modbusClient)

重新实现的受保护函数

virtual void close() override
virtual bool open() override
virtual QModbusResponse processRequest(const QModbusPdu &request) override

详细说明

通过 Modbus 进行通信需要单个 Modbus 客户端实例与单个 Modbus 服务器之间进行交互。本类通过 TCP 服务器提供 Modbus 服务器的实现。

Modbus TCP 网络中可以存在多个服务器。客户端设备(由 `QModbusTcpClient` 表示)可对服务器进行读写操作。

成员函数文档

[explicit] QModbusTcpServer::QModbusTcpServer(QObject *parent = nullptr)

使用指定的parent 实例化一个QModbusTcpServer。serverAddress 的预设值为255 。

[virtual noexcept] QModbusTcpServer::~QModbusTcpServer()

销毁该QModbusTcpServer 实例。

[override virtual protected] void QModbusTcpServer::close()

重写了:QModbusDevice::close()。

void QModbusTcpServer::installConnectionObserver(QModbusTcpConnectionObserver *observer)

安装一个observer ,当有新的TCP客户端连接到此服务器实例时,可用于接收通知。此外,还可以使用observer 拒绝传入的TCP连接。

QModbusTcpServer 接管指定的observer 。此前设置的任何观察者都将被删除。可通过将nullptr 作为参数调用此函数来卸载该观察者。

另请参阅 QModbusTcpConnectionObserver 。

[signal] void QModbusTcpServer::modbusClientDisconnected(QTcpSocket *modbusClient)

当当前基于 TCP 的modbusClient 与该 Modbus TCP 服务器断开连接时,将发出此信号。请注意,可能同时有多个 TCP 客户端连接。

可通过在installConnectionObserver() 上安装一个QModbusTcpConnectionObserver 来接收新连接建立的通知。

另请参阅 installConnectionObserver 。

[override virtual protected] bool QModbusTcpServer::open()

重写了:QModbusDevice::open()。

[override virtual protected] QModbusResponse QModbusTcpServer::processRequest(const QModbusPdu &request)

重写了:QModbusServer::processRequest(const QModbusPdu &request)。

处理由request 指定的 Modbus 客户端请求,并返回一个 Modbus 响应。

根据《Modbus 应用协议规范 1.1b》,以下 Modbus 功能代码因仅适用于串行线路而被过滤掉:

对 TCP 服务器的请求将收到一个包含异常代码 QModbusExceptionResponse::IllegalFunction 的 Modbus 异常响应。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.