Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "qextserialport.h"
#include "qextserialenumerator.h"
#include "dialog.h"
#include "ui_dialog.h"
#include <QtCore>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
//! [0]
foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
ui->portBox->addItem(info.portName);
//make sure user can input their own port name!
ui->portBox->setEditable(true);
ui->baudRateBox->addItem("1200", BAUD1200);
ui->baudRateBox->addItem("2400", BAUD2400);
ui->baudRateBox->addItem("4800", BAUD4800);
ui->baudRateBox->addItem("9600", BAUD9600);
ui->baudRateBox->addItem("19200", BAUD19200);
ui->baudRateBox->setCurrentIndex(3);
ui->parityBox->addItem("NONE", PAR_NONE);
ui->parityBox->addItem("ODD", PAR_ODD);
ui->parityBox->addItem("EVEN", PAR_EVEN);
ui->dataBitsBox->addItem("5", DATA_5);
ui->dataBitsBox->addItem("6", DATA_6);
ui->dataBitsBox->addItem("7", DATA_7);
ui->dataBitsBox->addItem("8", DATA_8);
ui->dataBitsBox->setCurrentIndex(3);
ui->stopBitsBox->addItem("1", STOP_1);
ui->stopBitsBox->addItem("2", STOP_2);
ui->queryModeBox->addItem("Polling", QextSerialPort::Polling);
ui->queryModeBox->addItem("EventDriven", QextSerialPort::EventDriven);
//! [0]
ui->led->turnOff();
timer = new QTimer(this);
timer->setInterval(40);
//! [1]
PortSettings settings = {BAUD9600, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
port = new QextSerialPort(ui->portBox->currentText(), settings, QextSerialPort::Polling);
//! [1]
enumerator = new QextSerialEnumerator(this);
enumerator->setUpNotifications();
connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)), SLOT(onBaudRateChanged(int)));
connect(ui->parityBox, SIGNAL(currentIndexChanged(int)), SLOT(onParityChanged(int)));
connect(ui->dataBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onDataBitsChanged(int)));
connect(ui->stopBitsBox, SIGNAL(currentIndexChanged(int)), SLOT(onStopBitsChanged(int)));
connect(ui->queryModeBox, SIGNAL(currentIndexChanged(int)), SLOT(onQueryModeChanged(int)));
connect(ui->timeoutBox, SIGNAL(valueChanged(int)), SLOT(onTimeoutChanged(int)));
connect(ui->portBox, SIGNAL(editTextChanged(QString)), SLOT(onPortNameChanged(QString)));
connect(ui->openCloseButton, SIGNAL(clicked()), SLOT(onOpenCloseButtonClicked()));
connect(ui->sendButton, SIGNAL(clicked()), SLOT(onSendButtonClicked()));
connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
connect(port, SIGNAL(readyRead()), SLOT(onReadyRead()));
connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
setWindowTitle(tr("QextSerialPort Demo"));
}
Dialog::~Dialog()
{
delete ui;
delete port;
}
void Dialog::changeEvent(QEvent *e)
{
QDialog::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void Dialog::onPortNameChanged(const QString & /*name*/)
{
if (port->isOpen()) {
port->close();
ui->led->turnOff();
}
}
//! [2]
void Dialog::onBaudRateChanged(int idx)
{
port->setBaudRate((BaudRateType)ui->baudRateBox->itemData(idx).toInt());
}
void Dialog::onParityChanged(int idx)
{
port->setParity((ParityType)ui->parityBox->itemData(idx).toInt());
}
void Dialog::onDataBitsChanged(int idx)
{
port->setDataBits((DataBitsType)ui->dataBitsBox->itemData(idx).toInt());
}
void Dialog::onStopBitsChanged(int idx)
{
port->setStopBits((StopBitsType)ui->stopBitsBox->itemData(idx).toInt());
}
void Dialog::onQueryModeChanged(int idx)
{
port->setQueryMode((QextSerialPort::QueryMode)ui->queryModeBox->itemData(idx).toInt());
}
void Dialog::onTimeoutChanged(int val)
{
port->setTimeout(val);
}
//! [2]
//! [3]
void Dialog::onOpenCloseButtonClicked()
{
if (!port->isOpen()) {
port->setPortName(ui->portBox->currentText());
port->open(QIODevice::ReadWrite);
}
else {
port->close();
}
//If using polling mode, we need a QTimer
if (port->isOpen() && port->queryMode() == QextSerialPort::Polling)
timer->start();
else
timer->stop();
//update led's status
ui->led->turnOn(port->isOpen());
}
//! [3]
//! [4]
void Dialog::onSendButtonClicked()
{
if (port->isOpen() && !ui->sendEdit->toPlainText().isEmpty())
port->write(ui->sendEdit->toPlainText().toLatin1());
}
void Dialog::onReadyRead()
{
if (port->bytesAvailable()) {
ui->recvEdit->moveCursor(QTextCursor::End);
ui->recvEdit->insertPlainText(QString::fromLatin1(port->readAll()));
}
}
void Dialog::onPortAddedOrRemoved()
{
QString current = ui->portBox->currentText();
ui->portBox->blockSignals(true);
ui->portBox->clear();
foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
ui->portBox->addItem(info.portName);
ui->portBox->setCurrentIndex(ui->portBox->findText(current));
ui->portBox->blockSignals(false);
}
//! [4]