| 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 "qmodbustcpclient.h" | 
| 38 | #include "qmodbustcpclient_p.h" | 
| 39 | |
| 40 | #include <QtCore/qurl.h> | 
| 41 | |
| 42 | QT_BEGIN_NAMESPACE | 
| 43 | |
| 44 | /*! | 
| 45 | \class QModbusTcpClient | 
| 46 | \inmodule QtSerialBus | 
| 47 | \since 5.8 | 
| 48 | |
| 49 | \brief The QModbusTcpClient class is the interface class for Modbus TCP client device. | 
| 50 | |
| 51 | QModbusTcpClient communicates with the Modbus backend providing users with a convenient API. | 
| 52 | */ | 
| 53 | |
| 54 | /*! | 
| 55 | Constructs a QModbusTcpClient with the specified \a parent. | 
| 56 | */ | 
| 57 | QModbusTcpClient::QModbusTcpClient(QObject *parent) | 
| 58 | : QModbusClient(*new QModbusTcpClientPrivate, parent) | 
| 59 | { | 
| 60 | Q_D(QModbusTcpClient); | 
| 61 | d->setupTcpSocket(); | 
| 62 | } | 
| 63 | |
| 64 | /*! | 
| 65 | Destroys the QModbusTcpClient instance. | 
| 66 | */ | 
| 67 | QModbusTcpClient::~QModbusTcpClient() | 
| 68 | { | 
| 69 | close(); | 
| 70 | } | 
| 71 | |
| 72 | /*! | 
| 73 | \internal | 
| 74 | */ | 
| 75 | QModbusTcpClient::QModbusTcpClient(QModbusTcpClientPrivate &dd, QObject *parent) | 
| 76 | : QModbusClient(dd, parent) | 
| 77 | { | 
| 78 | Q_D(QModbusTcpClient); | 
| 79 | d->setupTcpSocket(); | 
| 80 | } | 
| 81 | |
| 82 | /*! | 
| 83 | \reimp | 
| 84 | */ | 
| 85 | bool QModbusTcpClient::open() | 
| 86 | { | 
| 87 | if (state() == QModbusDevice::ConnectedState) | 
| 88 | return true; | 
| 89 | |
| 90 | Q_D(QModbusTcpClient); | 
| 91 | if (d->m_socket->state() != QAbstractSocket::UnconnectedState) | 
| 92 | return false; | 
| 93 | |
| 94 |     const QUrl url = QUrl::fromUserInput(userInput: d->m_networkAddress + QStringLiteral(":")  | 
| 95 | + QString::number(d->m_networkPort)); | 
| 96 | |
| 97 | if (!url.isValid()) { | 
| 98 |         setError(errorText: tr(s: "Invalid connection settings for TCP communication specified."),  | 
| 99 | error: QModbusDevice::ConnectionError); | 
| 100 |         qCWarning(QT_MODBUS) << "(TCP client) Invalid host:"<< url.host() << "or port:"  | 
| 101 | << url.port(); | 
| 102 | return false; | 
| 103 | } | 
| 104 | |
| 105 | d->m_socket->connectToHost(hostName: url.host(), port: url.port()); | 
| 106 | |
| 107 | return true; | 
| 108 | } | 
| 109 | |
| 110 | /*! | 
| 111 | \reimp | 
| 112 | */ | 
| 113 | void QModbusTcpClient::close() | 
| 114 | { | 
| 115 | if (state() == QModbusDevice::UnconnectedState) | 
| 116 | return; | 
| 117 | |
| 118 | Q_D(QModbusTcpClient); | 
| 119 | d->m_socket->disconnectFromHost(); | 
| 120 | } | 
| 121 | |
| 122 | QT_END_NAMESPACE | 
| 123 | 
