-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainwindow.cpp
310 lines (267 loc) · 8.87 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <QMessageBox>
#include <sys/time.h>
/*
=======================================
Zoomer 类
=======================================
*/
class MyZoomer: public QwtPlotZoomer
{
public:
MyZoomer(QWidget *canvas):
QwtPlotZoomer(canvas)
{
setTrackerMode(ActiveOnly);
}
virtual QwtText trackerTextF(const QPointF &pos) const
{
QColor bg(Qt::white);
bg.setAlpha(200);
QwtText text = QwtPlotZoomer::trackerTextF(pos);
text.setBackgroundBrush(QBrush(bg));
return text;
}
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->comboBox_2->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
ui->comboBox_2->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
ui->comboBox_2->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
ui->comboBox_2->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
ui->comboBox_2->setCurrentIndex(3);
ui->okButton->setCheckable(true);
ui->clearButton->setCheckable(true);
ui->okButton->setEnabled(false);
/* 设置坐标 */
ui->qwtPlot->setAxisScale(QwtPlot::xBottom, 0, 10*1024);
ui->qwtPlot->setAxisScale(QwtPlot::yLeft, -6096, +6096);
ui->qwtPlot->setAxisFont(QwtPlot::xBottom, QFont("Times", 8, QFont::Normal));
ui->qwtPlot->setAxisFont(QwtPlot::yLeft, QFont("Times", 8, QFont::Normal));
/* 设置画布 */
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setFrameStyle(QFrame::Box|QFrame::Plain);
canvas->setLineWidth(1);
canvas->setPalette(Qt::white);
ui->qwtPlot->setCanvas(canvas);
/* 设置网格 */
QwtPlotGrid *grid = new QwtPlotGrid();
grid->setPen(Qt::gray, 0.0, Qt::DotLine);
grid->attach(ui->qwtPlot);
/* 添加曲线 */
d_curve1 = new QwtPlotCurve();
d_curve1->setPen(Qt::blue, 1);
d_curve1->setRenderHint(QwtPlotItem::RenderAntialiased, false); /* 禁用抗锯齿 */
//d_curve1->setPaintAttribute(QwtPlotCurve::FilterPointsAggressive, true); /* 使能滤波算法 */
d_curve1->setLegendAttribute( QwtPlotCurve::LegendNoAttribute, true);
d_curve1->attach(ui->qwtPlot);
/* 默认左键选中放大,右键恢复上级,CTRL+右键恢复原样 */
zoomer = new MyZoomer(ui->qwtPlot->canvas());
zoomer->setMousePattern(QwtEventPattern::MouseSelect2, Qt::RightButton, Qt::ControlModifier);
zoomer->setMousePattern(QwtEventPattern::MouseSelect3, Qt::RightButton);
/* 左键平移 */
panner = new QwtPlotPanner(ui->qwtPlot->canvas());
panner->setEnabled(false);
/*容器初始化*/
//valuesx.resize(360*2000);
for(int i=0;i<10*1024;i++)
{
valuesx<<(double)i;
}
valuesy.resize(10*1024);
qDebug()<<"valusex/y size"<<valuesx.size()<<valuesy.size();
/*文件初始化*/
file=new QFile();
serial=NULL;
/*按钮初始化*/
if(ui->checkBox_word->isChecked()){
wordOrder=1;
}else{
wordOrder=0;
}
if(ui->checkBox_hex->isChecked()){
bin=1;
}else{
bin=0;
}
}
MainWindow::~MainWindow()
{
delete ui;
}
/* 串口接收数据事件回调函数 */
void MainWindow::MainSerialRecvMsgEvent()
{
// MainSerialRecvData.append(QString(serial->MainSerial->readAll()));
// if(MainSerialRecvData.size()>1000)
// {
// ui->textEdit->append(QString(MainSerialRecvData));
// MainSerialRecvData.clear();
// //file->write(buf);
// }
}
void MainWindow::on_okButton_toggled(bool checked)
{
/*打开串口*/
if (serial==NULL)
{
/*创建线程*/
serial=new serialThread();
connect(serial, SIGNAL(bufFull()), this, SLOT(serialfull()), Qt::QueuedConnection);
/* 判断是否有可用串口 */
if(ui->comboBox_2->count() != 0)
{
/* 串口已经关闭,现在来打开串口 */
/* 设置串口名称 */
serial->MainSerial->setPortName(ui->comboBox_1->currentText());
/* 设置波特率 */
serial->MainSerial->setBaudRate(QString(ui->comboBox_2->currentText()).toInt());
/* 设置数据位数 */
serial->MainSerial->setDataBits(QSerialPort::Data8);
/* 设置奇偶校验 */
serial->MainSerial->setParity(QSerialPort::NoParity);
/* 设置停止位 */
serial->MainSerial->setStopBits(QSerialPort::OneStop);
/* 设置流控制 */
serial->MainSerial->setFlowControl(QSerialPort::NoFlowControl);
/* 打开串口 */
serial->MainSerial->open(QIODevice::ReadWrite);
/* 设置串口缓冲区大小,这里必须设置为这么大 */
serial->MainSerial->setReadBufferSize(MainSerialPortOneFrameSize);
/* 注册回调函数 */
//QObject::connect(MainSerial, &QSerialPort::readyRead, this, &MainWindow::MainSerialRecvMsgEvent);
/*UI*/
ui->okButton->setText(tr("关闭串口"));
ui->okButton->setStyleSheet(QString("QPushButton{color:red}"));
ui->clearButton->setEnabled(false);
serial->start();
file->setFileName("read.dat");
if(!file->open(QIODevice::WriteOnly))
{
qDebug()<<"文件打开失败";
}
qDebug()<<"串口在关闭状态,现在打开了" + ui->comboBox_1->currentText();
}else{
qDebug()<<"没有可用串口,请重新常识扫描串口";
// 警告对话框
QMessageBox::warning(this,tr("警告"),tr("没有可用串口,请重新尝试扫描串口!"),QMessageBox::Ok);
}
}else{
/* 关闭串口 */
serial->MainSerial->close();
ui->okButton->setText(tr("打开串口"));
ui->okButton->setStyleSheet(QString("QPushButton{color:black}"));
ui->clearButton->setEnabled(true);
serial->stop();
//serial->exit();
while(!serial->isFinished()) {;}
disconnect(serial, SIGNAL(bufFull()), this, SLOT(serialfull()));
delete serial;
serial=NULL;
file->close();
qDebug()<<"串口在打开状态,串口关闭";
}
}
void MainWindow::on_clearButton_toggled(bool checked)
{
/* 查找可用串口 */
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
QSerialPort serial;
serial.setPort(info);
/* 判断端口是否能打开 */
if(serial.open(QIODevice::ReadWrite))
{
int isHaveItemInList = 0;
/* 判断是不是已经在列表中了 */
for(int i=0; i<ui->comboBox_2->count(); i++)
{
/* 如果,已经在列表中了,那么不添加这一项了就 */
if(ui->comboBox_1->itemText(i) == serial.portName())
{
isHaveItemInList++;
}
}
if(isHaveItemInList == 0)
{
ui->comboBox_1->addItem(serial.portName());
}
serial.close();
}
}
ui->okButton->setEnabled(true);
}
void MainWindow::serialfull()
{
//gettimeofday(&tpstart,NULL);//debug
union type_t
{
qint8 buf8[BUFSIZE];
qint16 buf16[BUFSIZE/2];
}buf;
/*更新ui*/
if(bin){
ui->textEdit->append(QString(serial->SerialBuf.toHex()));
}else{
}
//qDebug()<<"int buf size"<<fifo.size();
//if(fifo.size()<BUFSIZE*2) return;
for(int i=0;i<BUFSIZE;i++)
{
buf.buf8[i]=serial->SerialBuf.at(i);
}
serial->SerialBuf.remove(0,BUFSIZE);
//qDebug()<<"out buf size"<<serial->SerialBuf.size();
valuesy.erase(valuesy.begin(),valuesy.begin()+BUFSIZE/2);
/*大小端转换*/
if(wordOrder == 0){
for(int i=0;i<BUFSIZE/2;i++)
{
BIG2LITTLE16(buf.buf16[i]);
//valuesy<<(double)buf.buf16[i];
}
}
for(int i=0;i<BUFSIZE/2;i++)
{
valuesy<<(double)buf.buf16[i];
}
/*更新曲线数据*/
d_curve1->setSamples(valuesx,valuesy);
ui->qwtPlot->replot();
//gettimeofday(&tpend,NULL);//debug
//double timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) + tpend.tv_usec-tpstart.tv_usec)/1000000.0;
//qDebug()<<timeuse<<"s";
}
void MainWindow::on_pushButton_clicked()
{
qDebug("clear!");
ui->textEdit->clear();
}
void MainWindow::on_checkBox_word_stateChanged(int arg1)
{
if(ui->checkBox_word->isChecked()){
wordOrder=1;
}else{
wordOrder=0;
}
}
void MainWindow::on_checkBox_hex_stateChanged(int arg1)
{
if(ui->checkBox_hex->isChecked()){
bin=1;
}else{
bin=0;
}
}
void MainWindow::on_pushButton_2_clicked()
{
valuesy.clear();
valuesy.resize(10*1024);
d_curve1->setSamples(valuesx,valuesy);
ui->qwtPlot->replot();
}