| 1 |
// Copyright (C) 2012 Denis Shienkov <[email protected]> |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "dialog.h" |
| 5 |
|
| 6 |
#include <QComboBox> |
| 7 |
#include <QGridLayout> |
| 8 |
#include <QLabel> |
| 9 |
#include <QLineEdit> |
| 10 |
#include <QPushButton> |
| 11 |
#include <QSerialPortInfo> |
| 12 |
#include <QSpinBox> |
| 13 |
|
| 14 |
Dialog::Dialog(QWidget *parent) : |
| 15 |
QDialog(parent), |
| 16 |
m_serialPortLabel(new QLabel(tr("Serial port:"))), |
| 17 |
m_serialPortComboBox(new QComboBox), |
| 18 |
m_waitRequestLabel(new QLabel(tr("Wait request, msec:"))), |
| 19 |
m_waitRequestSpinBox(new QSpinBox), |
| 20 |
m_responseLabel(new QLabel(tr("Response:"))), |
| 21 |
m_responseLineEdit(new QLineEdit(tr("Hello, I'm the receiver."))), |
| 22 |
m_trafficLabel(new QLabel(tr("No traffic."))), |
| 23 |
m_statusLabel(new QLabel(tr("Status: Not running."))), |
| 24 |
m_runButton(new QPushButton(tr("Start"))) |
| 25 |
{ |
| 26 |
m_waitRequestSpinBox->setRange(0, 10000); |
| 27 |
m_waitRequestSpinBox->setValue(20); |
| 28 |
|
| 29 |
const auto infos = QSerialPortInfo::availablePorts(); |
| 30 |
for (const QSerialPortInfo &info : infos) |
| 31 |
m_serialPortComboBox->addItem(info.portName()); |
| 32 |
|
| 33 |
auto mainLayout = new QGridLayout; |
| 34 |
mainLayout->addWidget(m_serialPortLabel, 0, 0); |
| 35 |
mainLayout->addWidget(m_serialPortComboBox, 0, 1); |
| 36 |
mainLayout->addWidget(m_waitRequestLabel, 1, 0); |
| 37 |
mainLayout->addWidget(m_waitRequestSpinBox, 1, 1); |
| 38 |
mainLayout->addWidget(m_runButton, 0, 2, 2, 1); |
| 39 |
mainLayout->addWidget(m_responseLabel, 2, 0); |
| 40 |
mainLayout->addWidget(m_responseLineEdit, 2, 1, 1, 3); |
| 41 |
mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4); |
| 42 |
mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5); |
| 43 |
setLayout(mainLayout); |
| 44 |
|
| 45 |
setWindowTitle(tr("Receiver")); |
| 46 |
m_serialPortComboBox->setFocus(); |
| 47 |
|
| 48 |
m_timer.setSingleShot(true); |
| 49 |
|
| 50 |
connect(m_runButton, &QPushButton::clicked, this, &Dialog::startReceiver); |
| 51 |
connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readRequest); |
| 52 |
connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout); |
| 53 |
|
| 54 |
connect(m_serialPortComboBox, &QComboBox::currentIndexChanged, this, &Dialog::activateRunButton); |
| 55 |
connect(m_waitRequestSpinBox, &QSpinBox::valueChanged, this, &Dialog::activateRunButton); |
| 56 |
connect(m_responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton); |
| 57 |
} |
| 58 |
|
| 59 |
void Dialog::startReceiver() |
| 60 |
{ |
| 61 |
if (m_serial.portName() != m_serialPortComboBox->currentText()) { |
| 62 |
m_serial.close(); |
| 63 |
m_serial.setPortName(m_serialPortComboBox->currentText()); |
| 64 |
|
| 65 |
if (!m_serial.open(QIODevice::ReadWrite)) { |
| 66 |
processError(tr("Can't open %1, error code %2") |
| 67 |
.arg(m_serial.portName()).arg(m_serial.error())); |
| 68 |
return; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
m_runButton->setEnabled(false); |
| 73 |
m_statusLabel->setText(tr("Status: Running, connected to port %1.") |
| 74 |
.arg(m_serialPortComboBox->currentText())); |
| 75 |
} |
| 76 |
|
| 77 |
void Dialog::readRequest() |
| 78 |
{ |
| 79 |
if (!m_timer.isActive()) |
| 80 |
m_timer.start(m_waitRequestSpinBox->value()); |
| 81 |
m_request.append(m_serial.readAll()); |
| 82 |
} |
| 83 |
|
| 84 |
void Dialog::processTimeout() |
| 85 |
{ |
| 86 |
m_serial.write(m_responseLineEdit->text().toUtf8()); |
| 87 |
|
| 88 |
m_trafficLabel->setText(tr("Traffic, transaction #%1:" |
| 89 |
"\n\r-request: %2" |
| 90 |
"\n\r-response: %3") |
| 91 |
.arg(++m_transactionCount).arg(QString::fromUtf8(m_request)) |
| 92 |
.arg(m_responseLineEdit->text())); |
| 93 |
m_request.clear(); |
| 94 |
} |
| 95 |
|
| 96 |
void Dialog::activateRunButton() |
| 97 |
{ |
| 98 |
m_runButton->setEnabled(true); |
| 99 |
} |
| 100 |
|
| 101 |
void Dialog::processError(const QString &s) |
| 102 |
{ |
| 103 |
activateRunButton(); |
| 104 |
m_statusLabel->setText(tr("Status: Not running, %1.").arg(s)); |
| 105 |
m_trafficLabel->setText(tr("No traffic.")); |
| 106 |
} |