-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainWindow.cpp
480 lines (419 loc) · 12.8 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
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
#include <QtWidgets>
#include <iostream>
#include "mainWindow.h"
#include "common.h"
#include <QTextCursor>
#include <QFrame>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
createUartLayout();
/*setStyleSheet("QPushButton {\
color: white;\
background: transparent;\
border-style: outset;\
border-width: 1px;\
border-color: beige; }"
"QComboBox {\
color: white;\
background: transparent }"
"QCheckBox {\
color: white;\
background: transparent }"
"QRadioButton {\
color: white;\
background: transparent }"
"QTextEdit {\
color: white;\
background: transparent }"
"QLineEdit{\
color: white;\
background: transparent}"
"QLabel { color: white }");*/
auto mainFrame=new QWidget;
mainFrame->setAutoFillBackground(true);
QPalette palette;
QPixmap pixmap("source//back.png");
palette.setBrush(QPalette::Window, QBrush(pixmap));
mainFrame->setPalette(palette);
mainFrame->setLayout(mainLayout);
setWindowIcon(QIcon("source\\Icon.png"));
setFixedSize(557,447);
setCentralWidget(mainFrame);
setWindowTitle(tr("串口调试助手V0.2"));
}
void MainWindow::createUartBoxs()
{
/*串口基本数据设置*/
ComBox=new QComboBox;
BaudBox=new QComboBox;
BaudBox->addItem(tr("1200"));
BaudBox->addItem(tr("2400"));
BaudBox->addItem(tr("4800"));
BaudBox->addItem(tr("9600"));
BaudBox->addItem(tr("14400"));
BaudBox->addItem(tr("19200"));
BaudBox->addItem(tr("38400"));
BaudBox->addItem(tr("56000"));
BaudBox->addItem(tr("57600"));
BaudBox->addItem(tr("115200"));
BaudBox->addItem(tr("128000"));
BaudBox->addItem(tr("256000"));
ParityBox=new QComboBox;
ParityBox->addItem(tr("N"));
ParityBox->addItem(tr("O"));
ParityBox->addItem(tr("E"));
ByteSizeBox=new QComboBox;
ByteSizeBox->addItem(tr("5"));
ByteSizeBox->addItem(tr("6"));
ByteSizeBox->addItem(tr("7"));
ByteSizeBox->addItem(tr("8"));
StopBitBox=new QComboBox;
StopBitBox->addItem(tr("1"));
StopBitBox->addItem(tr("1.5"));
StopBitBox->addItem(tr("2"));
/*接收框*/
DisplayByHex=new QCheckBox(tr("十六进制显示"));
ReceiveBox=new QTextEdit;
ReceiveBox->setReadOnly(true);
ReceiveBoxClean=new QPushButton(tr("清空接收区"));
connect(ReceiveBoxClean,&QPushButton::clicked,[=](){ReceiveBox->clear();});
/*发送框*/
SendBox=new QTextEdit;
SendBox->setText(tr("串口调试助手v0.2"));
SendInHex=new QCheckBox(tr("十六进制发送"));
SendInHex->setEnabled(false);
SendData=new QPushButton(tr("发送数据"));
SendData->setEnabled(false);
connect(SendData,&QPushButton::clicked,[=](){this->sendData();});
SendTimer=new QTimer;
SendByTime=new QCheckBox(tr("定时发送"));
SendTimeSet=new QLineEdit;
QRegExp SendTimeSetRule("^[0-9]*");
SendTimeSet->setValidator(new QRegExpValidator(SendTimeSetRule));
SendByTime->setEnabled(false);
SendTimeSet->setMaximumWidth(50);
connect(SendByTime,SIGNAL(clicked()),this,SLOT(autoSend()));
connect(SendTimer,&QTimer::timeout,[=](){this->sendData();});
/*串口开关*/
UartSwitch=new QPushButton(tr("打开串口"));
connect(UartSwitch,SIGNAL(clicked()),this,SLOT(comSwitch()));
/*错误提示框*/
ErrorDialog=new QErrorMessage;
ErrorDialog->setWindowTitle(tr("出错啦!"));
ErrorDialog->setModal(true);
/*收数据定时器*/
ReceiveTimer=new QTimer;
connect(ReceiveTimer,SIGNAL(timeout()),this,SLOT(ReceiveData()));
/*编码选择*/
CodeSet=new QButtonGroup;
UseGB=new QRadioButton(tr("使用GB18030编码"));
UseUTF=new QRadioButton(tr("使用UTF-8编码"));
CodeSet->addButton(UseGB);
CodeSet->addButton(UseUTF);
UseUTF->setChecked(true);
/*自动刷新可用串口信息*/
updateSerialInfo=new QTimer;
updateSerialInfo->start(1000);
connect(updateSerialInfo,&QTimer::timeout,[=](){
static QList<QSerialPortInfo> proList;
QList<QSerialPortInfo> nowList;
for(auto &SerialInfo:QSerialPortInfo::availablePorts())
{
nowList+=SerialInfo;
}
if(nowList==proList)
{
return;
}
else
{
updateComBox(nowList);
}
proList=nowList;
});
}
void MainWindow::createUartLabels()
{
ComLabel=new QLabel(tr("串口:"));
BaudLabel=new QLabel(tr("波特率:"));
ParityLabel=new QLabel(tr("校验位:"));
ByteSizeLabel=new QLabel(tr("数据位:"));
StopBitLabel=new QLabel(tr("停止位:"));
SendTimeMs=new QLabel(tr("毫秒"));
}
void MainWindow::createUartLayout()
{
createUartBoxs();
createUartLabels();
auto uartSetLayout=new QGridLayout;
uartSetLayout->addWidget(ComLabel,0,0);
uartSetLayout->addWidget(ComBox,0,1);
uartSetLayout->addWidget(BaudLabel,1,0);
uartSetLayout->addWidget(BaudBox,1,1);
uartSetLayout->addWidget(ParityLabel,2,0);
uartSetLayout->addWidget(ParityBox,2,1);
uartSetLayout->addWidget(ByteSizeLabel,3,0);
uartSetLayout->addWidget(ByteSizeBox,3,1);
uartSetLayout->addWidget(StopBitLabel,4,0);
uartSetLayout->addWidget(StopBitBox,4,1);
uartSetLayout->addWidget(UartSwitch,5,0,1,2);
uartSetLayout->addWidget(ReceiveBoxClean,6,0,1,2);
uartSetLayout->addWidget(DisplayByHex,7,0,1,2);
uartSetLayout->addWidget(UseUTF,8,0,1,2);
uartSetLayout->addWidget(UseGB,9,0,1,2);
auto uartSendLayout=new QGridLayout;
uartSendLayout->addWidget(SendTimeSet,0,0);
uartSendLayout->addWidget(SendTimeMs,0,1);
uartSendLayout->addWidget(SendByTime,1,0,1,2);
uartSendLayout->addWidget(SendInHex,2,0,1,2);
uartSendLayout->addWidget(SendData,3,0,1,2);
auto leftLayout=new QVBoxLayout;
leftLayout->addLayout(uartSetLayout);
leftLayout->addStretch();
leftLayout->addLayout(uartSendLayout);
ReceiveBox->setMinimumWidth(400);
auto rightLayout=new QVBoxLayout;
rightLayout->addWidget(ReceiveBox);
rightLayout->addWidget(SendBox);
mainLayout=new QGridLayout;
mainLayout->addLayout(leftLayout,0,0);
mainLayout->addLayout(rightLayout,0,1,1,2);
}
void MainWindow::comSwitch()
{
if(fComOpen==false)
{
/*打开串口*/
ser.setPortName(QString("\\\\.\\")+getComName(ComBox->currentText()));
ser.setBaudRate(BaudBox->currentText().toInt());
switch(ByteSizeBox->currentText().toInt())
{
case 5:ser.setDataBits(QSerialPort::Data5); break;
case 6:ser.setDataBits(QSerialPort::Data6); break;
case 7:ser.setDataBits(QSerialPort::Data7); break;
case 8:ser.setDataBits(QSerialPort::Data8); break;
}
switch(ParityBox->currentText().toStdString()[0])
{
case 'N':ser.setParity(QSerialPort::NoParity); break;
case 'O':ser.setParity(QSerialPort::OddParity); break;
case 'E':ser.setParity(QSerialPort::EvenParity); break;
}
switch(static_cast<int>(StopBitBox->currentText().toFloat()*10))
{
case 10:ser.setStopBits(QSerialPort::OneStop); break;
case 15:ser.setStopBits(QSerialPort::OneAndHalfStop); break;
case 20:ser.setStopBits(QSerialPort::TwoStop); break;
}
if(ser.open(QIODevice::ReadWrite)==false)
{
ErrorDialog->showMessage(tr("串口正在被占用"));
return;
}
fComOpen=true;
ReceiveTimer->start(ReceiveCycle);
UartSwitch->setText(tr("关闭串口"));
ComBox->setEnabled(false);
BaudBox->setEnabled(false);
ByteSizeBox->setEnabled(false);
ParityBox->setEnabled(false);
StopBitBox->setEnabled(false);
SendInHex->setEnabled(true);
SendData->setEnabled(true);
SendByTime->setEnabled(true);
if(SendByTime->isChecked())
{
autoSend();
}
}
else
{
UartSwitch->setText(tr("打开串口"));
fComOpen=false;
/*关闭串口*/
ser.close();
ReceiveTimer->stop();
ComBox->setEnabled(true);
BaudBox->setEnabled(true);
ByteSizeBox->setEnabled(true);
ParityBox->setEnabled(true);
StopBitBox->setEnabled(true);
SendInHex->setEnabled(false);
SendData->setEnabled(false);
SendByTime->setEnabled(false);
if(SendTimer->isActive())
{
SendTimer->stop();
}
}
}
void MainWindow::autoSend()
{
if(fComOpen&&SendByTime->isChecked())
{
if(SendTimeSet->text().toInt()>=1)
{
SendTimer->start(SendTimeSet->text().toInt());
SendTimeSet->setEnabled(false);
}
else
{
ErrorDialog->showMessage("定时时间不能为0ms");
SendByTime->setChecked(false);
}
}
else
{
SendTimer->stop();
SendTimeSet->setEnabled(true);
}
}
void MainWindow::sendData()
{
if(fComOpen)
{
bool fSend=true;
if(!SendInHex->isChecked())
{
if(UseGB->isChecked())
{
fSend=ser.write(transcoding(SendBox->toPlainText().toUtf8(),"UTF-8","GB18030"));
}
else
{
fSend=ser.write(SendBox->toPlainText().toStdString().c_str());
}
}
else
{
auto temp=strToHex(SendBox->toPlainText().toStdString());
fSend=ser.write(temp.c_str());
}
if(!fSend)
{
ErrorDialog->showMessage(tr("发送失败,可能是串口已断开,即将关闭串口"));
comSwitch();
}
}
else
{
ErrorDialog->showMessage("尚未启动串口");
}
}
void MainWindow::ReceiveData()
{
auto DataBuffer=ser.readAll();
if(!DataBuffer.isEmpty())
{
ReceiveBox->moveCursor(QTextCursor::EndOfBlock);
if(DisplayByHex->isChecked())
{
ReceiveBox->insertPlainText(hexToStr((unsigned char *)DataBuffer.data()));
}
else
{
if(UseGB->isChecked())
{
ReceiveBox->insertPlainText(transcoding(DataBuffer,"GB18030","UTF-8"));
}
else
{
ReceiveBox->insertPlainText(DataBuffer);
}
}
}
}
void MainWindow::updateComBox(const QList<QSerialPortInfo> &now)
{
/*寻找减少*/
for(int i=0;i<ComBox->count();i++)
{
auto inow=now.begin();
for(;inow!=now.end();++inow)
{
if(ComBox->itemText(i)==inow->description()+" "+inow->portName())
{
break;
}
}
if(inow==now.end())
{
ComBox->removeItem(i);
}
}
/*寻找增加*/
for(auto inow=now.begin();inow!=now.end();++inow)
{
int i=0;
for(;i<ComBox->count();i++)
{
if(ComBox->itemText(i)==inow->description()+" "+inow->portName())
{
break;
}
}
if(i==ComBox->count())
{
ComBox->addItem(inow->description()+" "+inow->portName());
}
}
/*自适应调整下拉宽度*/
int maxLen=0;
for(auto &i:now)
{
if(maxLen<(i.description()+" "+i.portName()).size())
{
maxLen=(i.description()+" "+i.portName()).size();
}
}
ComBox->view()->setMinimumWidth(maxLen*6+10);
}
bool operator==(const QSerialPortInfo &lhs,const QSerialPortInfo &rhs)
{
return lhs.portName()==rhs.portName() &&
lhs.description()==rhs.description();
}
QString MainWindow::getComName(const QString &ComBoxText)
{
QString result;
auto pText=ComBoxText.end();
--pText;
for(;*pText!=' ';--pText);
pText++;
for(;pText!=ComBoxText.end();++pText)
{
result+=*pText;
}
return result;
}
MainWindow::~MainWindow()
{
delete updateSerialInfo;
delete ComBox;
delete BaudBox;
delete ParityBox;
delete ByteSizeBox;
delete StopBitBox;
delete UartSwitch;
delete ReceiveBox;
delete ReceiveBoxClean;
delete DisplayByHex;
delete CodeSet;
delete UseGB;
delete UseUTF;
delete SendBox;
delete SendData;
delete SendInHex;
delete SendTimeMs;
delete SendTimeSet;
delete SendByTime;
delete SendTimer;
delete ComLabel;
delete BaudLabel;
delete ParityLabel;
delete ByteSizeLabel;
delete StopBitLabel;
delete mainLayout;
delete ErrorDialog;
delete ReceiveTimer;
}