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(20); |
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: "Slave" )); |
93 | m_serialPortComboBox->setFocus(); |
94 | |
95 | m_timer.setSingleShot(true); |
96 | |
97 | connect(sender: m_runButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::startSlave); |
98 | connect(sender: &m_serial, signal: &QSerialPort::readyRead, receiver: this, slot: &Dialog::readRequest); |
99 | connect(sender: &m_timer, signal: &QTimer::timeout, receiver: this, slot: &Dialog::processTimeout); |
100 | |
101 | connect(sender: m_serialPortComboBox, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), |
102 | receiver: this, slot: &Dialog::activateRunButton); |
103 | connect(sender: m_waitRequestSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged), |
104 | receiver: this, slot: &Dialog::activateRunButton); |
105 | connect(sender: m_responseLineEdit, signal: &QLineEdit::textChanged, receiver: this, slot: &Dialog::activateRunButton); |
106 | } |
107 | |
108 | void Dialog::startSlave() |
109 | { |
110 | if (m_serial.portName() != m_serialPortComboBox->currentText()) { |
111 | m_serial.close(); |
112 | m_serial.setPortName(m_serialPortComboBox->currentText()); |
113 | |
114 | if (!m_serial.open(mode: QIODevice::ReadWrite)) { |
115 | processError(s: tr(s: "Can't open %1, error code %2" ) |
116 | .arg(a: m_serial.portName()).arg(a: m_serial.error())); |
117 | return; |
118 | } |
119 | } |
120 | |
121 | m_runButton->setEnabled(false); |
122 | m_statusLabel->setText(tr(s: "Status: Running, connected to port %1." ) |
123 | .arg(a: m_serialPortComboBox->currentText())); |
124 | } |
125 | |
126 | void Dialog::readRequest() |
127 | { |
128 | if (!m_timer.isActive()) |
129 | m_timer.start(msec: m_waitRequestSpinBox->value()); |
130 | m_request.append(a: m_serial.readAll()); |
131 | } |
132 | |
133 | void Dialog::processTimeout() |
134 | { |
135 | m_serial.write(data: m_responseLineEdit->text().toUtf8()); |
136 | |
137 | m_trafficLabel->setText(tr(s: "Traffic, transaction #%1:" |
138 | "\n\r-request: %2" |
139 | "\n\r-response: %3" ) |
140 | .arg(a: ++m_transactionCount).arg(a: QString::fromUtf8(str: m_request)) |
141 | .arg(a: m_responseLineEdit->text())); |
142 | m_request.clear(); |
143 | } |
144 | |
145 | void Dialog::activateRunButton() |
146 | { |
147 | m_runButton->setEnabled(true); |
148 | } |
149 | |
150 | void Dialog::processError(const QString &s) |
151 | { |
152 | activateRunButton(); |
153 | m_statusLabel->setText(tr(s: "Status: Not running, %1." ).arg(a: s)); |
154 | m_trafficLabel->setText(tr(s: "No traffic." )); |
155 | } |
156 | |