1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMODBUSCLIENT_P_H
5#define QMODBUSCLIENT_P_H
6
7#include <QtCore/qtimer.h>
8#include <QtSerialBus/qmodbusclient.h>
9#include <QtSerialBus/qmodbuspdu.h>
10
11#include <private/qmodbusdevice_p.h>
12#include <limits.h>
13
14//
15// W A R N I N G
16// -------------
17//
18// This file is not part of the Qt API. It exists purely as an
19// implementation detail. This header file may change from version to
20// version without notice, or even be removed.
21//
22// We mean it.
23//
24
25QT_BEGIN_NAMESPACE
26
27class Q_AUTOTEST_EXPORT QModbusClientPrivate : public QModbusDevicePrivate
28{
29 Q_DECLARE_PUBLIC(QModbusClient)
30
31public:
32 QModbusReply *sendRequest(const QModbusRequest &request, int serverAddress,
33 const QModbusDataUnit *const unit);
34 QModbusRequest createReadRequest(const QModbusDataUnit &data) const;
35 QModbusRequest createWriteRequest(const QModbusDataUnit &data) const;
36 QModbusRequest createRWRequest(const QModbusDataUnit &read, const QModbusDataUnit &write) const;
37
38 bool processResponse(const QModbusResponse &response, QModbusDataUnit *data);
39
40 bool processReadCoilsResponse(const QModbusResponse &response, QModbusDataUnit *data);
41 bool processReadDiscreteInputsResponse(const QModbusResponse &response, QModbusDataUnit *data);
42 bool collateBits(const QModbusPdu &pdu, QModbusDataUnit::RegisterType type, QModbusDataUnit *data);
43
44 bool processReadHoldingRegistersResponse(const QModbusResponse &response, QModbusDataUnit *data);
45 bool processReadInputRegistersResponse(const QModbusResponse &response, QModbusDataUnit *data);
46 bool collateBytes(const QModbusPdu &pdu, QModbusDataUnit::RegisterType type, QModbusDataUnit *data);
47
48 bool processWriteSingleCoilResponse(const QModbusResponse &response, QModbusDataUnit *data);
49 bool processWriteSingleRegisterResponse(const QModbusResponse &response,
50 QModbusDataUnit *data);
51 bool collateSingleValue(const QModbusPdu &pdu, QModbusDataUnit::RegisterType type,
52 QModbusDataUnit *data);
53
54 bool processWriteMultipleCoilsResponse(const QModbusResponse &response, QModbusDataUnit *data);
55 bool processWriteMultipleRegistersResponse(const QModbusResponse &response,
56 QModbusDataUnit *data);
57 bool collateMultipleValues(const QModbusPdu &pdu, QModbusDataUnit::RegisterType type,
58 QModbusDataUnit *data);
59
60 bool processReadWriteMultipleRegistersResponse(const QModbusResponse &response,
61 QModbusDataUnit *data);
62
63 virtual QModbusReply *enqueueRequest(const QModbusRequest &, int, const QModbusDataUnit &,
64 QModbusReply::ReplyType) {
65 return nullptr;
66 }
67 // TODO: Review once we have a transport layer in place.
68 virtual bool isOpen() const { return false; }
69
70 int m_numberOfRetries = 3;
71 int m_responseTimeoutDuration = 1000;
72
73 struct QueueElement {
74 QueueElement() = default;
75 QueueElement(QModbusReply *r, const QModbusRequest &req, const QModbusDataUnit &u, int num,
76 int timeout = -1)
77 : reply(r), requestPdu(req), unit(u), numberOfRetries(num)
78 {
79 if (timeout >= 0) {
80 // always the case for TCP
81 timer = QSharedPointer<QTimer>::create();
82 timer->setSingleShot(true);
83 timer->setInterval(timeout);
84 }
85 }
86 bool operator==(const QueueElement &other) const {
87 return reply == other.reply;
88 }
89
90 QPointer<QModbusReply> reply;
91 QModbusRequest requestPdu;
92 QModbusDataUnit unit;
93 int numberOfRetries;
94 QSharedPointer<QTimer> timer;
95 QByteArray adu;
96 qint64 bytesWritten = 0;
97 qint32 m_timerId = INT_MIN;
98 };
99 void processQueueElement(const QModbusResponse &pdu, const QueueElement &element);
100};
101
102QT_END_NAMESPACE
103
104#endif // QMODBUSCLIENT_P_H
105
106

source code of qtserialbus/src/serialbus/qmodbusclient_p.h