| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> |
| 4 | ** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org> |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtSerialPort module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:BSD$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** BSD License Usage |
| 19 | ** Alternatively, you may use this file under the terms of the BSD license |
| 20 | ** as follows: |
| 21 | ** |
| 22 | ** "Redistribution and use in source and binary forms, with or without |
| 23 | ** modification, are permitted provided that the following conditions are |
| 24 | ** met: |
| 25 | ** * Redistributions of source code must retain the above copyright |
| 26 | ** notice, this list of conditions and the following disclaimer. |
| 27 | ** * Redistributions in binary form must reproduce the above copyright |
| 28 | ** notice, this list of conditions and the following disclaimer in |
| 29 | ** the documentation and/or other materials provided with the |
| 30 | ** distribution. |
| 31 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 32 | ** contributors may be used to endorse or promote products derived |
| 33 | ** from this software without specific prior written permission. |
| 34 | ** |
| 35 | ** |
| 36 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 37 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 38 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 39 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 40 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 42 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 43 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 44 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 45 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 46 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 47 | ** |
| 48 | ** $QT_END_LICENSE$ |
| 49 | ** |
| 50 | ****************************************************************************/ |
| 51 | |
| 52 | #include "mainwindow.h" |
| 53 | #include "ui_mainwindow.h" |
| 54 | #include "console.h" |
| 55 | #include "settingsdialog.h" |
| 56 | |
| 57 | #include <QLabel> |
| 58 | #include <QMessageBox> |
| 59 | |
| 60 | //! [0] |
| 61 | MainWindow::MainWindow(QWidget *parent) : |
| 62 | QMainWindow(parent), |
| 63 | m_ui(new Ui::MainWindow), |
| 64 | m_status(new QLabel), |
| 65 | m_console(new Console), |
| 66 | m_settings(new SettingsDialog), |
| 67 | //! [1] |
| 68 | m_serial(new QSerialPort(this)) |
| 69 | //! [1] |
| 70 | { |
| 71 | //! [0] |
| 72 | m_ui->setupUi(this); |
| 73 | m_console->setEnabled(false); |
| 74 | setCentralWidget(m_console); |
| 75 | |
| 76 | m_ui->actionConnect->setEnabled(true); |
| 77 | m_ui->actionDisconnect->setEnabled(false); |
| 78 | m_ui->actionQuit->setEnabled(true); |
| 79 | m_ui->actionConfigure->setEnabled(true); |
| 80 | |
| 81 | m_ui->statusBar->addWidget(widget: m_status); |
| 82 | |
| 83 | initActionsConnections(); |
| 84 | |
| 85 | connect(sender: m_serial, signal: &QSerialPort::errorOccurred, receiver: this, slot: &MainWindow::handleError); |
| 86 | |
| 87 | //! [2] |
| 88 | connect(sender: m_serial, signal: &QSerialPort::readyRead, receiver: this, slot: &MainWindow::readData); |
| 89 | //! [2] |
| 90 | connect(sender: m_console, signal: &Console::getData, receiver: this, slot: &MainWindow::writeData); |
| 91 | //! [3] |
| 92 | } |
| 93 | //! [3] |
| 94 | |
| 95 | MainWindow::~MainWindow() |
| 96 | { |
| 97 | delete m_settings; |
| 98 | delete m_ui; |
| 99 | } |
| 100 | |
| 101 | //! [4] |
| 102 | void MainWindow::openSerialPort() |
| 103 | { |
| 104 | const SettingsDialog::Settings p = m_settings->settings(); |
| 105 | m_serial->setPortName(p.name); |
| 106 | m_serial->setBaudRate(baudRate: p.baudRate); |
| 107 | m_serial->setDataBits(p.dataBits); |
| 108 | m_serial->setParity(p.parity); |
| 109 | m_serial->setStopBits(p.stopBits); |
| 110 | m_serial->setFlowControl(p.flowControl); |
| 111 | if (m_serial->open(mode: QIODevice::ReadWrite)) { |
| 112 | m_console->setEnabled(true); |
| 113 | m_console->setLocalEchoEnabled(p.localEchoEnabled); |
| 114 | m_ui->actionConnect->setEnabled(false); |
| 115 | m_ui->actionDisconnect->setEnabled(true); |
| 116 | m_ui->actionConfigure->setEnabled(false); |
| 117 | showStatusMessage(message: tr(s: "Connected to %1 : %2, %3, %4, %5, %6" ) |
| 118 | .arg(a: p.name).arg(a: p.stringBaudRate).arg(a: p.stringDataBits) |
| 119 | .arg(a: p.stringParity).arg(a: p.stringStopBits).arg(a: p.stringFlowControl)); |
| 120 | } else { |
| 121 | QMessageBox::critical(parent: this, title: tr(s: "Error" ), text: m_serial->errorString()); |
| 122 | |
| 123 | showStatusMessage(message: tr(s: "Open error" )); |
| 124 | } |
| 125 | } |
| 126 | //! [4] |
| 127 | |
| 128 | //! [5] |
| 129 | void MainWindow::closeSerialPort() |
| 130 | { |
| 131 | if (m_serial->isOpen()) |
| 132 | m_serial->close(); |
| 133 | m_console->setEnabled(false); |
| 134 | m_ui->actionConnect->setEnabled(true); |
| 135 | m_ui->actionDisconnect->setEnabled(false); |
| 136 | m_ui->actionConfigure->setEnabled(true); |
| 137 | showStatusMessage(message: tr(s: "Disconnected" )); |
| 138 | } |
| 139 | //! [5] |
| 140 | |
| 141 | void MainWindow::about() |
| 142 | { |
| 143 | QMessageBox::about(parent: this, title: tr(s: "About Simple Terminal" ), |
| 144 | text: tr(s: "The <b>Simple Terminal</b> example demonstrates how to " |
| 145 | "use the Qt Serial Port module in modern GUI applications " |
| 146 | "using Qt, with a menu bar, toolbars, and a status bar." )); |
| 147 | } |
| 148 | |
| 149 | //! [6] |
| 150 | void MainWindow::writeData(const QByteArray &data) |
| 151 | { |
| 152 | m_serial->write(data); |
| 153 | } |
| 154 | //! [6] |
| 155 | |
| 156 | //! [7] |
| 157 | void MainWindow::readData() |
| 158 | { |
| 159 | const QByteArray data = m_serial->readAll(); |
| 160 | m_console->putData(data); |
| 161 | } |
| 162 | //! [7] |
| 163 | |
| 164 | //! [8] |
| 165 | void MainWindow::handleError(QSerialPort::SerialPortError error) |
| 166 | { |
| 167 | if (error == QSerialPort::ResourceError) { |
| 168 | QMessageBox::critical(parent: this, title: tr(s: "Critical Error" ), text: m_serial->errorString()); |
| 169 | closeSerialPort(); |
| 170 | } |
| 171 | } |
| 172 | //! [8] |
| 173 | |
| 174 | void MainWindow::initActionsConnections() |
| 175 | { |
| 176 | connect(sender: m_ui->actionConnect, signal: &QAction::triggered, receiver: this, slot: &MainWindow::openSerialPort); |
| 177 | connect(sender: m_ui->actionDisconnect, signal: &QAction::triggered, receiver: this, slot: &MainWindow::closeSerialPort); |
| 178 | connect(sender: m_ui->actionQuit, signal: &QAction::triggered, receiver: this, slot: &MainWindow::close); |
| 179 | connect(sender: m_ui->actionConfigure, signal: &QAction::triggered, receiver: m_settings, slot: &SettingsDialog::show); |
| 180 | connect(sender: m_ui->actionClear, signal: &QAction::triggered, receiver: m_console, slot: &Console::clear); |
| 181 | connect(sender: m_ui->actionAbout, signal: &QAction::triggered, receiver: this, slot: &MainWindow::about); |
| 182 | connect(sender: m_ui->actionAboutQt, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt); |
| 183 | } |
| 184 | |
| 185 | void MainWindow::showStatusMessage(const QString &message) |
| 186 | { |
| 187 | m_status->setText(message); |
| 188 | } |
| 189 | |