加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mainwindow.cpp 7.28 KB
一键复制 编辑 原始数据 按行查看 历史
sunny.song 提交于 2017-10-12 15:52 . youhua
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCharts/QChart>
#include <QtCharts/QDateTimeAxis>
#include <QLineSeries>
#include <QValueAxis>
#include <QTimer>
#include <QTime>
#include <QList>
#include <qmath.h>
#include <QPointF>
#include <QDebug>
#include <QtWidgets>
#include <QtCore/QDebug>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
//#include <QtCharts/QChartGlobal>
#include <QChartView>
QT_CHARTS_USE_NAMESPACE
QChart *m_chart;
QLineSeries *m_series,*series;
int maxSize = 2000;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),//默认初始化?
ui(new Ui::MainWindow),
mySerialPort(new QSerialPort),
mySerialInfo(0),
btnOpenIsOpen(false),
btnSendIsSend(false),
recvThread(0),
recvBytes(0),
sendBytes(0)
{
ui->setupUi(this);
this->uiInit();
m_chart = new QChart;
QChartView *chartView = new QChartView(m_chart);
chartView->setRubberBand(QChartView::RectangleRubberBand);
/************************first***************************/
m_series = new QLineSeries;
m_series->setName("ADC1");
for(int i=0;i<maxSize;++i){
m_series->append(i,0.1);
}
m_series->setPen(QPen(Qt::red));
m_series->setUseOpenGL(true);//openGl 加速
qDebug()<<m_series->useOpenGL();
/*************************second**************************/
#if 1
series = new QLineSeries;
series->setName("ADC2");
for(int i=0;i<maxSize;++i){
series->append(i,-0.1);
}
series->setPen(QPen(Qt::blue));
series->setUseOpenGL(true);//openGl 加速
qDebug()<<series->useOpenGL();
#endif
/*************************end**************************/
QValueAxis *axisX = new QValueAxis;
axisX->setRange(0,maxSize);
axisX->setLabelFormat("%g");
axisX->setTitleText("time/us");
m_chart->addSeries(m_series);
m_chart->addSeries(series);
m_chart->setAxisX(axisX,m_series);
m_chart->setAxisX(axisX,series);
QValueAxis *axisY = new QValueAxis;
axisY->setRange(-0.5,4.0);
axisY->setTitleText("Voltage/V");
m_chart->setAxisY(axisY,m_series);
m_chart->setAxisY(axisY,series);
//m_chart->legend()->hide();
m_chart->setTitle("振动数据");
QVBoxLayout *layout = ui->verticalLayout;
layout->addWidget(chartView);
}
double MainWindow::getData(double time)
{
double s = qCos( time * M_PI * 2 ) ;
return s;
}
void MainWindow::timerEvent(QTimerEvent *event)
{
#if 0
if(event->timerId()==timeId){//定时器到时间,//模拟数据填充
static QTime dataTime(QTime::currentTime());
long int eltime = dataTime.elapsed();
static int lastpointtime = 0;
int size = (eltime - lastpointtime);//数据个数
qDebug()<<"size-->"<<size;
if(isVisible()){
QVector<QPointF> oldPoints = m_series->pointsVector();//Returns the points in the series as a vector
QVector<QPointF> points;
for(int i=size;i<oldPoints.count();++i){
points.append(QPointF(i-size ,oldPoints.at(i).y()));//替换数据用
}
qint64 sizePoints = points.count();
for(int k=0;k<size;++k){
points.append(QPointF(k+sizePoints,getData((((double)lastpointtime+k+1)/1000))));
}
m_series->replace(points);
lastpointtime = eltime;
}
}
#endif
}
MainWindow::~MainWindow()
{
if(btnOpenIsOpen == true && mySerialInfo != NULL)
delete mySerialInfo;
delete mySerialPort;
delete ui;
}
void MainWindow::uiInit(void)
{
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
/* qDebug() << "Name : " << info.portName();
qDebug() << "Description : " << info.description();
qDebug() << "Manufacturer: " << info.manufacturer(); */
// Example use QSerialPort
QSerialPort serial;
serial.setPort(info);
if (serial.open(QIODevice::ReadWrite))
{
ui->combPort->addItem(info.portName());
serial.close();
}
}
// ui->textbRecv->document()->setMaximumBlockCount(1000);//lines
/* Default parameters */
mySerialPort->setBaudRate(QSerialPort::Baud115200);
mySerialPort->setDataBits(QSerialPort::Data8);
mySerialPort->setStopBits(QSerialPort::OneStop);
mySerialPort->setParity(QSerialPort::NoParity);
mySerialPort->setFlowControl(QSerialPort::NoFlowControl);
}
void MainWindow::showRecv(const QString &s)
{
QString temp3;
QString str1 = s.section('/',1,1);
QString str2 = s.section('/',2,2);
qDebug() <<"s:"<<s;
qDebug() <<"str1:"<<str1;
qDebug() <<"str2:"<<str2;
if(str1.isEmpty()|| str2.isEmpty())
return;
QStringList list1 = str1.split(" ", QString::SkipEmptyParts);
QStringList list2 = str2.split(" ", QString::SkipEmptyParts);
qDebug() << " list1 : " << list1;
qDebug() << " list2 : " << list2;
for(int n = 0;n<list1.length();n++)
{
if("0.00"==list1.at(n))
{
list1.removeAt(n);
n--;
}
}
for( int n = 0;n<list2.length();n++)
{
if("0.00"==list2.at(n))
list2.removeAt(n);
}
int size1 = list1.length();//数据个数
int size2 = list2.length();//数据个数
qDebug()<<"size1-->"<<size1;
qDebug()<<"size2-->"<<size2;
if(isVisible())
{
QVector<QPointF> oldPoints2 = series->pointsVector();//Returns the points in the series as a vector
QVector<QPointF> points2;
for(int j=size2;j<oldPoints2.count();++j){
points2.append(QPointF(j-size2 ,oldPoints2.at(j).y()));//替换数据用
}
qint64 sizePoints2 = points2.count();
for(int m=0;m<size2;m++){
temp3 = list2.at(m);
points2.append(QPointF(m+sizePoints2,temp3.toDouble()));
}
series->replace(points2);
QVector<QPointF> oldPoints1 = m_series->pointsVector();//Returns the points in the series as a vector
QVector<QPointF> points1;
for(int i=size1;i<oldPoints1.count();++i){
points1.append(QPointF(i-size1 ,oldPoints1.at(i).y()));//替换数据用
}
qint64 sizePoints1 = points1.count();
for(int k=0;k<size1;k++){
temp3 = list1.at(k);
points1.append(QPointF(k+sizePoints1,temp3.toDouble()*40));
}
m_series->replace(points1);
// ui->textbRecv->append(s);
}
//ui->textbRecv->moveCursor(QTextCursor::End);
}
void MainWindow::on_btnOpen_clicked()
{
if(btnOpenIsOpen == false)
{
btnOpenIsOpen = true;
ui->btnOpen->setText(tr("Close"));
mySerialInfo = new QSerialPortInfo(ui->combPort->currentText());
mySerialPort->setPort(*mySerialInfo);
mySerialPort->open(QIODevice::ReadWrite);
recvThread = new RecvThread(this);
connect(recvThread, SIGNAL(recvOk(QString)), this, SLOT(showRecv(QString)));
connect(recvThread, &RecvThread::finished, recvThread, &RecvThread::deleteLater);
recvThread->startRecv(mySerialPort, 1 * 1000);
}
else
{
btnOpenIsOpen = false;
ui->btnOpen->setText(tr("Open"));
delete recvThread;
recvThread = NULL;
mySerialPort->close();
delete mySerialInfo;
mySerialInfo = NULL;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化