-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
401 lines (347 loc) · 14.2 KB
/
mainwindow.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <Windows.h>
#include <Dbt.h>
#include "chartwindow.h"
static const char blankString[] = QT_TRANSLATE_NOOP("SettingsDialog", "N/A");
static const qint64 buffersize(2048);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
chart = new ChartWindow(this);
//serial port configure
connect(serial, static_cast<void(QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
this, &MainWindow::serialPortErrorHandle);
fillPortsParameters();
fillPortsInfo();
initConnections();
//receive data text editor configure
serial->setReadBufferSize(buffersize);
}
MainWindow::~MainWindow()
{
serial->close();
delete ui;
}
void MainWindow::fillPortsParameters()
{
//baudrate
ui->baudrateBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
ui->baudrateBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
ui->baudrateBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
ui->baudrateBox->addItem(QStringLiteral("57600"), QSerialPort::Baud57600);
ui->baudrateBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
//databits
ui->databitsBox->addItem(QStringLiteral("5"), QSerialPort::Data5);
ui->databitsBox->addItem(QStringLiteral("6"), QSerialPort::Data6);
ui->databitsBox->addItem(QStringLiteral("7"), QSerialPort::Data7);
ui->databitsBox->addItem(QStringLiteral("8"), QSerialPort::Data8);
ui->databitsBox->setCurrentIndex(3);
//parity
ui->parityBox->addItem(tr("None"), QSerialPort::NoParity);
ui->parityBox->addItem(tr("Even"), QSerialPort::EvenParity);
ui->parityBox->addItem(tr("Odd"), QSerialPort::OddParity);
ui->parityBox->addItem(tr("Mark"), QSerialPort::MarkParity);
ui->parityBox->addItem(tr("Space"), QSerialPort::SpaceParity);
//stopbits
ui->stopbitsBox->addItem(QStringLiteral("1"), QSerialPort::OneStop);
#ifdef Q_OS_WIN
ui->stopbitsBox->addItem(QStringLiteral("1.5"), QSerialPort::OneAndHalfStop);
#endif
ui->stopbitsBox->addItem(QStringLiteral("2"), QSerialPort::TwoStop);
}
void MainWindow::fillPortsInfo()
{
ui->serialPortBox->clear();
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos) {
QStringList list;
convertQSerialInfoToQSlist(info, list);
ui->serialPortBox->addItem(list.first(), list);
}
}
void MainWindow::showPortInfo(int idx)
{
if (idx == -1)
return;
QStringList list = ui->serialPortBox->itemData(idx).toStringList();
ui->descriptionLabel->setText(tr("Description: %1").arg(list.count() > 1 ?
list.at(1) : tr(blankString)));
ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.count() > 2 ?
list.at(2) : tr(blankString)));
ui->serialNumberLabel->setText(tr("Serial number: %1").arg(list.count() > 3 ?
list.at(3) : tr(blankString)));
ui->locationLabel->setText(tr("Location: %1").arg(list.count() > 4 ?
list.at(4) : tr(blankString)));
ui->viLabel->setText(tr("Vendor ID: %1").arg(list.count() > 5 ?
list.at(5) : tr(blankString)));
ui->piLabel->setText(tr("Product ID: %1").arg(list.count() > 6 ?
list.at(6) : tr(blankString)));
}
void MainWindow::clearPortInfo()
{
ui->descriptionLabel->setText(tr("Description: %1").arg(tr(blankString)));
ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(tr(blankString)));
ui->serialNumberLabel->setText(tr("Serial number: %1").arg(tr(blankString)));
ui->locationLabel->setText(tr("Location: %1").arg(tr(blankString)));
ui->viLabel->setText(tr("Vendor ID: %1").arg(tr(blankString)));
ui->piLabel->setText(tr("Product ID: %1").arg(tr(blankString)));
}
void MainWindow::initConnections()
{
//receive connection init
QObject::connect(ui->clearReceiveButton, &QPushButton::clicked,
ui->receiveTextEdit, &QPlainTextEdit::clear);
QObject::connect(ui->hexCheckBoxR, &QCheckBox::stateChanged,
this, &MainWindow::rcvTextFormSwitch);
if (ui->hexCheckBoxR->isChecked()) {
QObject::connect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialDataToRcvTextHex);
} else {
QObject::connect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialDataToRcvTextChar);
}
//send connection init
QObject::connect(ui->clearSendButton, &QPushButton::clicked,
ui->sendTextEdit, &QPlainTextEdit::clear);
QObject::connect(ui->hexCheckBoxS, &QCheckBox::stateChanged,
this, &MainWindow::sendTextFormSwitch);
if (ui->hexCheckBoxS->isChecked()) {
QObject::connect(ui->sendDataButton, &QPushButton::clicked,
this, &MainWindow::sendDataToserialHex);
} else {
QObject::connect(ui->sendDataButton, &QPushButton::clicked,
this, &MainWindow::sendDataToserialChar);
}
//chart connection init
connect(this, &MainWindow::sendToChart,
chart, &ChartWindow::writeData, Qt::UniqueConnection);
connect(ui->showChartButton, &QPushButton::clicked,
chart, &ChartWindow::show);
}
void MainWindow::on_connectionButton_clicked()
{
if (serial->isOpen()) {
closeSerialport();
} else {
openSerialport();
}
}
void MainWindow::on_baudrateBox_currentIndexChanged(int index)
{
serial->setBaudRate(static_cast<QSerialPort::BaudRate>
(ui->baudrateBox->itemData(index).toInt()));
}
void MainWindow::on_databitsBox_currentIndexChanged(int index)
{
serial->setDataBits(static_cast<QSerialPort::DataBits>
(ui->databitsBox->itemData(index).toInt()));
}
void MainWindow::on_parityBox_currentIndexChanged(int index)
{
serial->setParity(static_cast<QSerialPort::Parity>
(ui->parityBox->itemData(index).toInt()));
}
void MainWindow::on_stopbitsBox_currentIndexChanged(int index)
{
serial->setStopBits(static_cast<QSerialPort::StopBits>
(ui->stopbitsBox->itemData(index).toInt()));
}
void MainWindow::on_serialPortBox_currentIndexChanged(int index)
{
MainWindow::showPortInfo(index);
}
void MainWindow::serialPortErrorHandle(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
MainWindow::closeSerialport();
}
}
//return false if sucess other true
void MainWindow::openSerialport()
{
bool ret;
serial->setPortName(ui->serialPortBox->currentText());
serial->setBaudRate(static_cast<QSerialPort::BaudRate>
(ui->baudrateBox->currentData().toInt()));
serial->setDataBits(static_cast<QSerialPort::DataBits>
(ui->databitsBox->currentData().toInt()));
serial->setParity(static_cast<QSerialPort::Parity>
(ui->parityBox->currentData().toInt()));
serial->setStopBits(static_cast<QSerialPort::StopBits>
(ui->stopbitsBox->currentData().toInt()));
ret = serial->open(QIODevice::ReadWrite);
if (ret) {
ui->serialPortBox->setDisabled(true);
ui->connectionButton->setText(QStringLiteral("Disconnect"));
ui->sendDataButton->setEnabled(true);
ui->connectionButton->setCheckable(true);
ui->connectionButton->setChecked(true);
} else {
ui->connectionButton->setCheckable(false);
}
}
void MainWindow::closeSerialport()
{
serial->close();
ui->sendDataButton->setEnabled(false);
ui->connectionButton->setChecked(false);
ui->serialPortBox->setEnabled(true);
ui->connectionButton->setText(QStringLiteral("Connect"));
}
bool MainWindow::nativeEvent(const QByteArray &, void *message, long *)
{
MSG *msg = reinterpret_cast<MSG*>(message);
PDEV_BROADCAST_HDR phdr = reinterpret_cast<PDEV_BROADCAST_HDR>(msg->lParam);
if (msg->message == WM_DEVICECHANGE) {
switch (msg->wParam) {
case DBT_DEVICEARRIVAL:
if (phdr->dbch_devicetype == DBT_DEVTYP_PORT) {
PDEV_BROADCAST_PORT pPort = reinterpret_cast<PDEV_BROADCAST_PORT>
(phdr);
const QSerialPortInfo serialPortInfo(QString::fromWCharArray
(pPort->dbcp_name));
QStringList list;
convertQSerialInfoToQSlist(serialPortInfo, list);
ui->serialPortBox->addItem(list.first(), list);
// qDebug() << "port arrival: "
// << QString::fromWCharArray(pPort->dbcp_name);
}
break;
case DBT_DEVICEREMOVECOMPLETE:
if (phdr->dbch_devicetype == DBT_DEVTYP_PORT) {
PDEV_BROADCAST_PORT pPort = reinterpret_cast<PDEV_BROADCAST_PORT>
(phdr);
int index = ui->serialPortBox->findText(QString::fromWCharArray
(pPort->dbcp_name));
ui->serialPortBox->removeItem(index);
if (ui->serialPortBox->count() == 0)
clearPortInfo();
}
break;
}
}
return false;
}
void MainWindow::convertQSerialInfoToQSlist(const QSerialPortInfo &info, QStringList &list)
{
QString description;
QString manufacturer;
QString serialNumber;
description = info.description();
manufacturer = info.manufacturer();
serialNumber = info.serialNumber();
list << info.portName()
<< (!description.isEmpty() ? description : blankString)
<< (!manufacturer.isEmpty() ? manufacturer : blankString)
<< (!serialNumber.isEmpty() ? serialNumber : blankString)
<< info.systemLocation()
<< (info.vendorIdentifier() ?
QString::number(info.vendorIdentifier(), 16) : blankString)
<< (info.productIdentifier() ?
QString::number(info.productIdentifier(), 16) : blankString);
}
void MainWindow::serialDataToRcvTextChar()
{
QString string(serial->readAll());
smartInserText(string);
}
void MainWindow::serialDataToRcvTextHex()
{
QByteArray data(serial->readAll());
QString string;
for(int i = 0; i < data.size(); ++i) {
string.append(QByteArray(1, data.at(i)).toHex().toUpper());
string.append(' ');
}
smartInserText(string);
emit sendToChart(data);
}
void MainWindow::smartInserText(const QString &text)
{
QTextCursor prevCursor(ui->receiveTextEdit->textCursor());
bool scrollbar;
QScrollBar *bar = ui->receiveTextEdit->verticalScrollBar();
int barValue = bar->value();
if (bar->value() == bar->maximum()) {
scrollbar = true;
} else {
scrollbar = false;
}
int size(ui->receiveTextEdit->toPlainText().size() + text.size());
if (size > 1000) {
QTextCursor cursor(ui->receiveTextEdit->textCursor());
cursor.movePosition(QTextCursor::Start);
cursor.setPosition(size - 1000, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
ui->receiveTextEdit->moveCursor(QTextCursor::End);
ui->receiveTextEdit->insertPlainText(text);
ui->receiveTextEdit->setTextCursor(prevCursor);
if (scrollbar) {
bar->setValue(bar->maximum());
} else {
bar->setValue(barValue);
}
}
void MainWindow::rcvTextFormSwitch(int state)
{
if (state == Qt::Unchecked) {
QObject::disconnect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialDataToRcvTextHex);
QObject::connect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialDataToRcvTextChar);
} else if (state == Qt::Checked) {
QObject::disconnect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialDataToRcvTextChar);
QObject::connect(serial, &QSerialPort::readyRead,
this, &MainWindow::serialDataToRcvTextHex);
}
}
void MainWindow::sendDataToserialChar()
{
QString string(ui->sendTextEdit->toPlainText());
serial->write(&string.toStdString()[0], string.size());
}
void MainWindow::sendDataToserialHex()
{
QString string(ui->sendTextEdit->toPlainText());
QRegularExpression re("[^0-9a-fA-F\\s,]");
QRegularExpressionMatch match = re.match(string);
if (match.hasMatch()) {
QMessageBox::critical(this, tr("Format Error"),
QStringLiteral("invalid value!"));
return;
}
QStringList parts;
QRegularExpressionMatchIterator i = QRegularExpression("[^\\s,]{1,2}").globalMatch(string);
while (i.hasNext()) {
parts << i.next().captured(0);
}
QByteArray hexArray;
for (QString str: parts) {
uchar hexChar;
hexChar = str.toInt(nullptr, 16);
hexArray.append(hexChar);
}
serial->write(hexArray);
}
void MainWindow::sendTextFormSwitch(int state)
{
if (state == Qt::Unchecked) {
QObject::disconnect(ui->sendDataButton, &QPushButton::clicked,
this, &MainWindow::sendDataToserialHex);
QObject::connect(ui->sendDataButton, &QPushButton::clicked,
this, &MainWindow::sendDataToserialChar);
} else if (state == Qt::Checked) {
QObject::disconnect(ui->sendDataButton, &QPushButton::clicked,
this, &MainWindow::sendDataToserialChar);
QObject::connect(ui->sendDataButton, &QPushButton::clicked,
this, &MainWindow::sendDataToserialHex);
}
}