update chart

This commit is contained in:
guangzong chen 2022-06-09 21:34:31 -04:00
parent 4ec0e050a5
commit e526ddbc05
No known key found for this signature in database
GPG Key ID: E7F2158184087A5A
4 changed files with 191 additions and 95 deletions

View File

@ -5,6 +5,7 @@
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
main_window win;
win.read_data();
win.show();
return QApplication::exec();
}

124
main.ui
View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1489</width>
<height>875</height>
<width>1089</width>
<height>943</height>
</rect>
</property>
<property name="windowTitle">
@ -16,29 +16,47 @@
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QSplitter" name="splitter">
<widget class="QSplitter" name="splitter_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QWidget" name="layoutWidget">
<widget class="QWidget" name="widget" native="true">
<property name="maximumSize">
<size>
<width>300</width>
<height>16777215</height>
</size>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="1">
<widget class="QComboBox" name="stopbitsComboBox">
<item>
<item row="0" column="0">
<widget class="QPushButton" name="portUpdBtn">
<property name="text">
<string>1</string>
<string>port</string>
</property>
</widget>
</item>
<item>
<item row="0" column="1">
<widget class="QComboBox" name="portsComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>1.5</string>
<string>Baudrate</string>
</property>
</widget>
</item>
<item>
<item row="1" column="1">
<widget class="QLineEdit" name="baudLineEdit">
<property name="text">
<string>2</string>
<string>115200</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Databits</string>
</property>
</widget>
</item>
<item row="2" column="1">
@ -65,13 +83,32 @@
</item>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="portUpdBtn">
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>port</string>
<string>Stopbis</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="stopbitsComboBox">
<item>
<property name="text">
<string>1</string>
</property>
</item>
<item>
<property name="text">
<string>1.5</string>
</property>
</item>
<item>
<property name="text">
<string>2</string>
</property>
</item>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
@ -79,9 +116,6 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="portsComboBox"/>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="tmpFilenameEdit">
<property name="text">
@ -89,41 +123,13 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Baudrate</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Databits</string>
</property>
</widget>
</item>
<item row="5" column="0" colspan="2">
<item row="5" column="0">
<widget class="QPushButton" name="portOpenBtn">
<property name="text">
<string>open</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Stopbis</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="baudLineEdit">
<property name="text">
<string>115200</string>
</property>
</widget>
</item>
<item row="6" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
@ -132,13 +138,17 @@
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<height>574</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QTextEdit" name="hexTextEdit">
<property name="readOnly">
<bool>true</bool>
@ -150,6 +160,15 @@
</property>
</widget>
</widget>
<widget class="QChartView" name="plotChartView">
<property name="minimumSize">
<size>
<width>600</width>
<height>0</height>
</size>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
@ -158,13 +177,20 @@
<rect>
<x>0</x>
<y>0</y>
<width>1489</width>
<width>1089</width>
<height>31</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>QChartView</class>
<extends>QGraphicsView</extends>
<header>qchartview.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
<slots>

View File

