1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtSerialBus module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qmodbusrtuserialmaster.h"
38#include "qmodbusrtuserialmaster_p.h"
39
40#include <QtCore/qloggingcategory.h>
41
42QT_BEGIN_NAMESPACE
43
44Q_DECLARE_LOGGING_CATEGORY(QT_MODBUS)
45Q_DECLARE_LOGGING_CATEGORY(QT_MODBUS_LOW)
46
47/*!
48 \class QModbusRtuSerialMaster
49 \inmodule QtSerialBus
50 \since 5.8
51
52 \brief The QModbusRtuSerialMaster class represents a Modbus client
53 that uses a serial bus for its communication with the Modbus server.
54
55 Communication via Modbus requires the interaction between a single
56 Modbus client instance and multiple Modbus servers. This class
57 provides the client implementation via a serial port.
58*/
59
60/*!
61 Constructs a serial Modbus master with the specified \a parent.
62*/
63QModbusRtuSerialMaster::QModbusRtuSerialMaster(QObject *parent)
64 : QModbusClient(*new QModbusRtuSerialMasterPrivate, parent)
65{
66 Q_D(QModbusRtuSerialMaster);
67 d->setupSerialPort();
68}
69
70/*!
71 \internal
72*/
73QModbusRtuSerialMaster::~QModbusRtuSerialMaster()
74{
75 close();
76}
77
78/*!
79 Returns the amount of microseconds for the silent interval between two
80 consecutive Modbus messages.
81
82 \sa setInterFrameDelay()
83*/
84int QModbusRtuSerialMaster::interFrameDelay() const
85{
86 Q_D(const QModbusRtuSerialMaster);
87 return d->m_interFrameDelayMilliseconds * 1000;
88}
89
90/*!
91 Sets the amount of \a microseconds for the silent interval between two
92 consecutive Modbus messages. By default, the class implementation will use
93 a pre-calculated value according to the Modbus specification. A active or
94 running connection is not affected by such delay changes.
95
96 \note If \a microseconds is set to -1 or \a microseconds is less than the
97 pre-calculated delay then this pre-calculated value is used as frame delay.
98*/
99void QModbusRtuSerialMaster::setInterFrameDelay(int microseconds)
100{
101 Q_D(QModbusRtuSerialMaster);
102 d->m_interFrameDelayMilliseconds = qCeil(v: qreal(microseconds) / 1000.);
103 d->calculateInterFrameDelay();
104}
105
106/*!
107 \since 5.13
108
109 Returns the amount of milliseconds for the silent interval between a Modbus
110 broadcast and a consecutive Modbus messages. The default value is set to
111 \c 100 milliseconds.
112*/
113int QModbusRtuSerialMaster::turnaroundDelay() const
114{
115 Q_D(const QModbusRtuSerialMaster);
116 return d->m_turnaroundDelay;
117}
118
119/*!
120 \since 5.13
121
122 Sets the amount of milliseconds for the silent interval between a Modbus
123 broadcast and a consecutive Modbus messages to \a turnaroundDelay.
124 Typically the turnaround delay is in the range of \c 100 to \c 200
125 milliseconds.
126*/
127void QModbusRtuSerialMaster::setTurnaroundDelay(int turnaroundDelay)
128{
129 Q_D(QModbusRtuSerialMaster);
130 d->m_turnaroundDelay = turnaroundDelay;
131}
132
133/*!
134 \internal
135*/
136QModbusRtuSerialMaster::QModbusRtuSerialMaster(QModbusRtuSerialMasterPrivate &dd, QObject *parent)
137 : QModbusClient(dd, parent)
138{
139 Q_D(QModbusRtuSerialMaster);
140 d->setupSerialPort();
141}
142
143/*!
144 \reimp
145
146 \note When calling this function, existing buffered data is removed from
147 the serial port.
148*/
149bool QModbusRtuSerialMaster::open()
150{
151 if (state() == QModbusDevice::ConnectedState)
152 return true;
153
154 Q_D(QModbusRtuSerialMaster);
155 d->setupEnvironment(); // to be done before open
156 if (d->m_serialPort->open(mode: QIODevice::ReadWrite)) {
157 setState(QModbusDevice::ConnectedState);
158 d->m_serialPort->clear(); // only possible after open
159 } else {
160 setError(errorText: d->m_serialPort->errorString(), error: QModbusDevice::ConnectionError);
161 }
162 return (state() == QModbusDevice::ConnectedState);
163}
164
165/*!
166 \reimp
167*/
168void QModbusRtuSerialMaster::close()
169{
170 if (state() == QModbusDevice::UnconnectedState)
171 return;
172
173 setState(QModbusDevice::ClosingState);
174
175 Q_D(QModbusRtuSerialMaster);
176
177 if (d->m_serialPort->isOpen())
178 d->m_serialPort->close();
179
180 int numberOfAborts = 0;
181 while (!d->m_queue.isEmpty()) {
182 // Finish each open reply and forget them
183 QModbusRtuSerialMasterPrivate::QueueElement elem = d->m_queue.dequeue();
184 if (!elem.reply.isNull()) {
185 elem.reply->setError(error: QModbusDevice::ReplyAbortedError,
186 errorText: QModbusClient::tr(s: "Reply aborted due to connection closure."));
187 numberOfAborts++;
188 }
189 }
190
191 if (numberOfAborts > 0)
192 qCDebug(QT_MODBUS_LOW) << "(RTU client) Aborted replies:" << numberOfAborts;
193
194 setState(QModbusDevice::UnconnectedState);
195}
196
197QT_END_NAMESPACE
198

source code of qtserialbus/src/serialbus/qmodbusrtuserialmaster.cpp