| 1 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#ifndef MODBUSTCPCLIENT_P_H |
| 5 |
#define MODBUSTCPCLIENT_P_H |
| 6 |
|
| 7 |
#include "modbustcpclient.h" |
| 8 |
|
| 9 |
#include <QDebug> |
| 10 |
#include <QModbusReply> |
| 11 |
|
| 12 |
#include <private/qmodbustcpclient_p.h> |
| 13 |
|
| 14 |
QT_USE_NAMESPACE |
| 15 |
|
| 16 |
class ModbusTcpClientPrivate : private QModbusTcpClientPrivate |
| 17 |
{ |
| 18 |
Q_DECLARE_PUBLIC(ModbusTcpClient) |
| 19 |
|
| 20 |
public: |
| 21 |
QModbusReply *enqueueRequest(const QModbusRequest &request, int, const QModbusDataUnit &unit, |
| 22 |
QModbusReply::ReplyType type) override |
| 23 |
{ |
| 24 |
auto writeToSocket = [this](const QModbusRequest &request) { |
| 25 |
QByteArray buffer; |
| 26 |
QDataStream output(&buffer, QIODevice::WriteOnly); |
| 27 |
output << m_tId << m_pId << m_length << m_uId << request; |
| 28 |
|
| 29 |
qint64 writtenBytes = m_socket->write(buffer); |
| 30 |
if (writtenBytes == -1 || writtenBytes < buffer.size()) { |
| 31 |
Q_Q(ModbusTcpClient); |
| 32 |
qDebug() << "Cannot write request to socket."; |
| 33 |
q->setError(QModbusTcpClient::tr("Could not write request to socket."), |
| 34 |
QModbusDevice::WriteError); |
| 35 |
return false; |
| 36 |
} |
| 37 |
qDebug() << "Sent TCP ADU:" << buffer.toHex(); |
| 38 |
qDebug() << "Sent TCP PDU:" << request << "with tId:" << Qt::hex << m_tId; |
| 39 |
return true; |
| 40 |
}; |
| 41 |
|
| 42 |
if (!writeToSocket(request)) |
| 43 |
return nullptr; |
| 44 |
|
| 45 |
Q_Q(ModbusTcpClient); |
| 46 |
auto reply = new QModbusReply(type, m_uId, q); |
| 47 |
const auto element = QueueElement{reply, request, unit, m_numberOfRetries, |
| 48 |
m_responseTimeoutDuration}; |
| 49 |
m_transactionStore.insert(m_tId, element); |
| 50 |
|
| 51 |
q->connect(q, &QModbusClient::timeoutChanged, |
| 52 |
element.timer.data(), QOverload<int>::of(&QTimer::setInterval)); |
| 53 |
QObject::connect(element.timer.data(), &QTimer::timeout, q, [this, writeToSocket]() { |
| 54 |
if (!m_transactionStore.contains(m_tId)) |
| 55 |
return; |
| 56 |
|
| 57 |
QueueElement elem = m_transactionStore.take(m_tId); |
| 58 |
if (elem.reply.isNull()) |
| 59 |
return; |
| 60 |
|
| 61 |
if (elem.numberOfRetries > 0) { |
| 62 |
elem.numberOfRetries--; |
| 63 |
if (!writeToSocket(elem.requestPdu)) |
| 64 |
return; |
| 65 |
m_transactionStore.insert(m_tId, elem); |
| 66 |
elem.timer->start(); |
| 67 |
qDebug() << "Resend request with tId:" << Qt::hex << m_tId; |
| 68 |
} else { |
| 69 |
qDebug() << "Timeout of request with tId:" << Qt::hex << m_tId; |
| 70 |
elem.reply->setError(QModbusDevice::TimeoutError, |
| 71 |
QModbusClient::tr("Request timeout.")); |
| 72 |
} |
| 73 |
}); |
| 74 |
element.timer->start(); |
| 75 |
return reply; |
| 76 |
} |
| 77 |
|
| 78 |
quint16 m_tId = 0; |
| 79 |
quint16 m_pId = 0; |
| 80 |
quint16 m_length = 0; |
| 81 |
quint8 m_uId = 0; |
| 82 |
}; |
| 83 |
|
| 84 |
#endif // MODBUSTCPCLIENT_P_H |