@ -5,13 +5,8 @@
#include "main_window.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <iostream>
#include <queue>
#include <QFile>
#include<QMessageBox>
#include <cstdint>
#include <QLineSeries>
#include <QChart>
#include <QChartView>
#include <QWidget>
@ -34,8 +29,7 @@ void main_window::config_enable(bool ena) {
portOpenBtn->setText("open"), is_open = false;
if (receivefile.isOpen())
receivefile.close();
}
else {
} else {
portOpenBtn->setText("close"), is_open = true;
auto filename = tmpFilenameEdit->text();
receivefile.setFileName(filename);
@ -44,6 +38,7 @@ void main_window::config_enable(bool ena) {
}
void main_window::setUiComponent() {
this->ui = new Ui::MainWindow;
ui->setupUi(this);
@ -55,11 +50,45 @@ void main_window::setUiComponent() {
hexTextEdit = ui->hexTextEdit;
asciiTextEdit = ui->asciiTextEdit;
plotChartView = ui->plotChartView;
portOpenBtn = ui->portOpenBtn;
portUpdBtn = ui->portUpdBtn;
statusbar = ui->statusbar;
QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port()));
QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port()));
lineChart.chart->setTitle("WIP");
lineChart.axis_x->setRange(0, 5);
lineChart.axis_y->setRange(-2.5, 2.5);
lineChart.axis_x->setTickCount(11);
lineChart.axis_y->setTickCount(11);
lineChart.chart->addAxis(lineChart.axis_x, Qt::AlignBottom);
lineChart.chart->addAxis(lineChart.axis_y, Qt::AlignLeft);
lineChart.chart->addSeries(lineChart.data);
plotChartView->setChart(lineChart.chart);
lineChart.data->attachAxis(lineChart.axis_x);
lineChart.data->attachAxis(lineChart.axis_y);
// linedata = new QLineSeries();
// chart = new QChart();
// chart->setTitle("WIP");
// QValueAxis *axis_x = new QValueAxis();
// axis_x->setRange(0, 5);
// axis_x->setTickCount(11);
// QValueAxis *axis_y = new QValueAxis();
// axis_y->setRange(-2.5, 2.5);
// axis_y->setTickCount(11);
// chart->legend()->hide();
// chart->addAxis(axis_x, Qt::AlignBottom);
// chart->addAxis(axis_y, Qt::AlignLeft);
// chart->addSeries(linedata);
// plotChartView->setChart(chart);
// linedata->attachAxis(axis_x);
// linedata->attachAxis(axis_y);
}
void main_window::port_error_handler(QSerialPort::SerialPortError err) {
@ -84,8 +113,8 @@ main_window::main_window(QWidget *parent) : QMainWindow(parent) {
setUiComponent();
QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
QObject::connect(&port, &QSerialPort::errorOccurred, this, &main_window::port_error_handler);
QFont font("Courier");
hexTextEdit->setFont(font);
// QFont font("Courier");
// hexTextEdit->setFont(font);
QSerialPortInfo info;
auto tmp = QSerialPortInfo::availablePorts();
portsComboBox->clear();
@ -101,7 +130,7 @@ main_window::main_window(QWidget *parent) : QMainWindow(parent) {
}
main_window::~main_window() {
delete ui;
// delete ui;
}
void main_window::update_ui_port() {
@ -136,7 +165,6 @@ void main_window::open_port() {
port.setParity(QSerialPort::NoParity);
port.setFlowControl(QSerialPort::NoFlowControl);
if (port.open(QIODevice::ReadWrite)) {
config_enable(false);
statusbar->showMessage("open port success");
} else {
@ -147,13 +175,48 @@ void main_window::open_port() {
static uint64_t rest = 0;
static QLineSeries linedata;
static int cnt = 0;
static long long line_cnt = 0;
QByteArray zero_finder;
int zero_pos = -1;
int buf_cnt = 0;
int receive_cnt = 0;
void main_window::read_data() {
auto data = port.readAll();
static int val = 0;
if (zero_pos == -1) {
if (zero_finder.size() < 10)
zero_finder += data;
if (zero_finder.size() < 10) {
return;
}
int zero_cnt = 0;
for (int i = 0; i < 4; i++) {
if (zero_finder[i] == 0) {
zero_cnt++;
zero_pos = i;
}
}
for (int i = 4; i < zero_finder.size(); i++) {
if (zero_cnt == 1) {
break;
}
if (zero_finder[i - 4] == 0)
zero_cnt--;
if (zero_finder[i] == 0) {
zero_cnt++;
zero_pos = i;
}
}
for (int i = zero_pos+1; i < zero_finder.size(); i++) {
}
}
asciiTextEdit->append(QString(data));
cnt += data.size();
QString new_text = QString(data.toHex(' ').toUpper()) + " ";
@ -165,18 +228,7 @@ void main_window::read_data() {
hexTextEdit->clear();
asciiTextEdit->clear();
}
lineChart.data->append(0, 0.5);
lineChart.data->append(0.5, 1);
}
void main_window::update_chart() {
// TODO: memory leakage
QChart *chart = new QChart();
linedata.append(1, 10);
linedata.append(10, 7);
chart->addSeries(&linedata);
chart->createDefaultAxes();
chart->setTitle("WIP");
QChartView *chartview = new QChartView(chart);
chartview->show();
}

View File

@ -9,7 +9,23 @@
#include <QSerialPort>
#include <QFile>
#include <QThread>
#include <QLineSeries>
#include <QMessageBox>
#include <QSplineSeries>
#include <QValueAxis>
typedef struct _Glinechart{
QLineSeries* data;
QValueAxis* axis_x;
QValueAxis* axis_y;
QChart* chart;
_Glinechart() {
data = new QLineSeries();
axis_x = new QValueAxis();
axis_y = new QValueAxis();
chart = new QChart();
}
}GLineChart;
class main_window : public QMainWindow {
Q_OBJECT
@ -45,8 +61,12 @@ private:
QTextEdit *asciiTextEdit;
QMenuBar *menubar;
QStatusBar *statusbar;
QChartView *plotChartView;
QMessageBox box;
// QChart* chart;
// QLineSeries* linedata;
GLineChart lineChart;
private:
void setUiComponent();
@ -59,9 +79,6 @@ public slots:
void read_data();
void update_chart();
void port_error_handler(QSerialPort::SerialPortError err);
void config_enable(bool ena);