- Qt 6.7
- Qt Serial Bus
- C++ Classes
- QModbusTcpServer
- Qt 6.7.0 Reference Documentation

# QModbusTcpServer Class

The QModbusTcpServer class represents a Modbus server that uses a TCP server for its communication with the Modbus client. More...

Header: | #include <QModbusTcpServer>
CMake: | find_package(Qt6 REQUIRED COMPONENTS SerialBus) target_link_libraries(mytarget PRIVATE Qt6::SerialBus)
qmake: | QT += serialbus
Inherits: | QModbusServer

- List of all members, including inherited members

## Public Functions

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

## Signals

void | modbusClientDisconnected(QTcpSocket *modbusClient)

## Reimplemented Protected Functions

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

## Detailed Description

Communication via Modbus requires the interaction between a single Modbus client instance and single Modbus server. This class provides the Modbus server implementation via a TCP server.

Modbus TCP networks can have multiple servers. Servers are read/written by a client device represented by QModbusTcpClient.

## Member Function Documentation

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

Constructs a QModbusTcpServer with the specified parent. The serverAddress preset is `255`.

### `[virtual noexcept]` QModbusTcpServer::~QModbusTcpServer()

Destroys the QModbusTcpServer instance.

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

Reimplements: QModbusDevice::close().

### void QModbusTcpServer::installConnectionObserver(QModbusTcpConnectionObserver *observer)

Installs an observer that can be used to obtain notifications when a new TCP client connects to this server instance. In addition, the observer can be used to reject the incoming TCP connection.

QModbusTcpServer takes ownership of the given observer. Any previously set observer will be deleted. The observer can be uninstalled by calling this function with `nullptr` as parameter.

See also QModbusTcpConnectionObserver.

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

This signal is emitted when a current TCP based modbusClient disconnects from this Modbus TCP server. Note that there might be several TCP clients connected at the same time.

Notifications on incoming new connections can be received by installing a QModbusTcpConnectionObserver via installConnectionObserver().

See also installConnectionObserver.

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

Reimplements: QModbusDevice::open().

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

Reimplements: QModbusServer::processRequest(const QModbusPdu &request).

Processes the Modbus client request specified by request and returns a Modbus response.

The following Modbus function codes are filtered out as they are serial line only according to the Modbus Application Protocol Specification 1.1b:

- QModbusRequest::ReadExceptionStatus
- QModbusRequest::Diagnostics
- QModbusRequest::GetCommEventCounter
- QModbusRequest::GetCommEventLog
- QModbusRequest::ReportServerId

A request to the TCP server will be answered with a Modbus exception response with the exception code QModbusExceptionResponse::IllegalFunction.