| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only |
| 3 |
|
| 4 |
/*! \example modbus/server |
| 5 |
\title Modbus Server |
| 6 |
\ingroup qtserialbus-examples |
| 7 |
\examplecategory Connectivity |
| 8 |
|
| 9 |
\brief The example implements a Modbus server application. |
| 10 |
|
| 11 |
The example acts as Modbus server. It receives standard Modbus requests, |
| 12 |
adjusts its internal states based on the request and responds with the |
| 13 |
appropriate reply. |
| 14 |
|
| 15 |
This example must be used in conjunction with the |
| 16 |
\l {modbus/client}{Modbus Client example}. |
| 17 |
This example should be started and put into the listen state before the |
| 18 |
Modbus client example is started. Subsequent interactions between the two |
| 19 |
examples use the Modbus protocol. |
| 20 |
|
| 21 |
Key classes used in this example: |
| 22 |
|
| 23 |
\list |
| 24 |
\li \l QModbusServer |
| 25 |
\li \l QModbusDataUnit |
| 26 |
\endlist |
| 27 |
|
| 28 |
\image ../images/modbusserver.png |
| 29 |
|
| 30 |
\section1 Creating a QModbusServer |
| 31 |
|
| 32 |
An instance of \l QModbusServer is required to perform any communication. |
| 33 |
Depending on the specified connection type, the example can instantiate a |
| 34 |
\l QModbusRtuSerialServer (for serial communication) or a |
| 35 |
\l QModbusTcpServer (for TCP-based communication). |
| 36 |
|
| 37 |
\snippet modbus/server/mainwindow.cpp create_server_0 |
| 38 |
|
| 39 |
Once the server is created, the register map is set using |
| 40 |
\l QModbusServer::setMap() method. This register map is used by client |
| 41 |
applications to read and write server data. |
| 42 |
|
| 43 |
\snippet modbus/server/mainwindow.cpp create_server_1 |
| 44 |
|
| 45 |
After that the communication parameters and the server address are |
| 46 |
specified. The communication parameters depend on the communication type: |
| 47 |
|
| 48 |
\snippet modbus/server/mainwindow.cpp create_server_2 |
| 49 |
|
| 50 |
After the server is created and all the parameters are specified, use |
| 51 |
\l QModbusServer::connectDevice() to connect to Modbus network. |
| 52 |
|
| 53 |
\section1 Changing Local Values |
| 54 |
|
| 55 |
The example allows to change values of all supported |
| 56 |
\l {QModbusDataUnit::RegisterType}{register types} by using the provided |
| 57 |
combo boxes or line edits. Once the value is updated in the UI, the |
| 58 |
\l QModbusServer::setData() method is used to update the actual value on |
| 59 |
the server: |
| 60 |
|
| 61 |
\snippet modbus/server/mainwindow.cpp update_data_locally |
| 62 |
|
| 63 |
\section1 Handling Remote Writes |
| 64 |
|
| 65 |
The Modbus clients are capable of updating \l {QModbusDataUnit::}{Coils} |
| 66 |
and \l {QModbusDataUnit::}{HoldingRegisters} by sending write requests. |
| 67 |
Once the value is updated on the server side using such request, a |
| 68 |
\l QModbusServer::dataWritten() signal is emitted. |
| 69 |
|
| 70 |
\snippet modbus/server/mainwindow.cpp connect_data_written |
| 71 |
|
| 72 |
The Modbus server example connects to this signal, extracts the updated |
| 73 |
values and updates the UI accordingly: |
| 74 |
|
| 75 |
\snippet modbus/server/mainwindow.cpp update_data_from_remote |
| 76 |
|
| 77 |
\include examples-run.qdocinc |
| 78 |
*/ |