diff options
Diffstat (limited to 'tests/manual')
-rw-r--r-- | tests/manual/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tests/manual/examples/receiver/CMakeLists.txt | 26 | ||||
-rw-r--r-- | tests/manual/examples/receiver/dialog.cpp | 106 | ||||
-rw-r--r-- | tests/manual/examples/receiver/dialog.h | 54 | ||||
-rw-r--r-- | tests/manual/examples/receiver/main.cpp | 14 | ||||
-rw-r--r-- | tests/manual/examples/receiver/receiver.pro | 12 | ||||
-rw-r--r-- | tests/manual/examples/sender/CMakeLists.txt | 28 | ||||
-rw-r--r-- | tests/manual/examples/sender/dialog.cpp | 107 | ||||
-rw-r--r-- | tests/manual/examples/sender/dialog.h | 54 | ||||
-rw-r--r-- | tests/manual/examples/sender/main.cpp | 14 | ||||
-rw-r--r-- | tests/manual/examples/sender/sender.pro | 12 |
11 files changed, 434 insertions, 0 deletions
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 <denis.shienkov@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include <QComboBox> +#include <QGridLayout> +#include <QLabel> +#include <QLineEdit> +#include <QPushButton> +#include <QSerialPortInfo> +#include <QSpinBox> + +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 <denis.shienkov@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef DIALOG_H +#define DIALOG_H + +#include <QDialog> +#include <QSerialPort> +#include <QTimer> + +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 <denis.shienkov@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include <QApplication> + +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 <denis.shienkov@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include <QComboBox> +#include <QGridLayout> +#include <QLabel> +#include <QLineEdit> +#include <QPushButton> +#include <QSerialPortInfo> +#include <QSpinBox> + +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 <denis.shienkov@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#ifndef DIALOG_H +#define DIALOG_H + +#include <QDialog> +#include <QSerialPort> +#include <QTimer> + +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 <denis.shienkov@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include "dialog.h" + +#include <QApplication> + +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 |