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
61Dialog::Dialog(QWidget *parent) :
62 QDialog(parent),
63 m_serialPortLabel(new QLabel(tr(s: "Serial port:"))),
64 m_serialPortComboBox(new QComboBox),
65 m_waitResponseLabel(new QLabel(tr(s: "Wait response, msec:"))),
66 m_waitResponseSpinBox(new QSpinBox),
67 m_requestLabel(new QLabel(tr(s: "Request:"))),
68 m_requestLineEdit(new QLineEdit(tr(s: "Who are you?"))),
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 const auto infos = QSerialPortInfo::availablePorts();
74 for (const QSerialPortInfo &info : infos)
75 m_serialPortComboBox->addItem(atext: info.portName());
76
77 m_waitResponseSpinBox->setRange(min: 0, max: 10000);
78 m_waitResponseSpinBox->setValue(1000);
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_waitResponseLabel, row: 1, column: 0);
84 mainLayout->addWidget(m_waitResponseSpinBox, row: 1, column: 1);
85 mainLayout->addWidget(m_runButton, row: 0, column: 2, rowSpan: 2, columnSpan: 1);
86 mainLayout->addWidget(m_requestLabel, row: 2, column: 0);
87 mainLayout->addWidget(m_requestLineEdit, 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 Master"));
93 m_serialPortComboBox->setFocus();
94
95 connect(sender: m_runButton, signal: &QPushButton::clicked, receiver: this, slot: &Dialog::transaction);
96 connect(sender: &m_thread, signal: &MasterThread::response, receiver: this, slot: &Dialog::showResponse);
97 connect(sender: &m_thread, signal: &MasterThread::error, receiver: this, slot: &Dialog::processError);
98 connect(sender: &m_thread, signal: &MasterThread::timeout, receiver: this, slot: &Dialog::processTimeout);
99}
100
101void Dialog::transaction()
102{
103 setControlsEnabled(false);
104 m_statusLabel->setText(tr(s: "Status: Running, connected to port %1.")
105 .arg(a: m_serialPortComboBox->currentText()));
106 m_thread.transaction(portName: m_serialPortComboBox->currentText(),
107 waitTimeout: m_waitResponseSpinBox->value(),
108 request: m_requestLineEdit->text());
109}
110
111void Dialog::showResponse(const QString &s)
112{
113 setControlsEnabled(true);
114 m_trafficLabel->setText(tr(s: "Traffic, transaction #%1:"
115 "\n\r-request: %2"
116 "\n\r-response: %3")
117 .arg(a: ++m_transactionCount)
118 .arg(a: m_requestLineEdit->text())
119 .arg(a: s));
120}
121
122void Dialog::processError(const QString &s)
123{
124 setControlsEnabled(true);
125 m_statusLabel->setText(tr(s: "Status: Not running, %1.").arg(a: s));
126 m_trafficLabel->setText(tr(s: "No traffic."));
127}
128
129void Dialog::processTimeout(const QString &s)
130{
131 setControlsEnabled(true);
132 m_statusLabel->setText(tr(s: "Status: Running, %1.").arg(a: s));
133 m_trafficLabel->setText(tr(s: "No traffic."));
134}
135
136void Dialog::setControlsEnabled(bool enable)
137{
138 m_runButton->setEnabled(enable);
139 m_serialPortComboBox->setEnabled(enable);
140 m_waitResponseSpinBox->setEnabled(enable);
141 m_requestLineEdit->setEnabled(enable);
142}
143

source code of qtserialport/examples/serialport/blockingmaster/dialog.cpp