-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
822 lines (733 loc) · 28.8 KB
/
mainwindow.cpp
File metadata and controls
822 lines (733 loc) · 28.8 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QImageReader>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QDateTime>
#include <QtSerialPort/QSerialPortInfo>
#include <QSettings>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
/**
* @brief getDateFromMacro
* @param time __DATE__
* @return
*/
static time_t getDateFromMacro(char const *time) {
char s_month[5];
int month, day, year;
struct tm t;
memset(&t, 0, sizeof(tm));
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(time, "%s %d %d", s_month, &day, &year);
month = (strstr(month_names, s_month)-month_names)/3;
t.tm_mon = month;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_isdst = -1;
return mktime(&t);
}
MainWindow::Settings MainWindow::doSettings(bool isWrite, Settings inSettings)
{
Settings in = inSettings;
Settings out;
QSettings settings("Yzs_think", "Application");
if(isWrite) {
settings.setValue("name", in.name);
settings.setValue("baudRate", in.baudRate);
settings.setValue("stringBaudRate", in.stringBaudRate);
settings.setValue("dataBits", in.dataBits);
settings.setValue("stringDataBits", in.stringDataBits);
settings.setValue("parity", in.parity);
settings.setValue("stringParity", in.stringParity);
settings.setValue("stopBits", in.stopBits);
settings.setValue("stringStopBits", in.stringStopBits);
settings.setValue("flowControl", in.flowControl);
settings.setValue("stringFlowControl", in.stringFlowControl);
settings.setValue("sendNewLineEnabled", in.sendNewLineEnabled);
settings.setValue("stringStatus", in.stringStatus);
settings.setValue("isDtr", in.isDtr);
settings.setValue("isRts", in.isRts);
settings.setValue("isHexDisplay", in.isHexDisplay);
settings.setValue("isHexSend", in.isHexSend);
// 和Windows版本同步,不保存定时发送开关
settings.setValue("isTimerSend", DEF_SETTINGS.isTimerSend);
settings.setValue("timeTimerSend", in.timerLength);
settings.setValue("sendCache", in.sendCache);
} else {
out.name = settings.value("name", DEF_SETTINGS.name).toString();
out.baudRate = (QSerialPort::BaudRate)settings.value("baudRate", DEF_SETTINGS.baudRate).toInt();
out.stringBaudRate = settings.value("stringBaudRate", DEF_SETTINGS.stringBaudRate).toString();
out.dataBits = (QSerialPort::DataBits)settings.value("dataBits", DEF_SETTINGS.dataBits).toInt();
out.stringDataBits = settings.value("stringDataBits", DEF_SETTINGS.stringDataBits).toString();
out.parity = (QSerialPort::Parity)settings.value("parity", DEF_SETTINGS.parity).toInt();
out.stringParity = settings.value("stringParity", DEF_SETTINGS.stringParity).toString();
out.stopBits = (QSerialPort::StopBits)settings.value("stopBits", DEF_SETTINGS.stopBits).toInt();
out.stringStopBits = settings.value("stringStopBits", DEF_SETTINGS.stringStopBits).toString();
out.flowControl = (QSerialPort::FlowControl)settings.value("flowControl", DEF_SETTINGS.flowControl).toInt();
out.stringFlowControl = settings.value("stringFlowControl", DEF_SETTINGS.stringFlowControl).toString();
out.sendNewLineEnabled = settings.value("sendNewLineEnabled", DEF_SETTINGS.sendNewLineEnabled).toBool();
out.stringStatus = settings.value("stringStatus", DEF_SETTINGS.stringStatus).toString();
out.isDtr = settings.value("isDtr", DEF_SETTINGS.isDtr).toBool();
out.isRts = settings.value("isRts", DEF_SETTINGS.isRts).toBool();
out.isHexDisplay = settings.value("isHexDisplay", DEF_SETTINGS.isHexDisplay).toBool();
out.isHexSend = settings.value("isHexSend", DEF_SETTINGS.isHexSend).toBool();
out.isTimerSend = settings.value("isTimerSend", DEF_SETTINGS.isTimerSend).toBool();
out.timerLength = settings.value("timeTimerSend", DEF_SETTINGS.timerLength).toInt();
out.sendCache = settings.value("sendCache", DEF_SETTINGS.sendCache).toString();
out.sendNum = 0;
out.receiveNum = 0;
}
return out;
}
void MainWindow::init()
{
// 初始化定时发送定时器
autoSendTimer = new QTimer(this);
//将定时器超时信号与槽(功能函数)联系起来
connect( autoSendTimer,SIGNAL(timeout()), this, SLOT(writeData()) );
//读出上次保存Settings
currentSettings = doSettings(false, Settings());
mStatusBar = ui->statusBar;
mSendButton = ui->send_pushButton;
mSendFileButton = ui->sendfile_pushButton;
mOpenSerialButton = ui->openserial_pushButton;
mLedLabel = ui->led_label;
mOpenFileLineEdit = ui->openfile_lineEdit;
mTimerSendLineEdit = ui->timer_lineEdit;
intValidator = new QIntValidator(0, 4000000, this);
ui->baudRateBox->setInsertPolicy(QComboBox::NoInsert);
// 字体大小
font = QFont( "Arial", 8);
// 只能输入数字的正则
QValidator *numberOnlyValidator; //检验器,只允许输入数字
QRegExp regx("[0-9]+$"); //设置输入范围0~9
numberOnlyValidator = new QRegExpValidator(regx, mTimerSendLineEdit);
// 设置窗口标题
QDateTime dt = QDateTime::fromTime_t( (uint)getDateFromMacro(__DATE__));
this->setWindowTitle("sscom for Linux/Mac 0.4, github/kangear/sscom " + dt.toString("yyyy/MM")); //
// 状态
isOn = false;
mLedLabel->setPixmap(QPixmap(":/led/off"));
mOpenFileLineEdit->setText(tr("文件名"));
mFilePath = ui->openfile_lineEdit->text();
mTimerSendLineEdit->setValidator(numberOnlyValidator);
// 网址
mNetAddrLabel = new QLabel;
mNetAddrLabel->setMinimumSize(80, 14); // 设置标签最小大小
mNetAddrLabel->setText("www.daxia.com");
mNetAddrLabel->setAlignment(Qt::AlignHCenter);
mNetAddrLabel->setFont(font);
// 发送数量
mSendLabel = new QLabel;
mSendLabel->setMinimumSize(60, 14); // 设置标签最小大小
mSendLabel->setText("S:0");
mSendLabel->setAlignment(Qt::AlignLeft);
mSendLabel->setFont(font);
// 接收数量
mReceiveLabel = new QLabel;
mReceiveLabel->setMinimumSize(60, 14); // 设置标签最小大小
mReceiveLabel->setText("R:0");
mReceiveLabel->setAlignment(Qt::AlignLeft);
mReceiveLabel->setFont(font);
// 状态栏
mStatusLabel = new QLabel;
mStatusLabel->setMinimumSize(240, 14); // 设置标签最小大小
mStatusLabel->setAlignment(Qt::AlignLeft);
mStatusLabel->setFont(font);
// mFinallyLabel TODO:弄清楚意思后去实现它
mFinallyLabel = new QLabel;
mFinallyLabel->setMinimumSize(160, 14); // 设置标签最小大小
mFinallyLabel->setAlignment(Qt::AlignLeft);
mFinallyLabel->setText("CTS:0 DSR:0 RLSD:0 ");
mFinallyLabel->setFont(font);
mStatusBar->addWidget(mNetAddrLabel);
mStatusBar->addWidget(mReceiveLabel);
mStatusBar->addWidget(mSendLabel);
mStatusBar->addWidget(mStatusLabel);
mStatusBar->addWidget(mFinallyLabel);
// 2.更新文字
mOpenSerialButton->setText("打开串口");
// 3.使能发送按键
mSendButton->setDisabled(true);
// 4.使能发送文件按键
mSendFileButton->setDisabled(true);
//! [1]
serial = new QSerialPort(this);
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));
//! [2]
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
//! [2]
connect(mSendButton, SIGNAL(released()), this, SLOT(onSendButtonRelease()));
//! [3]
// 1.先填充参数
fillPortsParameters();
fillPortsInfo();
updateSettings();
// 2.连接改变事件,一定要在填充参数之后进行连接,否则会出现无法填充已经保存参数
connect(ui->serialPortInfoListBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(serialPortChanged()));
connect(ui->baudRateBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(checkCustomBaudRatePolicy(int)));
connect(ui->dataBitsBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->stopBitsBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->parityBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->flowControlBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->dtr_checkBox, SIGNAL(stateChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->rts_checkBox, SIGNAL(stateChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->timersend_checkBox, SIGNAL(stateChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->hexdisplay_checkBox, SIGNAL(stateChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->hexsend_checkBox, SIGNAL(stateChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->newLineCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(currentIndexChanged()));
connect(ui->timer_lineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(currentIndexChanged()));
connect(ui->sendLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(currentIndexChanged()));
}
/**
* @brief MainWindow::serialPortChanged
* @param idx
* handle StopBitsBox dataBitsBox stopBitsBox parityBox flowControlBox
*/
void MainWindow::serialPortChanged()
{
if(serial->isOpen())
closeSerialPort();
updateSettings();
}
/** 将hex(61 62 63 64 65 66 67) to String(abcdefg) */
static QString hexToQString(bool isDebug, QString hexStr) {
if(isDebug) qDebug() << __func__ << ": " << hexStr;
QString ret;
QStringList list = hexStr.split(" ", QString::SkipEmptyParts);
for(QString qs:list) {
bool bStatus = false;
int a = qs.toInt(&bStatus, 16);
if(bStatus && (qs.length() == 2)) {
QString sA = QString(QChar(a));
if(isDebug) qDebug() << "a:" << a << "sA:" << sA << "qs:" + qs;
ret.append(sA);
} else {/* 转换失败,使用默认字体串 */
if(isDebug) qDebug() << "error!!!";
// ret = "abcdefg";
break;
}
}
return ret;
}
/** 将String(abcdefg) to Hex(61 62 63 64 65 66 67) */
static QString stringToHex(bool isDebug, QString str) {
if(isDebug) qDebug() << __func__ << ": " << str;
QString ret;
/* 将String(abcdefg) to Hex */
for(int i=0; i<str.length(); i++) {
/* 将字符串中字符转换成QChar */
QChar random = str.at(i).toLatin1();
QString hex;
QString str1;
/* 将QChar转换成unicode */
hex.setNum(random.unicode(), 16);
/**
* 由于unicode位数随机,所以需要根据情况进行格式化
* 只保留末两位,如果不够两位补0
*/
if(hex.length() >= 2) {
if(isDebug) qDebug() << "hex.length() >= 2 hex:" << hex;
str1 = hex.mid(hex.length() - 2, hex.length());
} else if(hex.length() == 1) {
if(isDebug) qDebug() << "hex.length() == 1 hex:" << hex;
str1 = hex.prepend("0");
} else {
if(isDebug) qDebug() << "else";
str1 = "";
}
ret.append(str1.toUpper() + " ");
}
return ret;
}
/**
* @brief MainWindow::currentIndexChanged
* @param idx
* handle StopBitsBox dataBitsBox stopBitsBox parityBox flowControlBox
*/
void MainWindow::currentIndexChanged()
{
// qDebug() << __func__;
Settings old = currentSettings;
updateSettings();
Settings now = currentSettings;
if(serial->isOpen())
setParameter(serial, currentSettings);
// 更新显示方式
if(old.isHexDisplay != now.isHexDisplay) {
qDebug() << "is need hexDisplay:" << now.isHexDisplay;
}
// 输入框文字发生改变,更新缓存 解决第一次启动时缓存为空问题
if(currentSettings.sendStringCache == "" ||
QString::compare(old.sendCache, now.sendCache, Qt::CaseInsensitive) != 0) {
currentSettings.sendStringCache = now.sendCache;
/* 如果是16进制状态,需要将16进制数转换成字符串 */
if(now.isHexSend) {
currentSettings.sendStringCache = hexToQString(false, now.sendCache);
}
/* 将小写换成大写 */
ui->sendLineEdit->setText(ui->sendLineEdit->text().toUpper());
}
// 更新发送方式
if(old.isHexSend != now.isHexSend) {
/* 需要将输入框内容有字体串改为16进制显示 */
QString inputStr = ui->sendLineEdit->text();
if(now.isHexSend) {
/* 将原数据保存 */
currentSettings.sendCache = stringToHex(false, inputStr);
currentSettings.sendStringCache = inputStr;
} else {
/* 将数据保存 */
QString tmp = hexToQString(false ,inputStr);
currentSettings.sendCache = tmp;
currentSettings.sendStringCache = tmp;
}
// qDebug() << "sendStringCache:" << currentSettings.sendStringCache;
ui->sendLineEdit->setText(currentSettings.sendCache);
}
if(DEBUG) qDebug() << "sendStringCache:" << stringToHex(false, currentSettings.sendStringCache);
// 更新AutoSend Qtimer
if(old.isTimerSend != now.isTimerSend) {
if(now.isTimerSend) {
// 禁用timerLength编辑
ui->timer_lineEdit->setDisabled(true);
// 停止运行定时器
if (autoSendTimer->isActive() )
autoSendTimer->stop();
//开始运行定时器,定时时间间隔为1000ms
autoSendTimer->start(now.timerLength);
} else {
// 启用timerLength编辑
ui->timer_lineEdit->setDisabled(false);
// 停止运行定时器
if (autoSendTimer->isActive() )
autoSendTimer->stop();
}
}
}
void MainWindow::checkCustomBaudRatePolicy(int idx)
{
bool isCustomBaudRate = !ui->baudRateBox->itemData(idx).isValid();
ui->baudRateBox->setEditable(isCustomBaudRate);
if (isCustomBaudRate) {
ui->baudRateBox->clearEditText();
QLineEdit *edit = ui->baudRateBox->lineEdit();
edit->setValidator(intValidator);
}
updateSettings();
if(serial->isOpen())
setParameter(serial, currentSettings);
}
void setCurrentIndex(QComboBox *comboBox, QString text)
{
int index = comboBox->findText(text); //use default exact match
if(index >= 0)
comboBox->setCurrentIndex(index);
}
void MainWindow::fillPortsParameters()
{
// fill baud rate (is not the entire list of available values,
// desired values??, add your independently)
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("115200"), QSerialPort::Baud115200);
ui->baudRateBox->addItem(QStringLiteral("Custom"));
// fill data bits
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);
// fill parity
ui->parityBox->addItem(QStringLiteral("None"), QSerialPort::NoParity);
ui->parityBox->addItem(QStringLiteral("Even"), QSerialPort::EvenParity);
ui->parityBox->addItem(QStringLiteral("Odd"), QSerialPort::OddParity);
ui->parityBox->addItem(QStringLiteral("Mark"), QSerialPort::MarkParity);
ui->parityBox->addItem(QStringLiteral("Space"), QSerialPort::SpaceParity);
// fill stop bits
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);
// fill flow control
ui->flowControlBox->addItem(QStringLiteral("None"), QSerialPort::NoFlowControl);
ui->flowControlBox->addItem(QStringLiteral("Hardware"), QSerialPort::HardwareControl);
ui->flowControlBox->addItem(QStringLiteral("Software"), QSerialPort::SoftwareControl);
// 还原上次保存的参数
setCurrentIndex(ui->baudRateBox, currentSettings.stringBaudRate);
setCurrentIndex(ui->dataBitsBox, currentSettings.stringDataBits);
setCurrentIndex(ui->stopBitsBox, currentSettings.stringStopBits);
setCurrentIndex(ui->parityBox, currentSettings.stringParity);
setCurrentIndex(ui->flowControlBox, currentSettings.stringFlowControl);
ui->dtr_checkBox->setChecked(currentSettings.isDtr);
ui->rts_checkBox->setChecked(currentSettings.isRts);
ui->hexdisplay_checkBox->setChecked(currentSettings.isHexDisplay);
ui->hexsend_checkBox->setChecked(currentSettings.isHexSend);
ui->newLineCheckBox->setChecked(currentSettings.sendNewLineEnabled);
ui->timersend_checkBox->setChecked(currentSettings.isTimerSend);
ui->timer_lineEdit->setText(QString::number(currentSettings.timerLength));
ui->sendLineEdit->setText(currentSettings.sendCache);
/* 启动后第一次更新 */
currentIndexChanged();
}
MainWindow::~MainWindow()
{
// 更新最终设置并保存
updateSettings();
doSettings(true, currentSettings);
// 防止没有关闭串口
closeSerialPort();
// 删除申请的对象
delete autoSendTimer;
delete serial;
delete ui;
}
void MainWindow::on_openserial_pushButton_pressed()
{
if(serial->isOpen())
closeSerialPort();
else
openSerialPort();
currentIndexChanged();
}
void MainWindow::on_openfile_pushButton_released()
{
// 判断目前是否已经打开过的路径
if((mFilePath == nullptr || mFilePath.length() == 0) && !QDir(mFilePath).exists()) {
// 如果没有打开过,或者目录不存在,那么将打开应用所在目录
mFilePath = ".";
}
// 打开新文件
mFilePath = QFileDialog::getOpenFileName(this, tr("打开"), mFilePath, tr("All Files(*.* *.**)"));
if(mFilePath.length() != 0) {
mOpenFileLineEdit->setText(mFilePath);
}
}
void MainWindow::fillPortsInfo()
{
ui->serialPortInfoListBox->clear();
static const QString blankString = QObject::tr("N/A");
QString description;
QString manufacturer;
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
QStringList list;
description = info.description();
manufacturer = info.manufacturer();
list << info.portName()
<< (!description.isEmpty() ? description : blankString)
<< (!manufacturer.isEmpty() ? manufacturer : blankString)
<< info.systemLocation()
<< (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : blankString)
<< (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : blankString);
ui->serialPortInfoListBox->addItem(list.first(), list);
}
}
void MainWindow::updateSettings()
{
if(serial->isOpen())
currentSettings.stringStatus = "已打开";
else
currentSettings.stringStatus = "已关闭";
currentSettings.name = ui->serialPortInfoListBox->currentText();
// Baud Rate
if (ui->baudRateBox->currentIndex() == 4) {
// custom baud rate
currentSettings.baudRate = ui->baudRateBox->currentText().toInt();
} else {
// standard baud rate
currentSettings.baudRate = static_cast<QSerialPort::BaudRate>(
ui->baudRateBox->itemData(ui->baudRateBox->currentIndex()).toInt());
}
currentSettings.stringBaudRate = QString::number(currentSettings.baudRate);
// Data bits
currentSettings.dataBits = static_cast<QSerialPort::DataBits>(
ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt());
currentSettings.stringDataBits = ui->dataBitsBox->currentText();
// Parity
currentSettings.parity = static_cast<QSerialPort::Parity>(
ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());
currentSettings.stringParity = ui->parityBox->currentText();
// Stop bits
currentSettings.stopBits = static_cast<QSerialPort::StopBits>(
ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt());
currentSettings.stringStopBits = ui->stopBitsBox->currentText();
// Flow control
currentSettings.flowControl = static_cast<QSerialPort::FlowControl>(
ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt());
currentSettings.stringFlowControl = ui->flowControlBox->currentText();
// Additional options
currentSettings.localEchoEnabled = false; //ui->localEchoCheckBox->isChecked();
// new line
currentSettings.sendNewLineEnabled = ui->newLineCheckBox->isChecked();
currentSettings.isDtr = ui->dtr_checkBox->isChecked();
currentSettings.isRts = ui->rts_checkBox->isChecked();
currentSettings.isHexDisplay = ui->hexdisplay_checkBox->isChecked();
currentSettings.isHexSend = ui->hexsend_checkBox->isChecked();
currentSettings.sendNewLineEnabled = ui->newLineCheckBox->isChecked();
currentSettings.isTimerSend = ui->timersend_checkBox->isChecked();
currentSettings.timerLength = ui->timer_lineEdit->text().toInt();
currentSettings.sendCache = ui->sendLineEdit->text();
// 更新状态栏
updateUi(currentSettings);
}
void MainWindow::updateUi(Settings p)
{
// 更新状态栏
mStatusLabel->setText(tr("%1 %2 %3bps,%4,%5, %6, %7")
.arg(p.name).arg(p.stringStatus).arg(p.stringBaudRate).arg(p.stringDataBits)
.arg(p.stringStopBits).arg(p.stringParity).arg(p.stringFlowControl));
// 更新接收发送总量
if(DEBUG) qDebug() <<"p.receiveNum:" << p.receiveNum;
if(DEBUG) qDebug() <<"p.sendNum:" << p.sendNum;
mReceiveLabel->setText(tr("R:%1").arg(p.receiveNum));
mSendLabel->setText(tr("S:%1").arg(p.sendNum));
}
bool MainWindow::setParameter(QSerialPort *serial, Settings settings)
{
bool ret;
Settings p = settings;
if (serial->setBaudRate(p.baudRate)
&& serial->setDataBits(p.dataBits)
&& serial->setParity(p.parity)
&& serial->setStopBits(p.stopBits)
&& serial->setFlowControl(p.flowControl)) {
ret = true;
} else
ret = false;
return ret;
}
//! [4]
bool MainWindow::openSerialPort()
{
bool ret = false;
Settings p = currentSettings;
serial->setPortName(p.name);
if (serial->open(QIODevice::ReadWrite)) {
if (setParameter(serial, p)) {
mLedLabel->setPixmap(QPixmap(":/led/on"));
// 2.更新文字
mOpenSerialButton->setText("关闭串口");
// 3.使能发送按键
mSendButton->setDisabled(false);
// 4.使能发送文件按键
mSendFileButton->setDisabled(false);
ret = true;
} else {
serial->close();
QMessageBox::critical(this, tr("Error"), serial->errorString());
ui->statusBar->showMessage(tr("Open error"));
}
} else {
QMessageBox::critical(this, tr("Error"), serial->errorString());
ui->statusBar->showMessage(tr("Configure error"));
}
return ret;
}
//! [4]
//! [5]
void MainWindow::closeSerialPort()
{
mLedLabel->setPixmap(QPixmap(":/led/off"));
// 2.更新文字
mOpenSerialButton->setText("打开串口");
// 3.使能发送按键
mSendFileButton->setDisabled(true);
// 4.使能发送文件按键
mSendButton->setDisabled(true);
serial->close();
}
//! [5]
void MainWindow::about()
{
QMessageBox::about(this, tr("About Simple Terminal"),
tr("The <b>Simple Terminal</b> example demonstrates how to "
"use the Qt Serial Port module in modern GUI applications "
"using Qt, with a menu bar, toolbars, and a status bar."));
}
void MainWindow::onSendButtonRelease()
{
currentIndexChanged();
writeData();
}
/**
* bytesToHex
* @brief bytesToHex
* @param array
* @return
*/
static QString bytesToHex(QByteArray array) {
QString hex = array.toHex().toUpper();
return hex.replace(QRegularExpression("(.{2})"), "\\1 ");
}
/** 发送数据 */
void MainWindow::writeData()
{
if(!serial->isOpen()) {
return;
}
QString text = currentSettings.sendStringCache;
if(DEBUG) qDebug() << __func__ << ":" << text;
if(text.length() !=0 && currentSettings.sendNewLineEnabled)
text += "\r\n";
QByteArray data = text.toLatin1();
qDebug() << "writeData: " << bytesToHex(data);
qint32 len = serial->write(data);
// 更新显示长度
if(len >= 0) {
currentSettings.sendNum += len;
updateUi(currentSettings);
}
if(DEBUG) qDebug() <<"currentSettings.sendNum:" << currentSettings.sendNum;
}
qint64 lastTimestamp = 0;
//! [6]
//! [7]
void MainWindow::readData()
{
// 1.处理换行 距离上次接收间100ms换行
qint64 timestamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
if ((timestamp - lastTimestamp) > 100 && currentSettings.receiveNum > 0) {
ui->receive_textBrowser->insertPlainText("\n");
}
lastTimestamp = timestamp;
// 2.读取数据
QByteArray data = serial->readAll();
QString str = QString::fromLatin1(data.data());
QString hex = bytesToHex(data);
QString newStr = currentSettings.isHexDisplay ? hex : str;
ui->receive_textBrowser->moveCursor (QTextCursor::End);
// 禁用ascii terminal color
QRegExp rx("\u001B\\[[;\\d]*m");
newStr = newStr.replace(rx, "");
// 显示
ui->receive_textBrowser->insertPlainText(newStr);
ui->receive_textBrowser->moveCursor (QTextCursor::End);
// 3.更新显示长度
qint32 len = data.length();
if(DEBUG) qDebug() <<"len:" << len;
if(len >= 0) {
currentSettings.receiveNum += len;
currentIndexChanged();
}
}
//! [7]
//! [8]
//! 添加除错功能,解决强制拔出时程序崩溃。
void MainWindow::handleError(QSerialPort::SerialPortError error)
{
QString errStr = "NO ERROR";
switch(error) {
case QSerialPort::DeviceNotFoundError:
errStr = "DeviceNotFoundError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::PermissionError:
errStr = "PermissionError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::OpenError:
errStr = "OpenError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::ParityError:
errStr = "ParityError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::FramingError:
errStr = "FramingError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::BreakConditionError:
errStr = "BreakConditionError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::WriteError:
errStr = "WriteError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::ReadError:
errStr = "ReadError";
closeSerialPort();
break;
case QSerialPort::ResourceError:
errStr = "ResourceError";
qDebug() << errStr;
on_openserial_pushButton_pressed();
break;
case QSerialPort::UnsupportedOperationError:
errStr = "UnsupportedOperationError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::UnknownError:
// do nothing!!!
errStr = "UnknownError";
qDebug() << errStr;
// on_openserial_pushButton_pressed();
break;
case QSerialPort::TimeoutError:
errStr = "TimeoutError";
qDebug() << errStr;
closeSerialPort();
break;
case QSerialPort::NotOpenError:
// do nothing.
errStr = "NotOpenError";
qDebug() << errStr;
// closeSerialPort();
break;
default:
qDebug() << errStr;
break;
}
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
//closeSerialPort();
}
}
//! [8]
/**
* @brief MainWindow::on_clear_pushButton_released
* 清除接收区内容
*/
void MainWindow::on_clear_pushButton_released()
{
ui->receive_textBrowser->clear();
currentIndexChanged();
}
/** 16进制发送 checkbox按下时 */
void MainWindow::on_hexsend_checkBox_released()
{
}