当前位置:   article > 正文

Qt 串口数据采集并绘图_qt串口数据接收及图形绘制

qt串口数据接收及图形绘制

VX:13225601016       获取源码和数据集

github:GitHub - supershifeiyu/serial_test: Qt串口数据采集并实时绘图

 serial_test.pro

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2022-03-23T20:06:19
  4. #
  5. #-------------------------------------------------
  6. QT += core gui serialport charts
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = serial_test
  9. TEMPLATE = app
  10. # The following define makes your compiler emit warnings if you use
  11. # any feature of Qt which has been marked as deprecated (the exact warnings
  12. # depend on your compiler). Please consult the documentation of the
  13. # deprecated API in order to know how to port your code away from it.
  14. DEFINES += QT_DEPRECATED_WARNINGS
  15. # You can also make your code fail to compile if you use deprecated APIs.
  16. # In order to do so, uncomment the following line.
  17. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  18. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  19. CONFIG += c++11
  20. SOURCES += \
  21. main.cpp \
  22. widget.cpp \
  23. chart.cpp
  24. HEADERS += \
  25. widget.h \
  26. chart.h
  27. # Default rules for deployment.
  28. qnx: target.path = /tmp/$${TARGET}/bin
  29. else: unix:!android: target.path = /opt/$${TARGET}/bin
  30. !isEmpty(target.path): INSTALLS += target

chart.h

  1. #ifndef CHART_H
  2. #define CHART_H
  3. #include <QtCharts>
  4. #include <QtCharts/QChart>
  5. #include <QtCharts/QChartView>
  6. #include <QtCharts/QSplineSeries>
  7. #include <QDateTimeAxis>
  8. #include <QValueAxis>
  9. #include <QTimer>
  10. #include "QDateTime"
  11. #include <QWidget>
  12. class Chart : public QWidget
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit Chart(QWidget *parent = nullptr);
  17. void init_chart(QString title = tr("无线传输模块调试"), qint16 min =0 ,qint16 max= 30000);//表格初始化
  18. void draw_line(QDateTime current_time, double number);//画线
  19. ~Chart();
  20. private slots:
  21. void save(QDateTime start_time,QDateTime end_time);//保存图表
  22. private:
  23. QTimer *timer; //计时器
  24. QChartView *chartView;
  25. QChart *chart; //画布
  26. QSplineSeries *series; //线
  27. QDateTimeAxis *axisX; //轴
  28. QValueAxis *axisY;
  29. };
  30. #endif // CHART_H

widget.h

  1. #ifndef CHART_H
  2. #define CHART_H
  3. #include <QtCharts>
  4. #include <QtCharts/QChart>
  5. #include <QtCharts/QChartView>
  6. #include <QtCharts/QSplineSeries>
  7. #include <QDateTimeAxis>
  8. #include <QValueAxis>
  9. #include <QTimer>
  10. #include "QDateTime"
  11. #include <QWidget>
  12. class Chart : public QWidget
  13. {
  14. Q_OBJECT
  15. public:
  16. explicit Chart(QWidget *parent = nullptr);
  17. void init_chart(QString title = tr("无线传输模块调试"), qint16 min =0 ,qint16 max= 30000);//表格初始化
  18. void draw_line(QDateTime current_time, double number);//画线
  19. ~Chart();
  20. private slots:
  21. void save(QDateTime start_time,QDateTime end_time);//保存图表
  22. private:
  23. QTimer *timer; //计时器
  24. QChartView *chartView;
  25. QChart *chart; //画布
  26. QSplineSeries *series; //线
  27. QDateTimeAxis *axisX; //轴
  28. QValueAxis *axisY;
  29. };
  30. #endif // CHART_H

