Debian Codegraph @

examples/serialbus/modbus/client/doc/src/modbusclient.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/client
5 \title Modbus Client
6 \ingroup qtserialbus-examples
7 \examplecategory Connectivity
8
9 \brief The example implements a Modbus client application.
10
11 The example acts as Modbus client sending Modbus request via serial line
12 or TCP. The shown dialog allows the definition of standard requests and
13 displays incoming responses.
14
15 The example must be used in conjunction with the
16 \l {modbus/server}{Modbus Server example} or another Modbus device which is
17 either connected via TCP or Serial Port.
18
19 Key classes used in this example:
20
21 \list
22 \li \l QModbusClient
23 \li \l QModbusDataUnit
24 \li \l QModbusReply
25 \endlist
26
27 \image ../images/modbusclient.png
28
29 \section1 Creating a QModbusClient
30
31 An instance of \l QModbusClient is required to perform any communication.
32 Depending on the specified connection type, the example can instantiate
33 a \l QModbusRtuSerialClient (for serial communication), or a
34 \l QModbusTcpClient (for TCP-based communication).
35
36 \snippet modbus/client/mainwindow.cpp create_client_0
37
38 Once the client is created, use the
39 \l {QModbusClient::}{setConnectionParameter()} method to specify the
40 connection parameters. The parameters vary depending on the communication
41 type:
42
43 \snippet modbus/client/mainwindow.cpp create_client_1
44
45 After the client is created and all the parameters are specified, use
46 \l {QModbusClient::connectDevice()} to connect to Modbus network.
47
48 \section1 Reading Data
49
50 To read data from Modbus server, the client needs to specify a
51 server address and the parameters of objects that it wants to read:
52 \list
53 \li \l {QModbusDataUnit::}{RegisterType}
54 \li \l {QModbusDataUnit::startAddress}{start address}
55 \li \l {QModbusDataUnit::valueCount}{number of entries to read}
56 \endlist
57
58 The object parameters are represented by the \l QModbusDataUnit class:
59
60 \snippet modbus/client/mainwindow.cpp read_data_0
61
62 Once the parameters are collected, the
63 \l {QModbusClient::}{sendReadRequest()} method is used to send the actual
64 request. The method returns a \l QModbusReply which should be processed
65 in an asynchronous way, so the \l QModbusReply::finished() signal is used
66 to check when the reply is ready.
67
68 \snippet modbus/client/mainwindow.cpp read_data_1
69
70 Once the \l QModbusReply::finished() signal is received, the reply object
71 can be used to get the data or to check for read errors:
72
73 \snippet modbus/client/mainwindow.cpp read_data_2
74
75 \section1 Writing Data
76
77 To write the data to the Modbus server, the client needs to specify the
78 server address, and the parameters of objects that it wants to write.
79 As with reading the data, the \l QModbusDataUnit class is used to represent
80 the information about the data to be written. This time the data also
81 includes the desired \l {QModbusDataUnit::}{values}.
82 The \l {QModbusClient::}{sendWriteRequest()} method is used to write the
83 desired data:
84
85 \snippet modbus/client/mainwindow.cpp write_data_0
86
87 Like with reading the data, the returned \l QModbusReply object is used
88 to check for write errors.
89
90 \include examples-run.qdocinc
91 */