Terminal Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com>
** Copyright (C) 2012 Laszlo Papp <lpapp@kde.org>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtSerialPort module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"mainwindow.h"
#include
"ui_mainwindow.h"
#include
"console.h"
#include
"settingsdialog.h"
#include <QLabel>
#include <QMessageBox>
MainWindow::
MainWindow(QWidget *
parent) :
QMainWindow(parent),
m_ui(new
Ui::
MainWindow),
m_status(new
QLabel),
m_console(new
Console),
m_settings(new
SettingsDialog),
m_serial(new
QSerialPort(this
))
{
m_ui-&
gt;setupUi(this
);
m_console-&
gt;setEnabled(false
);
setCentralWidget(m_console);
m_ui-&
gt;actionConnect-&
gt;setEnabled(true
);
m_ui-&
gt;actionDisconnect-&
gt;setEnabled(false
);
m_ui-&
gt;actionQuit-&
gt;setEnabled(true
);
m_ui-&
gt;actionConfigure-&
gt;setEnabled(true
);
m_ui-&
gt;statusBar-&
gt;addWidget(m_status);
initActionsConnections();
connect(m_serial, &
amp;QSerialPort::
errorOccurred, this
, &
amp;MainWindow::
handleError);
connect(m_serial, &
amp;QSerialPort::
readyRead, this
, &
amp;MainWindow::
readData);
connect(m_console, &
amp;Console::
getData, this
, &
amp;MainWindow::
writeData);
}
MainWindow::
~
MainWindow()
{
delete
m_settings;
delete
m_ui;
}
void
MainWindow::
openSerialPort()
{
const
SettingsDialog::
Settings p =
m_settings-&
gt;settings();
m_serial-&
gt;setPortName(p.name);
m_serial-&
gt;setBaudRate(p.baudRate);
m_serial-&
gt;setDataBits(p.dataBits);
m_serial-&
gt;setParity(p.parity);
m_serial-&
gt;setStopBits(p.stopBits);
m_serial-&
gt;setFlowControl(p.flowControl);
if
(m_serial-&
gt;open(QIODevice::
ReadWrite)) {
m_console-&
gt;setEnabled(true
);
m_console-&
gt;setLocalEchoEnabled(p.localEchoEnabled);
m_ui-&
gt;actionConnect-&
gt;setEnabled(false
);
m_ui-&
gt;actionDisconnect-&
gt;setEnabled(true
);
m_ui-&
gt;actionConfigure-&
gt;setEnabled(false
);
showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6"
)
.arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
.arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
}
else
{
QMessageBox::
critical(this
, tr("Error"
), m_serial-&
gt;errorString());
showStatusMessage(tr("Open error"
));
}
}
void
MainWindow::
closeSerialPort()
{
if
(m_serial-&
gt;isOpen())
m_serial-&
gt;close();
m_console-&
gt;setEnabled(false
);
m_ui-&
gt;actionConnect-&
gt;setEnabled(true
);
m_ui-&
gt;actionDisconnect-&
gt;setEnabled(false
);
m_ui-&
gt;actionConfigure-&
gt;setEnabled(true
);
showStatusMessage(tr("Disconnected"
));
}
void
MainWindow::
about()
{
QMessageBox::
about(this
, tr("About Simple Terminal"
),
tr("The <b>Simple Terminal</b> example demonstrates how to "
"use the Qt Serial Port module in modern GUI applications "
"using Qt, with a menu bar, toolbars, and a status bar."
));
}
void
MainWindow::
writeData(const
QByteArray &
amp;data)
{
m_serial-&
gt;write(data);
}
void
MainWindow::
readData()
{
const
QByteArray data =
m_serial-&
gt;readAll();
m_console-&
gt;putData(data);
}
void
MainWindow::
handleError(QSerialPort::
SerialPortError error)
{
if
(error ==
QSerialPort::
ResourceError) {
QMessageBox::
critical(this
, tr("Critical Error"
), m_serial-&
gt;errorString());
closeSerialPort();
}
}
void
MainWindow::
initActionsConnections()
{
connect(m_ui-&
gt;actionConnect, &
amp;QAction::
triggered, this
, &
amp;MainWindow::
openSerialPort);
connect(m_ui-&
gt;actionDisconnect, &
amp;QAction::
triggered, this
, &
amp;MainWindow::
closeSerialPort);
connect(m_ui-&
gt;actionQuit, &
amp;QAction::
triggered, this
, &
amp;MainWindow::
close);
connect(m_ui-&
gt;actionConfigure, &
amp;QAction::
triggered, m_settings, &
amp;SettingsDialog::
show);
connect(m_ui-&
gt;actionClear, &
amp;QAction::
triggered, m_console, &
amp;Console::
clear);
connect(m_ui-&
gt;actionAbout, &
amp;QAction::
triggered, this
, &
amp;MainWindow::
about);
connect(m_ui-&
gt;actionAboutQt, &
amp;QAction::
triggered, qApp, &
amp;QApplication::
aboutQt);
}
void
MainWindow::
showStatusMessage(const
QString &
amp;message)
{
m_status-&
gt;setText(message);
}