From 6ff43af117284bee42c52f8104a5cb93c873690f Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 8 Feb 2023 10:58:10 +0100 Subject: Move Senred and Receiver examples to manual tests These examples have no description, so they are not visible in the docs and in QtCreator anyway. The APIs that are shown by these examples are also covered by other examples. Still they can be useful while debugging, so move them to tests/manual/examples. Task-number: QTBUG-110645 Task-number: QTBUG-108868 Change-Id: I7d575372a4b0af57f4564b18f6d024347e812c7e Reviewed-by: Alex Blasche (cherry picked from commit b114f04ed2b900884a4df5bc2ce855addbd467c9) Reviewed-by: Qt Cherry-pick Bot --- examples/serialport/CMakeLists.txt | 4 +- examples/serialport/receiver/CMakeLists.txt | 38 --------- examples/serialport/receiver/dialog.cpp | 106 ------------------------- examples/serialport/receiver/dialog.h | 54 ------------- examples/serialport/receiver/main.cpp | 14 ---- examples/serialport/receiver/receiver.pro | 15 ---- examples/serialport/sender/CMakeLists.txt | 38 --------- examples/serialport/sender/dialog.cpp | 107 -------------------------- examples/serialport/sender/dialog.h | 54 ------------- examples/serialport/sender/main.cpp | 14 ---- examples/serialport/sender/sender.pro | 15 ---- examples/serialport/serialport.pro | 2 +- tests/manual/CMakeLists.txt | 7 ++ tests/manual/examples/receiver/CMakeLists.txt | 26 +++++++ tests/manual/examples/receiver/dialog.cpp | 106 +++++++++++++++++++++++++ tests/manual/examples/receiver/dialog.h | 54 +++++++++++++ tests/manual/examples/receiver/main.cpp | 14 ++++ tests/manual/examples/receiver/receiver.pro | 12 +++ tests/manual/examples/sender/CMakeLists.txt | 28 +++++++ tests/manual/examples/sender/dialog.cpp | 107 ++++++++++++++++++++++++++ tests/manual/examples/sender/dialog.h | 54 +++++++++++++ tests/manual/examples/sender/main.cpp | 14 ++++ tests/manual/examples/sender/sender.pro | 12 +++ 23 files changed, 436 insertions(+), 459 deletions(-) delete mode 100644 examples/serialport/receiver/CMakeLists.txt delete mode 100644 examples/serialport/receiver/dialog.cpp delete mode 100644 examples/serialport/receiver/dialog.h delete mode 100644 examples/serialport/receiver/main.cpp delete mode 100644 examples/serialport/receiver/receiver.pro delete mode 100644 examples/serialport/sender/CMakeLists.txt delete mode 100644 examples/serialport/sender/dialog.cpp delete mode 100644 examples/serialport/sender/dialog.h delete mode 100644 examples/serialport/sender/main.cpp delete mode 100644 examples/serialport/sender/sender.pro create mode 100644 tests/manual/CMakeLists.txt create mode 100644 tests/manual/examples/receiver/CMakeLists.txt create mode 100644 tests/manual/examples/receiver/dialog.cpp create mode 100644 tests/manual/examples/receiver/dialog.h create mode 100644 tests/manual/examples/receiver/main.cpp create mode 100644 tests/manual/examples/receiver/receiver.pro create mode 100644 tests/manual/examples/sender/CMakeLists.txt create mode 100644 tests/manual/examples/sender/dialog.cpp create mode 100644 tests/manual/examples/sender/dialog.h create mode 100644 tests/manual/examples/sender/main.cpp create mode 100644 tests/manual/examples/sender/sender.pro diff --git a/examples/serialport/CMakeLists.txt b/examples/serialport/CMakeLists.txt index a771a68c..dd3d50d0 100644 --- a/examples/serialport/CMakeLists.txt +++ b/examples/serialport/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (C) 2022 The Qt Company Ltd. +# Copyright (C) 2023 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause qt_internal_add_example(creaderasync) @@ -7,6 +7,4 @@ if(TARGET Qt::Widgets) qt_internal_add_example(terminal) qt_internal_add_example(blockingsender) qt_internal_add_example(blockingreceiver) - qt_internal_add_example(sender) - qt_internal_add_example(receiver) endif() diff --git a/examples/serialport/receiver/CMakeLists.txt b/examples/serialport/receiver/CMakeLists.txt deleted file mode 100644 index f929a465..00000000 --- a/examples/serialport/receiver/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(receiver LANGUAGES CXX) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/serialport/receiver") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui SerialPort Widgets) - -qt_standard_project_setup() - -qt_add_executable(receiver - dialog.cpp dialog.h - main.cpp -) - -set_target_properties(receiver PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(receiver PRIVATE - Qt::Core - Qt::Gui - Qt::SerialPort - Qt::Widgets -) - -install(TARGETS receiver - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/serialport/receiver/dialog.cpp b/examples/serialport/receiver/dialog.cpp deleted file mode 100644 index 51e18ea8..00000000 --- a/examples/serialport/receiver/dialog.cpp +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright (C) 2012 Denis Shienkov -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "dialog.h" - -#include -#include -#include -#include -#include -#include -#include - -Dialog::Dialog(QWidget *parent) : - QDialog(parent), - m_serialPortLabel(new QLabel(tr("Serial port:"))), - m_serialPortComboBox(new QComboBox), - m_waitRequestLabel(new QLabel(tr("Wait request, msec:"))), - m_waitRequestSpinBox(new QSpinBox), - m_responseLabel(new QLabel(tr("Response:"))), - m_responseLineEdit(new QLineEdit(tr("Hello, I'm the receiver."))), - m_trafficLabel(new QLabel(tr("No traffic."))), - m_statusLabel(new QLabel(tr("Status: Not running."))), - m_runButton(new QPushButton(tr("Start"))) -{ - m_waitRequestSpinBox->setRange(0, 10000); - m_waitRequestSpinBox->setValue(20); - - const auto infos = QSerialPortInfo::availablePorts(); - for (const QSerialPortInfo &info : infos) - m_serialPortComboBox->addItem(info.portName()); - - auto mainLayout = new QGridLayout; - mainLayout->addWidget(m_serialPortLabel, 0, 0); - mainLayout->addWidget(m_serialPortComboBox, 0, 1); - mainLayout->addWidget(m_waitRequestLabel, 1, 0); - mainLayout->addWidget(m_waitRequestSpinBox, 1, 1); - mainLayout->addWidget(m_runButton, 0, 2, 2, 1); - mainLayout->addWidget(m_responseLabel, 2, 0); - mainLayout->addWidget(m_responseLineEdit, 2, 1, 1, 3); - mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4); - mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5); - setLayout(mainLayout); - - setWindowTitle(tr("Receiver")); - m_serialPortComboBox->setFocus(); - - m_timer.setSingleShot(true); - - connect(m_runButton, &QPushButton::clicked, this, &Dialog::startReceiver); - connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readRequest); - connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout); - - connect(m_serialPortComboBox, &QComboBox::currentIndexChanged, this, &Dialog::activateRunButton); - connect(m_waitRequestSpinBox, &QSpinBox::valueChanged, this, &Dialog::activateRunButton); - connect(m_responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton); -} - -void Dialog::startReceiver() -{ - if (m_serial.portName() != m_serialPortComboBox->currentText()) { - m_serial.close(); - m_serial.setPortName(m_serialPortComboBox->currentText()); - - if (!m_serial.open(QIODevice::ReadWrite)) { - processError(tr("Can't open %1, error code %2") - .arg(m_serial.portName()).arg(m_serial.error())); - return; - } - } - - m_runButton->setEnabled(false); - m_statusLabel->setText(tr("Status: Running, connected to port %1.") - .arg(m_serialPortComboBox->currentText())); -} - -void Dialog::readRequest() -{ - if (!m_timer.isActive()) - m_timer.start(m_waitRequestSpinBox->value()); - m_request.append(m_serial.readAll()); -} - -void Dialog::processTimeout() -{ - m_serial.write(m_responseLineEdit->text().toUtf8()); - - m_trafficLabel->setText(tr("Traffic, transaction #%1:" - "\n\r-request: %2" - "\n\r-response: %3") - .arg(++m_transactionCount).arg(QString::fromUtf8(m_request)) - .arg(m_responseLineEdit->text())); - m_request.clear(); -} - -void Dialog::activateRunButton() -{ - m_runButton->setEnabled(true); -} - -void Dialog::processError(const QString &s) -{ - activateRunButton(); - m_statusLabel->setText(tr("Status: Not running, %1.").arg(s)); - m_trafficLabel->setText(tr("No traffic.")); -} diff --git a/examples/serialport/receiver/dialog.h b/examples/serialport/receiver/dialog.h deleted file mode 100644 index a32e1480..00000000 --- a/examples/serialport/receiver/dialog.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (C) 2012 Denis Shienkov -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef DIALOG_H -#define DIALOG_H - -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QLabel; -class QLineEdit; -class QComboBox; -class QSpinBox; -class QPushButton; - -QT_END_NAMESPACE - -class Dialog : public QDialog -{ - Q_OBJECT - -public: - explicit Dialog(QWidget *parent = nullptr); - -private slots: - void startReceiver(); - void readRequest(); - void processTimeout(); - void activateRunButton(); - -private: - void processError(const QString &s); - -private: - int m_transactionCount = 0; - QLabel *m_serialPortLabel = nullptr; - QComboBox *m_serialPortComboBox = nullptr; - QLabel *m_waitRequestLabel = nullptr; - QSpinBox *m_waitRequestSpinBox = nullptr; - QLabel *m_responseLabel = nullptr; - QLineEdit *m_responseLineEdit = nullptr; - QLabel *m_trafficLabel = nullptr; - QLabel *m_statusLabel = nullptr; - QPushButton *m_runButton = nullptr; - - QSerialPort m_serial; - QByteArray m_request; - QTimer m_timer; -}; - -#endif // DIALOG_H diff --git a/examples/serialport/receiver/main.cpp b/examples/serialport/receiver/main.cpp deleted file mode 100644 index f08a0eed..00000000 --- a/examples/serialport/receiver/main.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (C) 2012 Denis Shienkov -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "dialog.h" - -#include - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - Dialog dialog; - dialog.show(); - return app.exec(); -} diff --git a/examples/serialport/receiver/receiver.pro b/examples/serialport/receiver/receiver.pro deleted file mode 100644 index 3842f505..00000000 --- a/examples/serialport/receiver/receiver.pro +++ /dev/null @@ -1,15 +0,0 @@ -QT += widgets serialport -requires(qtConfig(combobox)) - -TARGET = receiver -TEMPLATE = app - -HEADERS += \ - dialog.h - -SOURCES += \ - main.cpp \ - dialog.cpp - -target.path = $$[QT_INSTALL_EXAMPLES]/serialport/receiver -INSTALLS += target diff --git a/examples/serialport/sender/CMakeLists.txt b/examples/serialport/sender/CMakeLists.txt deleted file mode 100644 index 2fa11cb8..00000000 --- a/examples/serialport/sender/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (C) 2022 The Qt Company Ltd. -# SPDX-License-Identifier: BSD-3-Clause - -cmake_minimum_required(VERSION 3.16) -project(sender LANGUAGES CXX) - -if(NOT DEFINED INSTALL_EXAMPLESDIR) - set(INSTALL_EXAMPLESDIR "examples") -endif() - -set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/serialport/sender") - -find_package(Qt6 REQUIRED COMPONENTS Core Gui SerialPort Widgets) - -qt_standard_project_setup() - -qt_add_executable(sender - dialog.cpp dialog.h - main.cpp -) - -set_target_properties(sender PROPERTIES - WIN32_EXECUTABLE TRUE - MACOSX_BUNDLE TRUE -) - -target_link_libraries(sender PRIVATE - Qt::Core - Qt::Gui - Qt::SerialPort - Qt::Widgets -) - -install(TARGETS sender - RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" - BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" - LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" -) diff --git a/examples/serialport/sender/dialog.cpp b/examples/serialport/sender/dialog.cpp deleted file mode 100644 index d19ec888..00000000 --- a/examples/serialport/sender/dialog.cpp +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright (C) 2012 Denis Shienkov -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "dialog.h" - -#include -#include -#include -#include -#include -#include -#include - -Dialog::Dialog(QWidget *parent) : - QDialog(parent), - m_transactionCount(0), - m_serialPortLabel(new QLabel(tr("Serial port:"))), - m_serialPortComboBox(new QComboBox), - m_waitResponseLabel(new QLabel(tr("Wait response, msec:"))), - m_waitResponseSpinBox(new QSpinBox), - m_requestLabel(new QLabel(tr("Request:"))), - m_requestLineEdit(new QLineEdit(tr("Who are you?"))), - m_trafficLabel(new QLabel(tr("No traffic."))), - m_statusLabel(new QLabel(tr("Status: Not running."))), - m_runButton(new QPushButton(tr("Start"))) -{ - const auto infos = QSerialPortInfo::availablePorts(); - for (const QSerialPortInfo &info : infos) - m_serialPortComboBox->addItem(info.portName()); - - m_waitResponseSpinBox->setRange(0, 10000); - m_waitResponseSpinBox->setValue(100); - - auto mainLayout = new QGridLayout; - mainLayout->addWidget(m_serialPortLabel, 0, 0); - mainLayout->addWidget(m_serialPortComboBox, 0, 1); - mainLayout->addWidget(m_waitResponseLabel, 1, 0); - mainLayout->addWidget(m_waitResponseSpinBox, 1, 1); - mainLayout->addWidget(m_runButton, 0, 2, 2, 1); - mainLayout->addWidget(m_requestLabel, 2, 0); - mainLayout->addWidget(m_requestLineEdit, 2, 1, 1, 3); - mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4); - mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5); - setLayout(mainLayout); - - setWindowTitle(tr("Sender")); - m_serialPortComboBox->setFocus(); - - m_timer.setSingleShot(true); - - connect(m_runButton, &QPushButton::clicked, this, &Dialog::sendRequest); - connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readResponse); - connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout); -} - -void Dialog::sendRequest() -{ - if (m_serial.portName() != m_serialPortComboBox->currentText()) { - m_serial.close(); - m_serial.setPortName(m_serialPortComboBox->currentText()); - - if (!m_serial.open(QIODevice::ReadWrite)) { - processError(tr("Can't open %1, error code %2") - .arg(m_serial.portName()).arg(m_serial.error())); - return; - } - } - - setControlsEnabled(false); - m_statusLabel->setText(tr("Status: Running, connected to port %1.") - .arg(m_serialPortComboBox->currentText())); - - m_serial.write(m_requestLineEdit->text().toUtf8()); - m_timer.start(m_waitResponseSpinBox->value()); -} - -void Dialog::readResponse() -{ - m_response.append(m_serial.readAll()); -} - -void Dialog::processTimeout() -{ - setControlsEnabled(true); - m_trafficLabel->setText(tr("Traffic, transaction #%1:" - "\n\r-request: %2" - "\n\r-response: %3") - .arg(++m_transactionCount) - .arg(m_requestLineEdit->text()) - .arg(QString::fromUtf8(m_response))); - m_response.clear(); -} - -void Dialog::processError(const QString &error) -{ - setControlsEnabled(true); - m_statusLabel->setText(tr("Status: Not running, %1.").arg(error)); - m_trafficLabel->setText(tr("No traffic.")); -} - -void Dialog::setControlsEnabled(bool enable) -{ - m_runButton->setEnabled(enable); - m_serialPortComboBox->setEnabled(enable); - m_waitResponseSpinBox->setEnabled(enable); - m_requestLineEdit->setEnabled(enable); -} diff --git a/examples/serialport/sender/dialog.h b/examples/serialport/sender/dialog.h deleted file mode 100644 index 0f049950..00000000 --- a/examples/serialport/sender/dialog.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (C) 2012 Denis Shienkov -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#ifndef DIALOG_H -#define DIALOG_H - -#include -#include -#include - -QT_BEGIN_NAMESPACE - -class QLabel; -class QLineEdit; -class QSpinBox; -class QPushButton; -class QComboBox; - -QT_END_NAMESPACE - -class Dialog : public QDialog -{ - Q_OBJECT - -public: - explicit Dialog(QWidget *parent = nullptr); - -private slots: - void sendRequest(); - void readResponse(); - void processTimeout(); - -private: - void setControlsEnabled(bool enable); - void processError(const QString &error); - -private: - int m_transactionCount = 0; - QLabel *m_serialPortLabel = nullptr; - QComboBox *m_serialPortComboBox = nullptr; - QLabel *m_waitResponseLabel = nullptr; - QSpinBox *m_waitResponseSpinBox = nullptr; - QLabel *m_requestLabel = nullptr; - QLineEdit *m_requestLineEdit = nullptr; - QLabel *m_trafficLabel = nullptr; - QLabel *m_statusLabel = nullptr; - QPushButton *m_runButton = nullptr; - - QSerialPort m_serial; - QByteArray m_response; - QTimer m_timer; -}; - -#endif // DIALOG_H diff --git a/examples/serialport/sender/main.cpp b/examples/serialport/sender/main.cpp deleted file mode 100644 index f08a0eed..00000000 --- a/examples/serialport/sender/main.cpp +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (C) 2012 Denis Shienkov -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause - -#include "dialog.h" - -#include - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - Dialog dialog; - dialog.show(); - return app.exec(); -} diff --git a/examples/serialport/sender/sender.pro b/examples/serialport/sender/sender.pro deleted file mode 100644 index 83cb7be8..00000000 --- a/examples/serialport/sender/sender.pro +++ /dev/null @@ -1,15 +0,0 @@ -QT += widgets serialport -requires(qtConfig(combobox)) - -TARGET = sender -TEMPLATE = app - -HEADERS += \ - dialog.h - -SOURCES += \ - main.cpp \ - dialog.cpp - -target.path = $$[QT_INSTALL_EXAMPLES]/serialport/sender -INSTALLS += target diff --git a/examples/serialport/serialport.pro b/examples/serialport/serialport.pro index 98d9d7e0..ed6656b6 100644 --- a/examples/serialport/serialport.pro +++ b/examples/serialport/serialport.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs SUBDIRS = creaderasync cwriterasync -!isEmpty(QT.widgets.name):SUBDIRS += terminal blockingsender blockingreceiver sender receiver +!isEmpty(QT.widgets.name):SUBDIRS += terminal blockingsender blockingreceiver diff --git a/tests/manual/CMakeLists.txt b/tests/manual/CMakeLists.txt new file mode 100644 index 00000000..3057e843 --- /dev/null +++ b/tests/manual/CMakeLists.txt @@ -0,0 +1,7 @@ +# Copyright (C) 2023 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +if(TARGET Qt::Widgets) + add_subdirectory(examples/sender) + add_subdirectory(examples/receiver) +endif() diff --git a/tests/manual/examples/receiver/CMakeLists.txt b/tests/manual/examples/receiver/CMakeLists.txt new file mode 100644 index 00000000..0063c20d --- /dev/null +++ b/tests/manual/examples/receiver/CMakeLists.txt @@ -0,0 +1,26 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(receiver LANGUAGES CXX) + +find_package(Qt6 REQUIRED COMPONENTS Core Gui SerialPort Widgets) + +qt_standard_project_setup() + +qt_add_executable(receiver + dialog.cpp dialog.h + main.cpp +) + +set_target_properties(receiver PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_link_libraries(receiver PRIVATE + Qt::Core + Qt::Gui + Qt::SerialPort + Qt::Widgets +) diff --git a/tests/manual/examples/receiver/dialog.cpp b/tests/manual/examples/receiver/dialog.cpp new file mode 100644 index 00000000..51e18ea8 --- /dev/null +++ b/tests/manual/examples/receiver/dialog.cpp @@ -0,0 +1,106 @@ +// Copyright (C) 2012 Denis Shienkov +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include +#include +#include +#include +#include +#include +#include + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + m_serialPortLabel(new QLabel(tr("Serial port:"))), + m_serialPortComboBox(new QComboBox), + m_waitRequestLabel(new QLabel(tr("Wait request, msec:"))), + m_waitRequestSpinBox(new QSpinBox), + m_responseLabel(new QLabel(tr("Response:"))), + m_responseLineEdit(new QLineEdit(tr("Hello, I'm the receiver."))), + m_trafficLabel(new QLabel(tr("No traffic."))), + m_statusLabel(new QLabel(tr("Status: Not running."))), + m_runButton(new QPushButton(tr("Start"))) +{ + m_waitRequestSpinBox->setRange(0, 10000); + m_waitRequestSpinBox->setValue(20); + + const auto infos = QSerialPortInfo::availablePorts(); + for (const QSerialPortInfo &info : infos) + m_serialPortComboBox->addItem(info.portName()); + + auto mainLayout = new QGridLayout; + mainLayout->addWidget(m_serialPortLabel, 0, 0); + mainLayout->addWidget(m_serialPortComboBox, 0, 1); + mainLayout->addWidget(m_waitRequestLabel, 1, 0); + mainLayout->addWidget(m_waitRequestSpinBox, 1, 1); + mainLayout->addWidget(m_runButton, 0, 2, 2, 1); + mainLayout->addWidget(m_responseLabel, 2, 0); + mainLayout->addWidget(m_responseLineEdit, 2, 1, 1, 3); + mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4); + mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5); + setLayout(mainLayout); + + setWindowTitle(tr("Receiver")); + m_serialPortComboBox->setFocus(); + + m_timer.setSingleShot(true); + + connect(m_runButton, &QPushButton::clicked, this, &Dialog::startReceiver); + connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readRequest); + connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout); + + connect(m_serialPortComboBox, &QComboBox::currentIndexChanged, this, &Dialog::activateRunButton); + connect(m_waitRequestSpinBox, &QSpinBox::valueChanged, this, &Dialog::activateRunButton); + connect(m_responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton); +} + +void Dialog::startReceiver() +{ + if (m_serial.portName() != m_serialPortComboBox->currentText()) { + m_serial.close(); + m_serial.setPortName(m_serialPortComboBox->currentText()); + + if (!m_serial.open(QIODevice::ReadWrite)) { + processError(tr("Can't open %1, error code %2") + .arg(m_serial.portName()).arg(m_serial.error())); + return; + } + } + + m_runButton->setEnabled(false); + m_statusLabel->setText(tr("Status: Running, connected to port %1.") + .arg(m_serialPortComboBox->currentText())); +} + +void Dialog::readRequest() +{ + if (!m_timer.isActive()) + m_timer.start(m_waitRequestSpinBox->value()); + m_request.append(m_serial.readAll()); +} + +void Dialog::processTimeout() +{ + m_serial.write(m_responseLineEdit->text().toUtf8()); + + m_trafficLabel->setText(tr("Traffic, transaction #%1:" + "\n\r-request: %2" + "\n\r-response: %3") + .arg(++m_transactionCount).arg(QString::fromUtf8(m_request)) + .arg(m_responseLineEdit->text())); + m_request.clear(); +} + +void Dialog::activateRunButton() +{ + m_runButton->setEnabled(true); +} + +void Dialog::processError(const QString &s) +{ + activateRunButton(); + m_statusLabel->setText(tr("Status: Not running, %1.").arg(s)); + m_trafficLabel->setText(tr("No traffic.")); +} diff --git a/tests/manual/examples/receiver/dialog.h b/tests/manual/examples/receiver/dialog.h new file mode 100644 index 00000000..a32e1480 --- /dev/null +++ b/tests/manual/examples/receiver/dialog.h @@ -0,0 +1,54 @@ +// Copyright (C) 2012 Denis Shienkov +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QLabel; +class QLineEdit; +class QComboBox; +class QSpinBox; +class QPushButton; + +QT_END_NAMESPACE + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = nullptr); + +private slots: + void startReceiver(); + void readRequest(); + void processTimeout(); + void activateRunButton(); + +private: + void processError(const QString &s); + +private: + int m_transactionCount = 0; + QLabel *m_serialPortLabel = nullptr; + QComboBox *m_serialPortComboBox = nullptr; + QLabel *m_waitRequestLabel = nullptr; + QSpinBox *m_waitRequestSpinBox = nullptr; + QLabel *m_responseLabel = nullptr; + QLineEdit *m_responseLineEdit = nullptr; + QLabel *m_trafficLabel = nullptr; + QLabel *m_statusLabel = nullptr; + QPushButton *m_runButton = nullptr; + + QSerialPort m_serial; + QByteArray m_request; + QTimer m_timer; +}; + +#endif // DIALOG_H diff --git a/tests/manual/examples/receiver/main.cpp b/tests/manual/examples/receiver/main.cpp new file mode 100644 index 00000000..f08a0eed --- /dev/null +++ b/tests/manual/examples/receiver/main.cpp @@ -0,0 +1,14 @@ +// Copyright (C) 2012 Denis Shienkov +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Dialog dialog; + dialog.show(); + return app.exec(); +} diff --git a/tests/manual/examples/receiver/receiver.pro b/tests/manual/examples/receiver/receiver.pro new file mode 100644 index 00000000..d3180061 --- /dev/null +++ b/tests/manual/examples/receiver/receiver.pro @@ -0,0 +1,12 @@ +QT += widgets serialport +requires(qtConfig(combobox)) + +TARGET = receiver +TEMPLATE = app + +HEADERS += \ + dialog.h + +SOURCES += \ + main.cpp \ + dialog.cpp diff --git a/tests/manual/examples/sender/CMakeLists.txt b/tests/manual/examples/sender/CMakeLists.txt new file mode 100644 index 00000000..efbd5551 --- /dev/null +++ b/tests/manual/examples/sender/CMakeLists.txt @@ -0,0 +1,28 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(sender LANGUAGES CXX) + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/serialport/sender") + +find_package(Qt6 REQUIRED COMPONENTS Core Gui SerialPort Widgets) + +qt_standard_project_setup() + +qt_add_executable(sender + dialog.cpp dialog.h + main.cpp +) + +set_target_properties(sender PROPERTIES + WIN32_EXECUTABLE TRUE + MACOSX_BUNDLE TRUE +) + +target_link_libraries(sender PRIVATE + Qt::Core + Qt::Gui + Qt::SerialPort + Qt::Widgets +) diff --git a/tests/manual/examples/sender/dialog.cpp b/tests/manual/examples/sender/dialog.cpp new file mode 100644 index 00000000..d19ec888 --- /dev/null +++ b/tests/manual/examples/sender/dialog.cpp @@ -0,0 +1,107 @@ +// Copyright (C) 2012 Denis Shienkov +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include +#include +#include +#include +#include +#include +#include + +Dialog::Dialog(QWidget *parent) : + QDialog(parent), + m_transactionCount(0), + m_serialPortLabel(new QLabel(tr("Serial port:"))), + m_serialPortComboBox(new QComboBox), + m_waitResponseLabel(new QLabel(tr("Wait response, msec:"))), + m_waitResponseSpinBox(new QSpinBox), + m_requestLabel(new QLabel(tr("Request:"))), + m_requestLineEdit(new QLineEdit(tr("Who are you?"))), + m_trafficLabel(new QLabel(tr("No traffic."))), + m_statusLabel(new QLabel(tr("Status: Not running."))), + m_runButton(new QPushButton(tr("Start"))) +{ + const auto infos = QSerialPortInfo::availablePorts(); + for (const QSerialPortInfo &info : infos) + m_serialPortComboBox->addItem(info.portName()); + + m_waitResponseSpinBox->setRange(0, 10000); + m_waitResponseSpinBox->setValue(100); + + auto mainLayout = new QGridLayout; + mainLayout->addWidget(m_serialPortLabel, 0, 0); + mainLayout->addWidget(m_serialPortComboBox, 0, 1); + mainLayout->addWidget(m_waitResponseLabel, 1, 0); + mainLayout->addWidget(m_waitResponseSpinBox, 1, 1); + mainLayout->addWidget(m_runButton, 0, 2, 2, 1); + mainLayout->addWidget(m_requestLabel, 2, 0); + mainLayout->addWidget(m_requestLineEdit, 2, 1, 1, 3); + mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4); + mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5); + setLayout(mainLayout); + + setWindowTitle(tr("Sender")); + m_serialPortComboBox->setFocus(); + + m_timer.setSingleShot(true); + + connect(m_runButton, &QPushButton::clicked, this, &Dialog::sendRequest); + connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readResponse); + connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout); +} + +void Dialog::sendRequest() +{ + if (m_serial.portName() != m_serialPortComboBox->currentText()) { + m_serial.close(); + m_serial.setPortName(m_serialPortComboBox->currentText()); + + if (!m_serial.open(QIODevice::ReadWrite)) { + processError(tr("Can't open %1, error code %2") + .arg(m_serial.portName()).arg(m_serial.error())); + return; + } + } + + setControlsEnabled(false); + m_statusLabel->setText(tr("Status: Running, connected to port %1.") + .arg(m_serialPortComboBox->currentText())); + + m_serial.write(m_requestLineEdit->text().toUtf8()); + m_timer.start(m_waitResponseSpinBox->value()); +} + +void Dialog::readResponse() +{ + m_response.append(m_serial.readAll()); +} + +void Dialog::processTimeout() +{ + setControlsEnabled(true); + m_trafficLabel->setText(tr("Traffic, transaction #%1:" + "\n\r-request: %2" + "\n\r-response: %3") + .arg(++m_transactionCount) + .arg(m_requestLineEdit->text()) + .arg(QString::fromUtf8(m_response))); + m_response.clear(); +} + +void Dialog::processError(const QString &error) +{ + setControlsEnabled(true); + m_statusLabel->setText(tr("Status: Not running, %1.").arg(error)); + m_trafficLabel->setText(tr("No traffic.")); +} + +void Dialog::setControlsEnabled(bool enable) +{ + m_runButton->setEnabled(enable); + m_serialPortComboBox->setEnabled(enable); + m_waitResponseSpinBox->setEnabled(enable); + m_requestLineEdit->setEnabled(enable); +} diff --git a/tests/manual/examples/sender/dialog.h b/tests/manual/examples/sender/dialog.h new file mode 100644 index 00000000..0f049950 --- /dev/null +++ b/tests/manual/examples/sender/dialog.h @@ -0,0 +1,54 @@ +// Copyright (C) 2012 Denis Shienkov +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef DIALOG_H +#define DIALOG_H + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QLabel; +class QLineEdit; +class QSpinBox; +class QPushButton; +class QComboBox; + +QT_END_NAMESPACE + +class Dialog : public QDialog +{ + Q_OBJECT + +public: + explicit Dialog(QWidget *parent = nullptr); + +private slots: + void sendRequest(); + void readResponse(); + void processTimeout(); + +private: + void setControlsEnabled(bool enable); + void processError(const QString &error); + +private: + int m_transactionCount = 0; + QLabel *m_serialPortLabel = nullptr; + QComboBox *m_serialPortComboBox = nullptr; + QLabel *m_waitResponseLabel = nullptr; + QSpinBox *m_waitResponseSpinBox = nullptr; + QLabel *m_requestLabel = nullptr; + QLineEdit *m_requestLineEdit = nullptr; + QLabel *m_trafficLabel = nullptr; + QLabel *m_statusLabel = nullptr; + QPushButton *m_runButton = nullptr; + + QSerialPort m_serial; + QByteArray m_response; + QTimer m_timer; +}; + +#endif // DIALOG_H diff --git a/tests/manual/examples/sender/main.cpp b/tests/manual/examples/sender/main.cpp new file mode 100644 index 00000000..f08a0eed --- /dev/null +++ b/tests/manual/examples/sender/main.cpp @@ -0,0 +1,14 @@ +// Copyright (C) 2012 Denis Shienkov +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + Dialog dialog; + dialog.show(); + return app.exec(); +} diff --git a/tests/manual/examples/sender/sender.pro b/tests/manual/examples/sender/sender.pro new file mode 100644 index 00000000..f5fc421f --- /dev/null +++ b/tests/manual/examples/sender/sender.pro @@ -0,0 +1,12 @@ +QT += widgets serialport +requires(qtConfig(combobox)) + +TARGET = sender +TEMPLATE = app + +HEADERS += \ + dialog.h + +SOURCES += \ + main.cpp \ + dialog.cpp -- cgit v1.2.3