QModbusTcpServer Class
QModbusTcpServer 类表示使用 TCP 服务器与 Modbus 客户端通信的 Modbus 服务器。更多
Header: | #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 功能代码只适用于串行线路,因此会被过滤掉:
- QModbusRequest::ReadExceptionStatus
- QModbusRequest::Diagnostics
- QModbusRequest::GetCommEventCounter
- QModbusRequest::GetCommEventLog
- QModbusRequest::ReportServerId
向 TCP 服务器发出的请求将得到 Modbus 异常响应,异常代码为 QModbusExceptionResponse::IllegalFunction。
© 2025 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.