-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
256 lines (229 loc) · 11.4 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDesktopWidget>
#include <qwt_round_scale_draw.h>
#include <qwt_dial_needle.h>
#include "qpoint.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("上位机端");
QPalette bgpal = palette();
bgpal.setColor (QPalette::Background, QColor (192,192,192));
this->setPalette (bgpal);
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
shadow_effect->setOffset(-5, 5);
shadow_effect->setColor(Qt::gray);
shadow_effect->setBlurRadius(8);
ui->centralWidget->setGraphicsEffect(shadow_effect);
ui->baudRateComboBox->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;");
ui->dataBitsComboBox->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;");
ui->parityComboBox->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;");
ui->portNameComboBox->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;");
ui->stopBitsComboBox->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;");
ui->closeMyComBtn->setEnabled(false); //开始“关闭串口”按钮不可用
ui->sendMsgBtn->setEnabled(false); //开始“发送数据”按钮不可用
ui->showVDBtn->setEnabled(false);//开始“形象描述”按钮不可用
// 对表盘进行设置
QwtRoundScaleDraw *scaleDraw = new QwtRoundScaleDraw();
scaleDraw->setSpacing( 8 );
scaleDraw->enableComponent( QwtAbstractScaleDraw::Backbone, false );
scaleDraw->setTickLength( QwtScaleDiv::MinorTick, 0 );
scaleDraw->setTickLength( QwtScaleDiv::MediumTick, 4 );
scaleDraw->setTickLength( QwtScaleDiv::MajorTick, 8 );
ui->Dial_io->setScaleDraw(scaleDraw);
ui->Dial_io->setWrapping( false );
ui->Dial_io->setReadOnly( true );
ui->Dial_io->setOrigin( 135.0 );
ui->Dial_io->setScaleArc( 0.0, 270.0 );
ui->Dial_io->setScaleStepSize( 20.0 );
ui->Dial_io->setScale( 0.0, 240.0 );
ui->Dial_io->scaleDraw()->setPenWidth( 2 );
// 给表盘设置指针
QwtDialSimpleNeedle *needle = new QwtDialSimpleNeedle(
QwtDialSimpleNeedle::Arrow, true, Qt::red,
QColor( Qt::gray ).light( 130 ) );
ui->Dial_io->setNeedle( needle );
// 创建用于更新数据控件值的计时器
updateTimer = new QTimer(this);
updateTimer->setInterval(1000);
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData()));
// 创建用于更新表盘控件值的计时器
updateDialTimer = new QTimer(ui->Dial_io);
updateDialTimer->setInterval(50);
connect( updateDialTimer, SIGNAL( timeout() ), this, SLOT( changeSpeed() ) );
// 创建用于更新温度计值的计时器
updateThermoTimer = new QTimer(ui->Thermo_temperature);
updateThermoTimer->setInterval(50);
connect( updateThermoTimer, SIGNAL( timeout() ), this, SLOT( changeHigh() ) );
temp.clear();
allData.clear();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::updateData() {
nowData = Data::getInstance();
// 测试
//nowData->test();
// 更新各个控件的值
if (nowData->getltf() == 1) ui->frame_left->setStyleSheet("image: url(:/new/prefix1/lightOn)");
if (nowData->getltf() == 0) ui->frame_left->setStyleSheet("image: url(:/new/prefix1/lightOff)");
if (nowData->getlts() == 1) ui->frame_right->setStyleSheet("image: url(:/new/prefix1/lightOn)");
if (nowData->getlts() == 0) ui->frame_right->setStyleSheet("image: url(:/new/prefix1/lightOff)");
ui->lcd_acx->display(Data::getInstance()->getacX());
ui->lcd_acy->display(Data::getInstance()->getacY());
ui->lcd_acz->display(Data::getInstance()->getacZ());
ui->lcd_avx->display(Data::getInstance()->getavX());
ui->lcd_avy->display(Data::getInstance()->getavY());
ui->lcd_avz->display(Data::getInstance()->getavZ());
ui->lcd_anx->display(Data::getInstance()->getanX());
ui->lcd_any->display(Data::getInstance()->getanY());
ui->lcd_anz->display(Data::getInstance()->getanZ());
}
void MainWindow::changeSpeed() {
ui->Dial_io->setValue(Data::getInstance()->getgunValue());
}
void MainWindow::changeHigh() {
ui->Thermo_temperature->setValue(Data::getInstance()->gettemperatureValue());
}
void MainWindow::readMyCom() //读串口函数
{
//使用myCom->bytesAvailable()函数可以判断串口缓冲区中的数据量
//读取串口缓冲区的所有数据给临时变量temp
temp = myCom->readAll();
//将串口的数据显示在窗口的文本浏览器中,调用insertPlainText()函数,是窗口上的文本浏览器中连续输出数据,而不是每次写数据前都清除以前的
//QString类中有字符串切割函数split(特征符)可与at(第几个字符串(从0开始))配合从而读取字符串中特定数据
//将QByteArray的对象直接复制给QString对象即可完成(QByteArray到QString转换)转换
if (!temp.isEmpty())
{
allData.append(temp);
if (allData.contains("d"))
{
QByteArray getPage = allData.split('d').at(0) + 'd';
nowData->analyze(getPage);
ui->textBrowser->insertPlainText(allData.split('d').at(0) + 'd' + '\n' + '\n');
allData = allData.right(allData.length() - allData.indexOf('d') - 1);
}
}
}
void MainWindow::on_openMyComBtn_clicked()
{
QString portName = ui->portNameComboBox->currentText(); //获取串口名
myCom = new Win_QextSerialPort(portName,QextSerialBase::EventDriven); //定义串口对象,并传递参数,在构造函数里对其进行初始化
//myCom = new Win_QextSerialPort("COM1", QextSerialBase::Polling); // 使用Polling模式(查询方式)
//readTimer = new QTimer(this);// 使用Polling模式(查询方式)
//readTimer->start(100); // 设置延时为100ms// 使用Polling模式(查询方式)
myCom ->open(QIODevice::ReadWrite); //打开串口
//设置波特率(根据组合框内容对串口进行设置)
if(ui->baudRateComboBox->currentText()==tr("9600"))
myCom->setBaudRate(BAUD9600);
else if(ui->baudRateComboBox->currentText()==tr("115200"))
myCom->setBaudRate(BAUD115200);
//设置数据位
if(ui->dataBitsComboBox->currentText()==tr("8"))
myCom->setDataBits(DATA_8);
else if(ui->dataBitsComboBox->currentText()==tr("7"))
myCom->setDataBits(DATA_7);
//设置奇偶校验
if(ui->parityComboBox->currentText()==tr("无"))
myCom->setParity(PAR_NONE);
else if(ui->parityComboBox->currentText()==tr("奇"))
myCom->setParity(PAR_ODD);
else if(ui->parityComboBox->currentText()==tr("偶"))
myCom->setParity(PAR_EVEN);
//设置停止位
if(ui->stopBitsComboBox->currentText()==tr("1"))
myCom->setStopBits(STOP_1);
else if(ui->stopBitsComboBox->currentText()==tr("2"))
myCom->setStopBits(STOP_2);
//设置数据流控制,我们使用无数据流控制的默认设置
myCom->setFlowControl(FLOW_OFF);
//设置延时
myCom->setTimeout(500);
//信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
//connect(readTimer, SIGNAL(timeout()), this, SLOT(readMyCom())); // 使用Polling模式(查询方式)
ui->openMyComBtn->setEnabled(false); //打开串口后“打开串口”按钮不可用
ui->closeMyComBtn->setEnabled(true); //打开串口后“关闭串口”按钮可用
ui->sendMsgBtn->setEnabled(true); //打开串口后“发送数据”按钮可用
ui->showVDBtn->setEnabled(true);//打开串口后“形象描述”按钮可用
//设置各个组合框不可用
ui->baudRateComboBox->setEnabled(false);
ui->dataBitsComboBox->setEnabled(false);
ui->parityComboBox->setEnabled(false);
ui->stopBitsComboBox->setEnabled(false);
ui->portNameComboBox->setEnabled(false);
// 数据采集类创建对象并初始化
nowData = Data::getInstance();
nowData->init();
// 开始更新控件数据
updateTimer->start();
updateDialTimer->start();
updateThermoTimer->start();
// //定义一个结构体,用来存放串口各个参数
// struct PortSettings myComSetting = {BAUD9600,DATA_8,PAR_NONE,STOP_1,FLOW_OFF,500};
// //定义串口对象,并传递参数,在构造函数里对其进行初始化
// // 完成了串口的选择和串口的初始化
// myCom = new Win_QextSerialPort("com1",myComSetting,QextSerialBase::EventDriven);
// //以可读写方式打开串口
// myCom ->open(QIODevice::ReadWrite);
// //信号和槽函数关联,当串口缓冲区有数据时,进行读串口操作
// connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
// ui->openMyComBtn->setEnabled(false); //打开串口后“打开串口”按钮不可用
// ui->closeMyComBtn->setEnabled(true); //打开串口后“关闭串口”按钮可用
// ui->sendMsgBtn->setEnabled(true); //打开串口后“发送数据”按钮可用
}
void MainWindow::on_closeMyComBtn_clicked()
{
myCom->close(); //关闭串口,该函数在win_qextserialport.cpp文件中定义
ui->openMyComBtn->setEnabled(true); //关闭串口后“打开串口”按钮可用
ui->closeMyComBtn->setEnabled(false); //关闭串口后“关闭串口”按钮不可用
ui->sendMsgBtn->setEnabled(false); //关闭串口后“发送数据”按钮不可用
ui->showVDBtn->setEnabled(false);//关闭串口后“形象描述”按钮不可用
if(!vDwin.isHidden()) vDwin.hide();
//设置各个组合框可用
ui->baudRateComboBox->setEnabled(true);
ui->dataBitsComboBox->setEnabled(true);
ui->parityComboBox->setEnabled(true);
ui->stopBitsComboBox->setEnabled(true);
ui->portNameComboBox->setEnabled(true);
// 停止更新控件数据
updateTimer->stop();
updateDialTimer->stop();
updateThermoTimer->stop();
nowData->init(); // 数据采集类初始化
// 各控件值初始化
ui->frame_left->setStyleSheet("image: url(:/new/prefix1/lightOff)");
ui->frame_right->setStyleSheet("image: url(:/new/prefix1/lightOff)");
ui->Dial_io->setValue(0);
ui->Thermo_temperature->setValue(0);
ui->lcd_acx->display(0);
ui->lcd_acy->display(0);
ui->lcd_acz->display(0);
ui->lcd_avx->display(0);
ui->lcd_avy->display(0);
ui->lcd_avz->display(0);
ui->lcd_anx->display(0);
ui->lcd_any->display(0);
ui->lcd_anz->display(0);
// myCom->close(); //关闭串口,该函数在win_qextserialport.cpp文件中定义
// ui->openMyComBtn->setEnabled(true); //关闭串口后“打开串口”按钮可用
// ui->closeMyComBtn->setEnabled(false); //关闭串口后“关闭串口”按钮不可用
// ui->sendMsgBtn->setEnabled(false); //关闭串口后“发送数据”按钮不可用
}
void MainWindow::on_sendMsgBtn_clicked()
{
// //以ASCII码形式将行编辑框中的数据写入串口
// myCom->write(ui->sendMsgLineEdit->text().toLatin1());
ui->textBrowser->clear();
}
void MainWindow::on_showVDBtn_clicked()
{
vDwin.show();
vDwin.move((QApplication::desktop()->width() - vDwin.width()) / 2,
(QApplication::desktop()->height() - vDwin.height()) / 2);
}