| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2012 Denis Shienkov <denis.shienkov@gmail.com> |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtSerialPort module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "dialog.h" |
| 52 | |
| 53 | #include <QComboBox> |
| 54 | #include <QGridLayout> |
| 55 | #include <QLabel> |
| 56 | #include <QLineEdit> |
| 57 | #include <QPushButton> |
| 58 | #include <QSerialPortInfo> |
| 59 | #include <QSpinBox> |
| 60 | |
| 61 | Dialog::Dialog(QWidget *parent) : |
| 62 | QDialog(parent), |
| 63 | m_transactionCount(0), |
| 64 | m_serialPortLabel(new QLabel(tr(s: "Serial port:" ))), |
| 65 | m_serialPortComboBox(new QComboBox), |
| 66 | m_waitResponseLabel(new QLabel(tr(s: "Wait response, msec:" ))), |
| 67 | m_waitResponseSpinBox(new QSpinBox), |
| 68 | m_requestLabel(new QLabel(tr(s: "Request:" ))), |
| 69 | m_requestLineEdit(new QLineEdit(tr(s: "Who are you?" ))), |
| 70 | m_trafficLabel(new QLabel(tr(s: "No traffic." ))), |
| 71 | m_statusLabel(new QLabel(tr(s: "Status: Not running." ))), |
| 72 | m_runButton(new QPushButton(tr(s: "Start" ))) |
| 73 | { |
| 74 | const auto infos = QSerialPortInfo::availablePorts(); |
| 75 | for (const QSerialPortInfo &info : infos) |
| 76 | m_serialPortComboBox->addItem(atext: info.portName()); |
| 77 | |
| 78 | m_waitResponseSpinBox->setRange(min: 0, max: 10000); |
| 79 | m_waitResponseSpinBox->setValue(100); |
| 80 | |
| 81 | auto mainLayout = new QGridLayout; |
| 82 | mainLayout->addWidget(m_serialPortLabel, row: 0, column: 0); |
| 83 | mainLayout->addWidget(m_serialPortComboBox, row: 0, column: 1); |
| 84 | mainLayout->addWidget(m_waitResponseLabel, row: 1, column: 0); |
| 85 | mainLayout->addWidget(m_waitResponseSpinBox, row: 1, column: 1); |
| 86 | mainLayout->addWidget(m_runButton, row: 0, column: 2, rowSpan: 2, columnSpan: 1); |
| 87 | mainLayout->addWidget(m_requestLabel, row: 2, column: 0); |
| 88 | mainLayout->addWidget(m_requestLineEdit, row: 2, column: 1, rowSpan: 1, columnSpan: 3); |
| 89 | mainLayout->addWidget(m_trafficLabel, row: 3, column: 0, rowSpan: 1, columnSpan: 4); |
| 90 | mainLayout->addWidget(m_statusLabel, row: 4, column: 0, rowSpan: 1, columnSpan: 5); |
| 91 | setLayout(mainLayout); |
| 92 | |
| 93 | setWindowTitle(tr(s: "Master" )); |
| 94 | m_serialPortComboBox->setFocus(); |
| 95 | |
| 96 | m_timer.setSingleShot(true); |
| 97 | |
| 98 | connect(sender: m_runButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::sendRequest); |
| 99 | connect(sender: &m_serial, signal: &QSerialPort::readyRead, receiver: this, slot: &Dialog::readResponse); |
| 100 | connect(sender: &m_timer, signal: &QTimer::timeout, receiver: this, slot: &Dialog::processTimeout); |
| 101 | } |
| 102 | |
| 103 | void Dialog::sendRequest() |
| 104 | { |
| 105 | if (m_serial.portName() != m_serialPortComboBox->currentText()) { |
| 106 | m_serial.close(); |
| 107 | m_serial.setPortName(m_serialPortComboBox->currentText()); |
| 108 | |
| 109 | if (!m_serial.open(mode: QIODevice::ReadWrite)) { |
| 110 | processError(error: tr(s: "Can't open %1, error code %2" ) |
| 111 | .arg(a: m_serial.portName()).arg(a: m_serial.error())); |
| 112 | return; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | setControlsEnabled(false); |
| 117 | m_statusLabel->setText(tr(s: "Status: Running, connected to port %1." ) |
| 118 | .arg(a: m_serialPortComboBox->currentText())); |
| 119 | |
| 120 | m_serial.write(data: m_requestLineEdit->text().toUtf8()); |
| 121 | m_timer.start(msec: m_waitResponseSpinBox->value()); |
| 122 | } |
| 123 | |
| 124 | void Dialog::readResponse() |
| 125 | { |
| 126 | m_response.append(a: m_serial.readAll()); |
| 127 | } |
| 128 | |
| 129 | void Dialog::processTimeout() |
| 130 | { |
| 131 | setControlsEnabled(true); |
| 132 | m_trafficLabel->setText(tr(s: "Traffic, transaction #%1:" |
| 133 | "\n\r-request: %2" |
| 134 | "\n\r-response: %3" ) |
| 135 | .arg(a: ++m_transactionCount) |
| 136 | .arg(a: m_requestLineEdit->text()) |
| 137 | .arg(a: QString::fromUtf8(str: m_response))); |
| 138 | m_response.clear(); |
| 139 | } |
| 140 | |
| 141 | void Dialog::processError(const QString &error) |
| 142 | { |
| 143 | setControlsEnabled(true); |
| 144 | m_statusLabel->setText(tr(s: "Status: Not running, %1." ).arg(a: error)); |
| 145 | m_trafficLabel->setText(tr(s: "No traffic." )); |
| 146 | } |
| 147 | |
| 148 | void Dialog::setControlsEnabled(bool enable) |
| 149 | { |
| 150 | m_runButton->setEnabled(enable); |
| 151 | m_serialPortComboBox->setEnabled(enable); |
| 152 | m_waitResponseSpinBox->setEnabled(enable); |
| 153 | m_requestLineEdit->setEnabled(enable); |
| 154 | } |
| 155 | |