-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDigitalOscilloscope.cpp
381 lines (336 loc) · 11.4 KB
/
DigitalOscilloscope.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
#include "DigitalOscilloscope.h"
#include "ui_DigitalOscilloscope.h"
#include "SerialPort/qextserialenumerator.h"
#include "SerialPort/qextserialport.h"
#include "wheelbox.h"
#include "QTimer"
#include "signaldata.h"
#include "qwt_global.h"
#include <qthread.h>
#include "qwt_system_clock.h"
#include "PathMgr.h"
#include "QMessageBox"
#include "AboutDialog.h"
DigitalOscilloscope::DigitalOscilloscope(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::DigitalOscilloscope)
{
ui->setupUi(this);
//////////////////////////////////////////////////////
//波形显示窗
line = 0;
time = 0.0;
const double intervalLength = 10.0; // seconds
ui->QwtPlotFrame->setIntervalLength(intervalLength);
///////////////////////////////////////////////////////////////
//调节滑轮
d_timerWheel = new WheelBox(" 时间轴扩展 ",
1.0, 100.0, 1.0,
ui->d_timerWheel);
d_timerWheel->setValue(intervalLength);
// d_intervalWheel = new WheelBox(" Sample Interval [ms]",
// 0.0,20.0, 0.1,
// ui->d_intervalWheel);
// d_intervalWheel->setValue(10.0);
interval = 10.0;
connect(d_timerWheel, SIGNAL(valueChanged(double)),
ui->QwtPlotFrame,
SLOT(setIntervalLength( double )));
/*
connect(d_intervalWheel, SIGNAL( valueChanged(double)),
SIGNAL( signalIntervalChanged(double)));
connect(d_intervalWheel, SIGNAL( valueChanged(double)),
SLOT(setInterval( double )));
*/
////////////////////////////////
//初始化显示
ui->led->turnOff();
initCOMList();
initBaudRateList();
initDataBitList();
initParityList();
initStopBitList();
initQueryModel();
////////////////////////////////////////////////////////////////////////////////
//串口操作
timer = new QTimer(this);
timer->setInterval(10);
PortSettings settings = {BAUD9600, DATA_8, PAR_NONE, STOP_1, FLOW_OFF, 10};
port = new QextSerialPort(ui->COMComboBox->currentText(),
settings, QextSerialPort::Polling);
enumerator = new QextSerialEnumerator(this);
enumerator->setUpNotifications();
PathManage::createRelExeDir("/RecvDataDB/");
// QString RecvDataDB = PathManage::makePathStr("/RecvDataDB/RecvDataDB.db");
// recvDatabase = new RecvDatabBase(RecvDataDB,"RecvDataDB");
// recvDatabase->clearTable();
QString currTime;
currTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh-mm-ss");
QString RecvDataTXT = PathManage::makePathStr(QString("/RecvDataDB/Recv%1.txt").arg(currTime));
file = new QFile(RecvDataTXT);
if(!file->open(QIODevice::WriteOnly | QIODevice::Text))
{
QMessageBox::warning(this, tr(" 错误 "), tr("\n 文件打开失败!"));
qDebug() << "file open false";
}
else
{
qDebug() << "file open";
out.setDevice(file);
}
/////////////////////////////////////////////////////////////
//采样点获得线程
// samplingThread = new SerialSamplingThread(this,NULL,RecvDataTXT);
// samplingThread->setInterval(signalInterval());
// connect(this, SIGNAL(signalIntervalChanged(double)),
// samplingThread, SLOT(setInterval(double )));
////////////////////////////////////
//串口信号与槽的关联
connect(ui->BaudComboBox, SIGNAL(currentIndexChanged(int)), SLOT(onBaudRateChanged(int)));
connect(ui->ParityBitComboBox, SIGNAL(currentIndexChanged(int)), SLOT(onParityChanged(int)));
connect(ui->DataBitComboBox, SIGNAL(currentIndexChanged(int)), SLOT(onDataBitsChanged(int)));
connect(ui->StopComboBox, SIGNAL(currentIndexChanged(int)), SLOT(onStopBitsChanged(int)));
connect(ui->timeoutBox, SIGNAL(valueChanged(int)), SLOT(onTimeoutChanged(int)));
connect(ui->QueryModeComBox, SIGNAL(currentIndexChanged(int)), SLOT(onQueryModeChanged(int)));
connect(ui->COMComboBox, SIGNAL(editTextChanged(QString)), SLOT(onPortNameChanged(QString)));
connect(ui->OpenCloseButton, SIGNAL(clicked()), SLOT(onOpenCloseButtonClicked()));
connect(timer, SIGNAL(timeout()), SLOT(onReadyRead()));
connect(port, SIGNAL(readyRead()),this,SLOT(onReadyRead()));
connect(enumerator, SIGNAL(deviceDiscovered(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
connect(enumerator, SIGNAL(deviceRemoved(QextPortInfo)), SLOT(onPortAddedOrRemoved()));
}
DigitalOscilloscope::~DigitalOscilloscope()
{
delete ui;
port->deleteLater();
port = NULL;
if(timer->isActive())
{
timer->stop();
}
timer->deleteLater();
timer = NULL;
enumerator->deleteLater();
enumerator = NULL;
// samplingThread->stop();
// samplingThread->wait(1000);
d_timerWheel->deleteLater();
d_timerWheel = NULL;
// d_intervalWheel->deleteLater();
// d_intervalWheel = NULL;
// recvDatabase->clearTable();
// delete recvDatabase;
// recvDatabase = NULL;
}
void DigitalOscilloscope::initCOMList()
{
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
qDebug() << "List of ports:";
foreach (QextPortInfo info, ports)
{
qDebug() << "port name:" << info.portName;
qDebug() << "friendly name:" << info.friendName;
qDebug() << "physical name:" << info.physName;
qDebug() << "enumerator name:" << info.enumName;
qDebug() << "vendor ID:" << info.vendorID;
qDebug() << "product ID:" << info.productID;
////////////////////////////////////////////////////
//将可用的串口加入到串口列表
ui->COMComboBox->addItem(info.portName);
}
}
void DigitalOscilloscope::initBaudRateList()
{
ui->BaudComboBox->addItem("2400", BAUD2400);
ui->BaudComboBox->addItem("4800", BAUD4800);
ui->BaudComboBox->addItem("9600", BAUD9600);
ui->BaudComboBox->addItem("19200", BAUD19200);
ui->BaudComboBox->addItem("115200", BAUD115200);
ui->BaudComboBox->setCurrentIndex(2);
}
void DigitalOscilloscope::initDataBitList()
{
ui->DataBitComboBox->addItem("5", DATA_5);
ui->DataBitComboBox->addItem("6", DATA_6);
ui->DataBitComboBox->addItem("7", DATA_7);
ui->DataBitComboBox->addItem("8", DATA_8);
ui->DataBitComboBox->setCurrentIndex(3);
}
void DigitalOscilloscope::initParityList()
{
ui->ParityBitComboBox->addItem("无", PAR_NONE);
ui->ParityBitComboBox->addItem("奇", PAR_ODD);
ui->ParityBitComboBox->addItem("偶", PAR_EVEN);
ui->ParityBitComboBox->setCurrentIndex(0);
}
void DigitalOscilloscope::initStopBitList()
{
ui->StopComboBox->addItem("1", STOP_1);
ui->StopComboBox->addItem("2", STOP_2);
ui->StopComboBox->setCurrentIndex(0);
}
void DigitalOscilloscope::initQueryModel()
{
ui->QueryModeComBox->addItem("Polling", QextSerialPort::Polling);
ui->QueryModeComBox->addItem("EventDriven", QextSerialPort::EventDriven);
ui->QueryModeComBox->setCurrentIndex(0);
}
void DigitalOscilloscope::onPortNameChanged(const QString &name)
{
Q_UNUSED(name);
if (port->isOpen())
{
port->close();
// recvDatabase->clearTable();
ui->led->turnOff();
// samplingThread->stop();
// samplingThread->wait(1000);
}
}
void DigitalOscilloscope::onBaudRateChanged(int idx)
{
port->setBaudRate((BaudRateType)ui->BaudComboBox->itemData(idx).toInt());
}
void DigitalOscilloscope::onParityChanged(int idx)
{
port->setParity((ParityType)ui->ParityBitComboBox->itemData(idx).toInt());
}
void DigitalOscilloscope::onDataBitsChanged(int idx)
{
port->setDataBits((DataBitsType)ui->DataBitComboBox->itemData(idx).toInt());
}
void DigitalOscilloscope::onStopBitsChanged(int idx)
{
port->setStopBits((StopBitsType)ui->StopComboBox->itemData(idx).toInt());
}
void DigitalOscilloscope::onTimeoutChanged(int val)
{
port->setTimeout(val);
}
void DigitalOscilloscope::onQueryModeChanged(int idx)
{
port->setQueryMode((QextSerialPort::QueryMode)ui->QueryModeComBox->itemData(idx).toInt());
}
void DigitalOscilloscope::onOpenCloseButtonClicked()
{
if (!port->isOpen())
{
/////////////////////////////////////////////////////
//打开串口
port->setPortName(ui->COMComboBox->currentText());
port->open(QIODevice::ReadWrite);
/////////////////////////////////////////////////////
//打开串口以后的操作
time = 0.0;
// recvDatabase->clearTable();
start();
}
else
{
port->close();
// samplingThread->stop();
// samplingThread->wait(1000);
// recvDatabase->clearTable();
}
//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());
}
void DigitalOscilloscope::onReadyRead()
{
QByteArray adData;
if (port->bytesAvailable())
{
adData = port->readAll();
/////////////////////////////////////////
//收到的数据
ui->RecvTextBrowser->moveCursor(QTextCursor::End);
ui->RecvTextBrowser->insertPlainText(QString::fromLatin1(adData));
out << QString::fromLatin1(adData) << "\n";
}
//////////////////////////////////
//处理接收到的数据
QString RecvStr = adData;
QStringList RecvData = RecvStr.split(",", QString::SkipEmptyParts);
for(int i=0;i<RecvData.size();i++)
{
line++;
if(line%200 == 0)
{
ui->RecvTextBrowser->clear();
}
const int udata = RecvData.at(i).toInt();
const float recvData = ((float)udata)*3300/4096/1000;
// qDebug() << QString("收到的数据为:%1 在文件中处在第 %2 行").arg(recvData).arg(line);
//准备显示接收到的数据
// const QPointF s(time,recvData);
const double elapsed = clock.elapsed();
const QPointF s(elapsed/(interval*100.0),recvData);
SignalData::instance().append(s);
// time += 0.005; //100ms一个数据
}
}
void DigitalOscilloscope::onPortAddedOrRemoved()
{
QString current = ui->COMComboBox->currentText();
ui->COMComboBox->blockSignals(true);
ui->COMComboBox->clear();
foreach (QextPortInfo info, QextSerialEnumerator::getPorts())
{
ui->COMComboBox->addItem(info.portName);
}
ui->COMComboBox->setCurrentIndex(ui->COMComboBox->findText(current));
ui->COMComboBox->blockSignals(false);
}
void DigitalOscilloscope::start()
{
////////////////////////////////////////////////////
//开始
// samplingThread->start();
ui->QwtPlotFrame->start();
clock.start();
}
void DigitalOscilloscope::setInterval(double val)
{
interval = val;
}
double DigitalOscilloscope::signalInterval() const
{
return d_timerWheel->value();
}
void DigitalOscilloscope::on_action_Quit_triggered()
{
if (QMessageBox::Yes == QMessageBox::information(this,
"提示信息",
"您确定要退出吗?",
QMessageBox::Yes | QMessageBox::No))
{
QApplication::closeAllWindows();
}
else
{
}
}
void DigitalOscilloscope::on_action_Qt_triggered()
{
QApplication::aboutQt();
}
void DigitalOscilloscope::on_action_About_triggered()
{
AboutDialog *about;
about = new AboutDialog(this);
if(about->exec() == QDialog::Rejected)
{
}
about->close();
about->deleteLater();
about = NULL;
}