| 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 QMODBUSDEVICE_P_H |
| 5 | #define QMODBUSDEVICE_P_H |
| 6 | |
| 7 | #include <QtCore/qvariant.h> |
| 8 | #include <QtSerialBus/qmodbusdevice.h> |
| 9 | #if QT_CONFIG(modbus_serialport) |
| 10 | #include <QtSerialPort/qserialport.h> |
| 11 | #endif |
| 12 | |
| 13 | #include <private/qobject_p.h> |
| 14 | |
| 15 | // |
| 16 | // W A R N I N G |
| 17 | // ------------- |
| 18 | // |
| 19 | // This file is not part of the Qt API. It exists purely as an |
| 20 | // implementation detail. This header file may change from version to |
| 21 | // version without notice, or even be removed. |
| 22 | // |
| 23 | // We mean it. |
| 24 | // |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QModbusDevicePrivate : public QObjectPrivate |
| 29 | { |
| 30 | Q_DECLARE_PUBLIC(QModbusDevice) |
| 31 | |
| 32 | public: |
| 33 | QModbusDevice::State state = QModbusDevice::UnconnectedState; |
| 34 | QModbusDevice::Error error = QModbusDevice::NoError; |
| 35 | QString errorString; |
| 36 | |
| 37 | #if QT_CONFIG(modbus_serialport) |
| 38 | QString m_comPort; |
| 39 | QSerialPort::DataBits m_dataBits = QSerialPort::Data8; |
| 40 | QSerialPort::Parity m_parity = QSerialPort::EvenParity; |
| 41 | QSerialPort::StopBits m_stopBits = QSerialPort::OneStop; |
| 42 | QSerialPort::BaudRate m_baudRate = QSerialPort::Baud19200; |
| 43 | |
| 44 | /*! |
| 45 | According to the Modbus specification, in RTU mode message frames |
| 46 | are separated by a silent interval of at least 3.5 character times. |
| 47 | Calculate the timeout if we are less than 19200 baud, use a fixed |
| 48 | timeout for everything equal or greater than 19200 baud. |
| 49 | If the user set the timeout to be longer than the calculated one, |
| 50 | we'll keep the user defined. |
| 51 | */ |
| 52 | void calculateInterFrameDelay() |
| 53 | { |
| 54 | // The spec recommends a timeout value of 1.750 msec. Without such |
| 55 | // precise single-shot timers use a approximated value of 1.750 msec. |
| 56 | int delayMilliSeconds = RecommendedDelay; |
| 57 | if (m_baudRate < 19200) { |
| 58 | // Example: 9600 baud, 11 bit per packet -> 872 char/sec so: |
| 59 | // 1000 ms / 872 char = 1.147 ms/char * 3.5 character = 4.0145 ms |
| 60 | // Always round up because the spec requests at least 3.5 char. |
| 61 | delayMilliSeconds = qCeil(v: 3500. / (qreal(m_baudRate) / 11.)); |
| 62 | } |
| 63 | m_interFrameDelayMilliseconds = qMax(a: m_interFrameDelayMilliseconds, b: delayMilliSeconds); |
| 64 | } |
| 65 | static constexpr int RecommendedDelay = 2; // A approximated value of 1.750 msec. |
| 66 | int m_interFrameDelayMilliseconds = RecommendedDelay; |
| 67 | #endif |
| 68 | |
| 69 | int m_networkPort = 502; |
| 70 | QString m_networkAddress = QStringLiteral("127.0.0.1"); |
| 71 | |
| 72 | virtual QIODevice *device() const { return nullptr; } |
| 73 | }; |
| 74 | |
| 75 | QT_END_NAMESPACE |
| 76 | |
| 77 | #endif // QMODBUSDEVICE_P_H |
| 78 |
