Debian Codegraph @

examples/serialbus/modbus/custom/doc/src/custom.qdoc

raw
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/custom
5 \title Modbus Custom Command
6 \ingroup qtserialbus-examples
7 \examplecategory Connectivity
8
9 \brief The example shows how to handle custom Modbus function codes.
10
11 The example acts as both Modbus client and server in a single application.
12 The connection between them is established via Modbus TCP. It is used to
13 send and receive custom Modbus requests and adjusts its internal states
14 based on the request and response.
15
16 The main purpose of the example is to provide some demo code on how to
17 implement a Modbus client or Modbus server handling custom Modbus function
18 codes.
19
20 \image ../images/custom.png
21
22 \section1 User-Defined Modbus Function Codes
23
24 The Modbus protocol supports function codes in the range \c {1 - 127
25 (0x01 - 0x7F HEX)}. Most of the function codes are well defined and publicly
26 documented. However, there are two ranges that can be used for user-defined
27 functions. Those are \c {65 - 72 (0x41 - 48 HEX)} and
28 \c {100 - 110 (0x64 - 0x6E HEX)}. The user can select function codes from
29 these ranges and handle them in some custom way.
30
31 This application uses function code \c {65 (0x41 HEX)} to implement
32 \c CustomRead command and function code \c {66 (0x42 HEX)} to implement
33 \c CustomWrite command. In this example the custom commands are used to
34 simply read and write the
35 \l {QModbusDataUnit::HoldingRegisters}{Holding Registers}.
36
37 \section1 Sending Custom Modbus Commands
38
39 The custom Modbus commands are sent using the
40 \l QModbusClient::sendRawRequest() method. This method requires to generate
41 a \l QModbusRequest object with the desired function code and a list of
42 arguments which will be encoded into a \l {QByteArray}:
43
44 \snippet modbus/custom/mainwindow.cpp generate_modbus_request
45
46 The \l QModbusClient::sendRawRequest() method returns a \l QModbusReply
47 object which can be used to check for errors as usual:
48
49 \snippet modbus/custom/mainwindow.cpp send_custom_command
50
51 \section1 Custom Modbus Server
52
53 The custom server is derived from the \l QModbusTcpServer class. It
54 overrides the \l QModbusServer::processPrivateRequest() method.
55
56 \snippet modbus/custom/modbusserver.h custom_server
57
58 The base server class calls the
59 \l {QModbusServer::}{processPrivateRequest()} method when a command with a
60 custom function code is received.
61
62 The custom implementation handles the \c CustomRead command by generating
63 a \l QModbusResponse with the values of requested registers:
64
65 \snippet modbus/custom/modbusserver.cpp handle_custom_read
66
67 Handling \c CustomWrite command includes extracting the new values from
68 the received \l QModbusPdu, doing the actual value update, and returning
69 a \l QModbusResponse with the registers that were actually updated:
70
71 \snippet modbus/custom/modbusserver.cpp handle_custom_write
72
73 \section1 Custom Modbus Client
74
75 The custom client is derived from the \l QModbusTcpClient class. It
76 overrides the \l QModbusClient::processPrivateResponse() method.
77
78 \snippet modbus/custom/modbusclient.h custom_client
79
80 The base client class calls the
81 \l {QModbusClient::}{processPrivateResponse()} method to process the server
82 responses with custom function codes.
83
84 The custom implementation handles the responses with \c CustomRead and
85 \c CustomWrite function codes:
86
87 \snippet modbus/custom/modbusclient.cpp private_response
88
89 The \c CustomRead response is handled by decoding the provided \l QModbusPdu
90 and extracting the values for requested registers:
91
92 \snippet modbus/custom/modbusclient.cpp process_custom_read
93
94 The \c CustomWrite response is handled by simply validating the response
95 parameters:
96
97 \snippet modbus/custom/modbusclient.cpp process_custom_write
98
99 \include examples-run.qdocinc
100
101 This example cannot be used in conjunction with other applications. Once the
102 example is started, it can only exchange custom Modbus commands within the
103 application itself. All interactions between the client and server use the
104 Modbus TCP protocol.
105 */