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_serialPortLabel(new QLabel(tr(s: "Serial port:" ))), |
64 | m_serialPortComboBox(new QComboBox), |
65 | m_waitRequestLabel(new QLabel(tr(s: "Wait request, msec:" ))), |
66 | m_waitRequestSpinBox(new QSpinBox), |
67 | m_responseLabel(new QLabel(tr(s: "Response:" ))), |
68 | m_responseLineEdit(new QLineEdit(tr(s: "Hello, I'm Slave." ))), |
69 | m_trafficLabel(new QLabel(tr(s: "No traffic." ))), |
70 | m_statusLabel(new QLabel(tr(s: "Status: Not running." ))), |
71 | m_runButton(new QPushButton(tr(s: "Start" ))) |
72 | { |
73 | m_waitRequestSpinBox->setRange(min: 0, max: 10000); |
74 | m_waitRequestSpinBox->setValue(10000); |
75 | |
76 | const auto infos = QSerialPortInfo::availablePorts(); |
77 | for (const QSerialPortInfo &info : infos) |
78 | m_serialPortComboBox->addItem(atext: info.portName()); |
79 | |
80 | auto mainLayout = new QGridLayout; |
81 | mainLayout->addWidget(m_serialPortLabel, row: 0, column: 0); |
82 | mainLayout->addWidget(m_serialPortComboBox, row: 0, column: 1); |
83 | mainLayout->addWidget(m_waitRequestLabel, row: 1, column: 0); |
84 | mainLayout->addWidget(m_waitRequestSpinBox, row: 1, column: 1); |
85 | mainLayout->addWidget(m_runButton, row: 0, column: 2, rowSpan: 2, columnSpan: 1); |
86 | mainLayout->addWidget(m_responseLabel, row: 2, column: 0); |
87 | mainLayout->addWidget(m_responseLineEdit, row: 2, column: 1, rowSpan: 1, columnSpan: 3); |
88 | mainLayout->addWidget(m_trafficLabel, row: 3, column: 0, rowSpan: 1, columnSpan: 4); |
89 | mainLayout->addWidget(m_statusLabel, row: 4, column: 0, rowSpan: 1, columnSpan: 5); |
90 | setLayout(mainLayout); |
91 | |
92 | setWindowTitle(tr(s: "Blocking Slave" )); |
93 | m_serialPortComboBox->setFocus(); |
94 | |
95 | connect(sender: m_runButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::startSlave); |
96 | connect(sender: &m_thread, signal: &SlaveThread::request, receiver: this,slot: &Dialog::showRequest); |
97 | connect(sender: &m_thread, signal: &SlaveThread::error, receiver: this, slot: &Dialog::processError); |
98 | connect(sender: &m_thread, signal: &SlaveThread::timeout, receiver: this, slot: &Dialog::processTimeout); |
99 | |
100 | connect(sender: m_serialPortComboBox, signal: QOverload<const QString &>::of(ptr: &QComboBox::currentIndexChanged), |
101 | receiver: this, slot: &Dialog::activateRunButton); |
102 | connect(sender: m_waitRequestSpinBox, signal: &QSpinBox::textChanged, receiver: this, slot: &Dialog::activateRunButton); |
103 | connect(sender: m_responseLineEdit, signal: &QLineEdit::textChanged, receiver: this, slot: &Dialog::activateRunButton); |
104 | } |
105 | |
106 | void Dialog::startSlave() |
107 | { |
108 | m_runButton->setEnabled(false); |
109 | m_statusLabel->setText(tr(s: "Status: Running, connected to port %1." ) |
110 | .arg(a: m_serialPortComboBox->currentText())); |
111 | m_thread.startSlave(portName: m_serialPortComboBox->currentText(), |
112 | waitTimeout: m_waitRequestSpinBox->value(), |
113 | response: m_responseLineEdit->text()); |
114 | } |
115 | |
116 | void Dialog::showRequest(const QString &s) |
117 | { |
118 | m_trafficLabel->setText(tr(s: "Traffic, transaction #%1:" |
119 | "\n\r-request: %2" |
120 | "\n\r-response: %3" ) |
121 | .arg(a: ++m_transactionCount) |
122 | .arg(a: s) |
123 | .arg(a: m_responseLineEdit->text())); |
124 | } |
125 | |
126 | void Dialog::processError(const QString &s) |
127 | { |
128 | activateRunButton(); |
129 | m_statusLabel->setText(tr(s: "Status: Not running, %1." ).arg(a: s)); |
130 | m_trafficLabel->setText(tr(s: "No traffic." )); |
131 | } |
132 | |
133 | void Dialog::processTimeout(const QString &s) |
134 | { |
135 | m_statusLabel->setText(tr(s: "Status: Running, %1." ).arg(a: s)); |
136 | m_trafficLabel->setText(tr(s: "No traffic." )); |
137 | } |
138 | |
139 | void Dialog::activateRunButton() |
140 | { |
141 | m_runButton->setEnabled(true); |
142 | } |
143 | |