-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
494 lines (431 loc) · 14.7 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include <QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
startInit();
myCom = NULL;
ui->qter->setText(
tr("<a href=\"http://www.qter.org\">www.qter.org</a>"));
#ifdef Q_OS_LINUX
ui->portNameComboBox->addItem( "ttyUSB0");
ui->portNameComboBox->addItem( "ttyUSB1");
ui->portNameComboBox->addItem( "ttyUSB2");
ui->portNameComboBox->addItem( "ttyUSB3");
ui->portNameComboBox->addItem( "ttyS0");
ui->portNameComboBox->addItem( "ttyS1");
ui->portNameComboBox->addItem( "ttyS2");
ui->portNameComboBox->addItem( "ttyS3");
ui->portNameComboBox->addItem( "ttyS4");
ui->portNameComboBox->addItem( "ttyS5");
ui->portNameComboBox->addItem( "ttyS6");
#elif defined (Q_OS_WIN)
ui->portNameComboBox->addItem("COM0");
ui->portNameComboBox->addItem("COM1");
ui->portNameComboBox->addItem("COM2");
ui->portNameComboBox->addItem("COM3");
ui->portNameComboBox->addItem("COM4");
ui->portNameComboBox->addItem("COM5");
ui->portNameComboBox->addItem("COM6");
ui->portNameComboBox->addItem("COM7");
ui->portNameComboBox->addItem("COM8");
ui->portNameComboBox->addItem("COM9");
ui->portNameComboBox->addItem("COM10");
ui->portNameComboBox->addItem("COM11");
ui->portNameComboBox->addItem("COM12");
ui->portNameComboBox->addItem("COM13");
ui->portNameComboBox->addItem("COM14");
ui->portNameComboBox->addItem("COM15");
ui->portNameComboBox->addItem("COM16");
ui->portNameComboBox->addItem("COM17");
ui->portNameComboBox->addItem("COM18");
ui->portNameComboBox->addItem("COM19");
ui->portNameComboBox->addItem("COM20");
#endif
ui->statusBar->showMessage(tr("欢迎使用QCom串口调试助手!"));
}
MainWindow::~MainWindow()
{
if(myCom != NULL){
if(myCom->isOpen()){
myCom->close();
}
delete myCom;
}
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void MainWindow::startInit(){
setActionsEnabled(false);
ui->delayspinBox->setEnabled(false);
ui->sendmsgBtn->setEnabled(false);
ui->sendMsgLineEdit->setEnabled(false);
ui->obocheckBox->setEnabled(false);
ui->actionAdd->setEnabled(true);
//初始化连续发送计时器计时间隔
obotimerdly = OBO_TIMER_INTERVAL;
//设置连续发送计时器
obotimer = new QTimer(this);
connect(obotimer, SIGNAL(timeout()), this, SLOT(sendMsg()));
}
void MainWindow::setActionsEnabled(bool status)
{
ui->actionSave->setEnabled(status);
ui->actionClose->setEnabled(status);
ui->actionLoadfile->setEnabled(status);
ui->actionCleanPort->setEnabled(status);
ui->actionWriteToFile->setEnabled(status);
}
void MainWindow::setComboBoxEnabled(bool status)
{
ui->portNameComboBox->setEnabled(status);
ui->baudRateComboBox->setEnabled(status);
ui->dataBitsComboBox->setEnabled(status);
ui->parityComboBox->setEnabled(status);
ui->stopBitsComboBox->setEnabled(status);
}
//打开串口
void MainWindow::on_actionOpen_triggered()
{
QString portName = ui->portNameComboBox->currentText(); //获取串口名
#ifdef Q_OS_LINUX
myCom = new QextSerialPort("/dev/" + portName);
#elif defined (Q_OS_WIN)
myCom = new QextSerialPort(portName);
#endif
connect(myCom, SIGNAL(readyRead()), this, SLOT(readMyCom()));
//设置波特率
myCom->setBaudRate((BaudRateType)ui->baudRateComboBox->currentText().toInt());
//设置数据位
myCom->setDataBits((DataBitsType)ui->dataBitsComboBox->currentText().toInt());
//设置校验
switch(ui->parityComboBox->currentIndex()){
case 0:
myCom->setParity(PAR_NONE);
break;
case 1:
myCom->setParity(PAR_ODD);
break;
case 2:
myCom->setParity(PAR_EVEN);
break;
default:
myCom->setParity(PAR_NONE);
qDebug("set to default : PAR_NONE");
break;
}
//设置停止位
switch(ui->stopBitsComboBox->currentIndex()){
case 0:
myCom->setStopBits(STOP_1);
break;
case 1:
#ifdef Q_OS_WIN
myCom->setStopBits(STOP_1_5);
#endif
break;
case 2:
myCom->setStopBits(STOP_2);
break;
default:
myCom->setStopBits(STOP_1);
qDebug("set to default : STOP_1");
break;
}
//设置数据流控制
myCom->setFlowControl(FLOW_OFF);
//设置延时
myCom->setTimeout(TIME_OUT);
if(myCom->open(QIODevice::ReadWrite)){
QMessageBox::information(this, tr("打开成功"), tr("已成功打开串口") + portName, QMessageBox::Ok);
//界面控制
ui->sendmsgBtn->setEnabled(true);
setComboBoxEnabled(false);
ui->sendMsgLineEdit->setEnabled(true);
ui->actionOpen->setEnabled(false);
ui->sendMsgLineEdit->setFocus();
ui->obocheckBox->setEnabled(true);
ui->actionAdd->setEnabled(false);
setActionsEnabled(true);
}else{
QMessageBox::critical(this, tr("打开失败"), tr("未能打开串口 ") + portName + tr("\n该串口设备不存在或已被占用"), QMessageBox::Ok);
return;
}
ui->statusBar->showMessage(tr("打开串口成功"));
}
//关闭串口
void MainWindow::on_actionClose_triggered()
{
myCom->close();
delete myCom;
myCom = NULL;
ui->sendmsgBtn->setEnabled(false);
setComboBoxEnabled(true);
ui->actionOpen->setEnabled(true);
ui->sendMsgLineEdit->setEnabled(false);
ui->obocheckBox->setEnabled(false);
ui->actionAdd->setEnabled(true);
setActionsEnabled(false);
ui->actionWriteToFile->setChecked(false);
ui->statusBar->showMessage(tr("串口已经关闭"));
}
//关于
void MainWindow::on_actionAbout_triggered()
{
aboutdlg.show();
// 在主窗口中间显示
int x =this->x() + (this->width() - aboutdlg.width()) / 2;
int y =this->y() + (this->height() - aboutdlg.height()) / 2;
aboutdlg.move(x, y);
ui->statusBar->showMessage(tr("关于Wincom"));
}
//读取数据
void MainWindow::readMyCom()
{
QByteArray temp = myCom->readAll();
QString buf;
if(!temp.isEmpty()){
ui->textBrowser->setTextColor(Qt::black);
if(ui->ccradioButton->isChecked()){
buf = temp;
}else if(ui->chradioButton->isChecked()){
QString str;
for(int i = 0; i < temp.count(); i++){
QString s;
s.sprintf("0x%02x, ", (unsigned char)temp.at(i));
buf += s;
}
}
if(!write2fileName.isEmpty()){
QFile file(write2fileName);
//如果打开失败则给出提示并退出函数
if(!file.open(QFile::WriteOnly | QIODevice::Text)){
QMessageBox::warning(this, tr("写入文件"), tr("打开文件 %1 失败, 无法写入\n%2").arg(write2fileName).arg(file.errorString()), QMessageBox::Ok);
return;
}
QTextStream out(&file);
out<<buf;
file.close();
}
ui->textBrowser->setText(ui->textBrowser->document()->toPlainText() + buf);
QTextCursor cursor = ui->textBrowser->textCursor();
cursor.movePosition(QTextCursor::End);
ui->textBrowser->setTextCursor(cursor);
ui->recvbyteslcdNumber->display(ui->recvbyteslcdNumber->value() + temp.size());
ui->statusBar->showMessage(tr("成功读取%1字节数据").arg(temp.size()));
}
}
//发送数据
void MainWindow::sendMsg()
{
QByteArray buf;
if(ui->sendAsHexcheckBox->isChecked()){
QString str;
bool ok;
char data;
QStringList list;
str = ui->sendMsgLineEdit->text();
list = str.split(" ");
for(int i = 0; i < list.count(); i++){
if(list.at(i) == " ")
continue;
if(list.at(i).isEmpty())
continue;
data = (char)list.at(i).toInt(&ok, 16);
if(!ok){
QMessageBox::information(this, tr("提示消息"), tr("输入的数据格式有错误!"), QMessageBox::Ok);
if(obotimer != NULL)
obotimer->stop();
ui->sendmsgBtn->setText(tr("发送"));
ui->sendmsgBtn->setIcon(QIcon(":new/prefix1/src/send.png"));
return;
}
buf.append(data);
}
}else{
#if QT_VERSION < 0x050000
buf = ui->sendMsgLineEdit->text().toAscii();
#else
buf = ui->sendMsgLineEdit->text().toLocal8Bit();
#endif
}
//发送数据
myCom->write(buf);
ui->statusBar->showMessage(tr("发送数据成功"));
//界面控制
ui->textBrowser->setTextColor(Qt::lightGray);
}
//发送数据按钮
void MainWindow::on_sendmsgBtn_clicked()
{
//如果当前正在连续发送数据,暂停发送
if(ui->sendmsgBtn->text() == tr("暂停")){
obotimer->stop();
ui->sendmsgBtn->setText(tr("发送"));
ui->sendmsgBtn->setIcon(QIcon(":new/prefix1/src/send.png"));
return;
}
//如果发送数据为空,给出提示并返回
if(ui->sendMsgLineEdit->text().isEmpty()){
QMessageBox::information(this, tr("提示消息"), tr("没有需要发送的数据"), QMessageBox::Ok);
return;
}
//如果不是连续发送
if(!ui->obocheckBox->isChecked()){
ui->sendMsgLineEdit->setFocus();
//发送数据
sendMsg();
}else{ //连续发送
obotimer->start(obotimerdly);
ui->sendmsgBtn->setText(tr("暂停"));
ui->sendmsgBtn->setIcon(QIcon(":new/prefix1/src/pause.png"));
}
}
//清空记录
void MainWindow::on_clearUpBtn_clicked()
{
ui->textBrowser->clear();
ui->statusBar->showMessage(tr("记录已经清空"));
}
//计数器清零
void MainWindow::on_actionClearBytes_triggered()
{
if(ui->recvbyteslcdNumber->value() == 0){
QMessageBox::information(this, tr("提示消息"), tr("貌似已经清零了呀:)"), QMessageBox::Ok);
}else{
ui->recvbyteslcdNumber->display(0);
ui->statusBar->showMessage(tr("计数器已经清零"));
}
}
//单击连续发送checkBox
void MainWindow::on_obocheckBox_clicked()
{
if(ui->obocheckBox->isChecked()){
ui->delayspinBox->setEnabled(true);
//检查是否有数据,如有则启动定时器
ui->statusBar->showMessage(tr("启用连续发送"));
}else{
ui->delayspinBox->setEnabled(false);
//若定时器已经启动则关闭它
ui->statusBar->showMessage(tr("停止连续发送"));
}
}
//保存textBrowser中的内容
void MainWindow::on_actionSave_triggered()
{
if(ui->textBrowser->toPlainText().isEmpty()){
QMessageBox::information(this, "提示消息", tr("貌似还没有数据! 您需要在发送编辑框中输入要发送的数据"), QMessageBox::Ok);
return;
}
QString filename = QFileDialog::getSaveFileName(this, tr("保存为"), tr("未命名.txt"));
QFile file(filename);
//如果用户取消了保存则直接退出函数
if(file.fileName().isEmpty()){
return;
}
//如果打开失败则给出提示并退出函数
if(!file.open(QFile::WriteOnly | QIODevice::Text)){
QMessageBox::warning(this, tr("保存文件"), tr("打开文件 %1 失败, 无法保存\n%2").arg(filename).arg(file.errorString()), QMessageBox::Ok);
return;
}
//写数据到文件
QTextStream out(&file);
out<<ui->textBrowser->toPlainText();
file.close();
//修改窗口标题为保存文件路径
setWindowTitle("saved: " + QFileInfo(filename).canonicalFilePath());
}
//退出程序
void MainWindow::on_actionExit_triggered()
{
this->close();
}
//添加串口
void MainWindow::on_actionAdd_triggered()
{
bool ok = false;
QString portname;
portname = QInputDialog::getText(this, tr("添加串口"), tr("设备文件名"), QLineEdit::Normal, 0, &ok);
if(ok && !portname.isEmpty()){
ui->portNameComboBox->addItem(portname);
ui->statusBar->showMessage(tr("添加串口成功"));
}
}
//调整连续发送时间间隔
void MainWindow::on_delayspinBox_valueChanged(int )
{
obotimerdly = ui->delayspinBox->value();
}
//载入外部文件
void MainWindow::on_actionLoadfile_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, tr("读取文件"), 0, tr("保存为 (*.txt *.log)")); //添加更多的文件类型
QFile file(filename);
//如果取消打开则退出函数
if(file.fileName().isEmpty()){
return;
}
//如果打开失败则提示并退出函数
if(!file.open(QFile::ReadOnly | QIODevice::Text)){
QMessageBox::warning(this, tr("打开失败"),
tr("抱歉! Wincom未能打开此文件, 这可能是由于没有足够的权限造成的. 您可以尝试使用chmod命令修改文件权限."),
QMessageBox::Ok);
return;
}
//文件大小超过限制
if(file.size() > MAX_FILE_SIZE){
QMessageBox::critical(this, tr("载入失败"), tr("文件大小为 %1 字节, 超过限制大小 10000 字节").arg(file.size()), QMessageBox::Ok);
return;
}
//文件太大时提示是否继续打开
if(file.size() > TIP_FILE_SIZE){
if(QMessageBox::question(this, tr("提示消息"), tr("您要打开的文件过大, 这将消耗更多一些的时间,要继续打开么?"),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::No){
return;
}
}
//读取数据并加入到发送数据编辑框
QByteArray line;
while(!file.atEnd()){
line = file.readLine();
ui->sendMsgLineEdit->setText(ui->sendMsgLineEdit->text() + tr(line));
}
file.close();
ui->statusBar->showMessage(tr("已经成功读取文件中的数据"));
}
//清空串口中的I/O数据
void MainWindow::on_actionCleanPort_triggered()
{
myCom->flush();
}
//写入文件菜单
void MainWindow::on_actionWriteToFile_triggered()
{
if(ui->actionWriteToFile->isChecked()){
QString filename = QFileDialog::getSaveFileName(this, tr("写入文件"), 0, tr("保存为 (*.*)")); //添加更多的文件类型
if(filename.isEmpty()){
ui->actionWriteToFile->setChecked(false);
}else{
write2fileName = filename;
//ui->textBrowser->setEnabled(false);
ui->actionWriteToFile->setToolTip(tr("停止写入到文件"));
}
}else{
write2fileName.clear();
//ui->textBrowser->setEnabled(true);
ui->actionWriteToFile->setToolTip(tr("将读取数据写入到文件"));
}
}