chart.cpp

  1. #include "chart.h"
  2. Chart::Chart(QWidget *parent): QWidget(parent)
  3. {
  4. timer = new QTimer(this);
  5. timer->start();
  6. timer->setInterval(1000); //设置定时周期//创建定时器
  7. }
  8. void Chart::init_chart(QString title, qint16 min,qint16 max)
  9. {
  10. QPen penY(Qt::darkBlue,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
  11. chartView = new QChartView(this);
  12. chartView->resize(1600,1000);
  13. chart = new QChart();
  14. chart->setTitle(title);
  15. series = new QSplineSeries;
  16. axisX = new QDateTimeAxis();
  17. axisY = new QValueAxis();
  18. chart->legend()->hide(); //隐藏图例
  19. chart->addSeries(series); //把线添加到chart
  20. chart->setTheme(QChart::ChartThemeDark);
  21. axisX->setTickCount(10); //设置坐标轴格数
  22. axisY->setTickCount(5);
  23. axisX->setFormat("hh:mm:ss"); //设置时间显示格式
  24. axisY->setMin(min); //设置Y轴范围
  25. axisY->setMax(max);
  26. axisX->setTitleText("实时时间"); //设置X轴名称
  27. axisY->setLinePenColor(QColor(Qt::darkBlue)); //设置坐标轴颜色样式
  28. axisY->setGridLineColor(QColor(Qt::darkBlue));
  29. axisY->setGridLineVisible(true); //设置Y轴网格显示
  30. axisY->setLinePen(penY);
  31. axisX->setLinePen(penY);
  32. chart->addAxis(axisX,Qt::AlignBottom); //设置坐标轴位于chart中的位置
  33. chart->addAxis(axisY,Qt::AlignLeft);
  34. series->attachAxis(axisX); //把数据添加到坐标轴上
  35. series->attachAxis(axisY);
  36. axisY->setTitleText("y");
  37. //把chart显示到窗口上
  38. chartView->setChart(chart);
  39. //chartView->setRenderHint(QPainter::Antialiasing); //设置抗锯齿
  40. }
  41. void Chart::draw_line(QDateTime current_time, double number)
  42. {
  43. //设置坐标轴显示范围
  44. chart->axisX()->setMin(QDateTime::currentDateTime().addSecs(-60 * 1)); //系统当前时间的前一秒
  45. chart->axisX()->setMax(QDateTime::currentDateTime().addSecs(0)); //系统当前时间 //这里生成随机数做测试
  46. //增加新的点到曲线末端
  47. series->append(current_time.toMSecsSinceEpoch(), number);
  48. }
  49. void Chart::save(QDateTime start_time,QDateTime end_time)
  50. {
  51. chart->axisX()->setMin(start_time);
  52. chart->axisX()->setMax(end_time);
  53. QPixmap p = chartView->grab();
  54. QImage image = p.toImage();
  55. QString filename = QFileDialog::getSaveFileName(this,
  56. tr("Save Image"),
  57. "",
  58. tr("*.bmp;; *.png;; *.jpg;; *.tif;; *.GIF")); //选择路径
  59. if (filename.isEmpty())
  60. {
  61. return;
  62. }
  63. else
  64. {
  65. if (!(image.save(filename))) //保存图像
  66. {
  67. QMessageBox::information(this,
  68. tr("Failed to save the image"),
  69. tr("Failed to save the image!"));
  70. return;
  71. }
  72. }
  73. }
  74. Chart::~Chart()
  75. {
  76. delete chart;
  77. }

widget.cpp

  1. #include "widget.h"
  2. Widget::Widget(QWidget *parent)
  3. : QWidget(parent)
  4. {
  5. this->setFixedSize(1620,1080);
  6. port = new QComboBox();
  7. port->addItem(tr("端口号"));
  8. port->addItem(tr("COM1"));
  9. port->addItem(tr("COM2"));
  10. port->addItem(tr("COM3"));
  11. port->addItem(tr("COM4"));
  12. port->addItem(tr("COM5"));
  13. baud = new QComboBox();
  14. baud->addItem(tr("波特率"));
  15. baud->addItem(tr("1200"));
  16. baud->addItem(tr("2400"));
  17. baud->addItem(tr("4800"));
  18. baud->addItem(tr("9600"));
  19. baud->addItem(tr("19200"));
  20. baud->addItem(tr("38400"));
  21. baud->addItem(tr("57600"));
  22. baud->addItem(tr("115200"));
  23. start = new QPushButton(tr("开始采集"));
  24. end = new QPushButton(tr("结束采集"));
  25. QHBoxLayout *top_lay = new QHBoxLayout();//上方布局
  26. top_lay->addWidget(port,0);
  27. top_lay->setStretchFactor(port,1);
  28. top_lay->addWidget(baud,1);
  29. top_lay->setStretchFactor(baud,1);
  30. top_lay->addWidget(start,2);
  31. top_lay->setStretchFactor(start,1);
  32. top_lay->addWidget(end,3);
  33. top_lay->setStretchFactor(end,1);
  34. top_lay->setMargin(10);
  35. top_lay->setSpacing(5);
  36. chart = new Chart();
  37. chart->init_chart("无线模块调试",-100,100);
  38. QVBoxLayout *main_lay = new QVBoxLayout(this);
  39. main_lay->addLayout(top_lay,0);
  40. main_lay->setStretchFactor(top_lay,1);
  41. main_lay->addWidget(chart,1);
  42. main_lay->setStretchFactor(main_lay,10);
  43. main_lay->setMargin(10);
  44. main_lay->setSpacing(5);
  45. global_port.setParity(QSerialPort::NoParity);//校验位
  46. global_port.setDataBits(QSerialPort::Data8);//数据位
  47. global_port.setStopBits(QSerialPort::OneStop);//停止位
  48. global_port.setFlowControl(QSerialPort::NoFlowControl);//流控制一般没用
  49. // global_port.setPortName("COM4");
  50. // global_port.setBaudRate(QSerialPort::Baud2400);
  51. connect(start,SIGNAL(clicked()),this,SLOT(start_collect()));
  52. connect(end,SIGNAL(clicked()),this,SLOT(end_collect()));
  53. connect(port,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,&Widget::port_handle);
  54. connect(baud,static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),this,&Widget::baud_handle);
  55. connect(&global_port ,SIGNAL(readyRead()) , this ,SLOT(on_readyRead()));
  56. }
  57. void Widget::port_handle(int index)//端口号槽函数
  58. {
  59. switch(port->currentIndex())
  60. {
  61. case 1:
  62. global_port.setPortName("COM1");
  63. break;
  64. case 2:
  65. global_port.setPortName("COM2");
  66. break;
  67. case 3:
  68. global_port.setPortName("COM3");
  69. break;
  70. case 4:
  71. global_port.setPortName("COM4");
  72. break;
  73. case 5:
  74. global_port.setPortName("COM5");
  75. break;
  76. }
  77. }
  78. void Widget::baud_handle(int index)//波特率槽函数
  79. {
  80. switch(baud->currentIndex())
  81. {
  82. case 1:
  83. global_port.setBaudRate(QSerialPort::Baud1200);
  84. break;
  85. case 2:
  86. global_port.setBaudRate(QSerialPort::Baud2400);
  87. break;
  88. case 3:
  89. global_port.setBaudRate(QSerialPort::Baud4800);
  90. break;
  91. case 4:
  92. global_port.setBaudRate(QSerialPort::Baud9600);
  93. break;
  94. case 5:
  95. global_port.setBaudRate(QSerialPort::Baud19200);
  96. break;
  97. case 6:
  98. global_port.setBaudRate(QSerialPort::Baud38400);
  99. break;
  100. case 7:
  101. global_port.setBaudRate(QSerialPort::Baud57600 );
  102. break;
  103. case 8:
  104. global_port.setBaudRate(QSerialPort::Baud115200);
  105. break;
  106. }
  107. }
  108. void Widget::start_collect()
  109. {
  110. qDebug() << global_port.portName() << global_port.baudRate();
  111. global_port.open(QSerialPort::ReadWrite);
  112. }
  113. void Widget::on_readyRead()
  114. {
  115. buff = global_port.readAll();
  116. double number = (buff[0]-'0')*10 + (buff[1]-'0');
  117. qDebug() << number;
  118. chart->draw_line(QDateTime::currentDateTime(),number);
  119. }
  120. void Widget::end_collect()
  121. {
  122. global_port.close();
  123. }
  124. Widget::~Widget()
  125. {
  126. delete this;
  127. }

main.cpp

  1. #include "widget.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. Widget w;
  7. w.show();
  8. return a.exec();
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/545707
推荐阅读
相关标签
  

闽ICP备14008679号