| 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_transactionCount(0), |
| 17 |
m_serialPortLabel(new QLabel(tr("Serial port:"))), |
| 18 |
m_serialPortComboBox(new QComboBox), |
| 19 |
m_waitResponseLabel(new QLabel(tr("Wait response, msec:"))), |
| 20 |
m_waitResponseSpinBox(new QSpinBox), |
| 21 |
m_requestLabel(new QLabel(tr("Request:"))), |
| 22 |
m_requestLineEdit(new QLineEdit(tr("Who are you?"))), |
| 23 |
m_trafficLabel(new QLabel(tr("No traffic."))), |
| 24 |
m_statusLabel(new QLabel(tr("Status: Not running."))), |
| 25 |
m_runButton(new QPushButton(tr("Start"))) |
| 26 |
{ |
| 27 |
const auto infos = QSerialPortInfo::availablePorts(); |
| 28 |
for (const QSerialPortInfo &info : infos) |
| 29 |
m_serialPortComboBox->addItem(info.portName()); |
| 30 |
|
| 31 |
m_waitResponseSpinBox->setRange(0, 10000); |
| 32 |
m_waitResponseSpinBox->setValue(100); |
| 33 |
|
| 34 |
auto mainLayout = new QGridLayout; |
| 35 |
mainLayout->addWidget(m_serialPortLabel, 0, 0); |
| 36 |
mainLayout->addWidget(m_serialPortComboBox, 0, 1); |
| 37 |
mainLayout->addWidget(m_waitResponseLabel, 1, 0); |
| 38 |
mainLayout->addWidget(m_waitResponseSpinBox, 1, 1); |
| 39 |
mainLayout->addWidget(m_runButton, 0, 2, 2, 1); |
| 40 |
mainLayout->addWidget(m_requestLabel, 2, 0); |
| 41 |
mainLayout->addWidget(m_requestLineEdit, 2, 1, 1, 3); |
| 42 |
mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4); |
| 43 |
mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5); |
| 44 |
setLayout(mainLayout); |
| 45 |
|
| 46 |
setWindowTitle(tr("Sender")); |
| 47 |
m_serialPortComboBox->setFocus(); |
| 48 |
|
| 49 |
m_timer.setSingleShot(true); |
| 50 |
|
| 51 |
connect(m_runButton, &QPushButton::clicked, this, &Dialog::sendRequest); |
| 52 |
connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readResponse); |
| 53 |
connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout); |
| 54 |
} |
| 55 |
|
| 56 |
void Dialog::sendRequest() |
| 57 |
{ |
| 58 |
if (m_serial.portName() != m_serialPortComboBox->currentText()) { |
| 59 |
m_serial.close(); |
| 60 |
m_serial.setPortName(m_serialPortComboBox->currentText()); |
| 61 |
|
| 62 |
if (!m_serial.open(QIODevice::ReadWrite)) { |
| 63 |
processError(tr("Can't open %1, error code %2") |
| 64 |
.arg(m_serial.portName()).arg(m_serial.error())); |
| 65 |
return; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
setControlsEnabled(false); |
| 70 |
m_statusLabel->setText(tr("Status: Running, connected to port %1.") |
| 71 |
.arg(m_serialPortComboBox->currentText())); |
| 72 |
|
| 73 |
m_serial.write(m_requestLineEdit->text().toUtf8()); |
| 74 |
m_timer.start(m_waitResponseSpinBox->value()); |
| 75 |
} |
| 76 |
|
| 77 |
void Dialog::readResponse() |
| 78 |
{ |
| 79 |
m_response.append(m_serial.readAll()); |
| 80 |
} |
| 81 |
|
| 82 |
void Dialog::processTimeout() |
| 83 |
{ |
| 84 |
setControlsEnabled(true); |
| 85 |
m_trafficLabel->setText(tr("Traffic, transaction #%1:" |
| 86 |
"\n\r-request: %2" |
| 87 |
"\n\r-response: %3") |
| 88 |
.arg(++m_transactionCount) |
| 89 |
.arg(m_requestLineEdit->text()) |
| 90 |
.arg(QString::fromUtf8(m_response))); |
| 91 |
m_response.clear(); |
| 92 |
} |
| 93 |
|
| 94 |
void Dialog::processError(const QString &error) |
| 95 |
{ |
| 96 |
setControlsEnabled(true); |
| 97 |
m_statusLabel->setText(tr("Status: Not running, %1.").arg(error)); |
| 98 |
m_trafficLabel->setText(tr("No traffic.")); |
| 99 |
} |
| 100 |
|
| 101 |
void Dialog::setControlsEnabled(bool enable) |
| 102 |
{ |
| 103 |
m_runButton->setEnabled(enable); |
| 104 |
m_serialPortComboBox->setEnabled(enable); |
| 105 |
m_waitResponseSpinBox->setEnabled(enable); |
| 106 |
m_requestLineEdit->setEnabled(enable); |
| 107 |
} |