Compare commits
No commits in common. "0f77f98fe4d89e1f1a94936f96f387e7a945d646" and "f7941a4a5b80420daf11b8eabb77d79ecc35edf1" have entirely different histories.
0f77f98fe4
...
f7941a4a5b
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,4 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
.idea/
|
.idea/
|
||||||
/cmake-build-*
|
|
||||||
build/
|
|
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.21)
|
cmake_minimum_required(VERSION 3.21)
|
||||||
project(com)
|
project(untitled)
|
||||||
#set(CMAKE_PREFIX_PATH "C:\\Programs\\Qt\\6.2.4\\msvc2019_64")
|
set(CMAKE_PREFIX_PATH "C:\\Programs\\Qt\\6.2.4\\msvc2019_64")
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
set(CMAKE_AUTOMOC ON)
|
set(CMAKE_AUTOMOC ON)
|
||||||
set(CMAKE_AUTORCC ON)
|
set(CMAKE_AUTORCC ON)
|
||||||
@ -15,10 +15,9 @@ find_package(Qt6 COMPONENTS
|
|||||||
Charts
|
Charts
|
||||||
REQUIRED)
|
REQUIRED)
|
||||||
|
|
||||||
include_directories(./)
|
add_executable(untitled main.cpp main_window.cpp main_window.h)
|
||||||
add_executable(com main.cpp main_window.cpp main_window.h GChartView.cpp GChartView.h)
|
|
||||||
#add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h)
|
#add_executable(untitled WIN32 main.cpp main_window.cpp main_window.h)
|
||||||
target_link_libraries(com
|
target_link_libraries(untitled
|
||||||
Qt::Core
|
Qt::Core
|
||||||
Qt::Gui
|
Qt::Gui
|
||||||
Qt::Widgets
|
Qt::Widgets
|
||||||
|
@ -1,96 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by zong on 6/10/22.
|
|
||||||
//
|
|
||||||
|
|
||||||
// You may need to build the project (run Qt uic code generator) to get "ui_gchartview.h" resolved
|
|
||||||
|
|
||||||
#include "GChartView.h"
|
|
||||||
#include "QtCharts/QScatterSeries"
|
|
||||||
#include <QtCharts/QChart>
|
|
||||||
#include <QtCharts/QChartView>
|
|
||||||
#include <QtCharts/QSplineSeries>
|
|
||||||
#include <QtCharts/QScatterSeries>
|
|
||||||
#include <QtCharts/QValueAxis>
|
|
||||||
#include <QtWidgets/QApplication>
|
|
||||||
#include <QtWidgets/QMainWindow>
|
|
||||||
|
|
||||||
|
|
||||||
#define MAX_DATA 200
|
|
||||||
GChartView::GChartView(QWidget *parent) : QChartView(parent) {
|
|
||||||
data = new QSplineSeries();
|
|
||||||
// Customize spline series
|
|
||||||
data->setPen(QPen(Qt::blue)); // Set the color of the spline line
|
|
||||||
|
|
||||||
// Customize scatter series
|
|
||||||
// data->setMarkerSize(10.0); // Set the size of the scatter points
|
|
||||||
data->setPointsVisible(true); // Set the scatter points visible or not
|
|
||||||
data->setBrush(QBrush(Qt::red)); // Set the color of the scatter points
|
|
||||||
|
|
||||||
axis_x = new QValueAxis();
|
|
||||||
axis_y = new QValueAxis();
|
|
||||||
chart = new QChart();
|
|
||||||
chart->setTitle("WIP");
|
|
||||||
axis_x->setRange(0, MAX_DATA);
|
|
||||||
axis_y->setRange(-6, 7);
|
|
||||||
// axis_x->setTickType(QValueAxis::TickType::TicksDynamic);
|
|
||||||
axis_x->setTickInterval(1);
|
|
||||||
axis_x->setLabelFormat("%d");
|
|
||||||
// axis_y->setTickCount(21);
|
|
||||||
chart->addAxis(axis_x, Qt::AlignBottom);
|
|
||||||
chart->addAxis(axis_y, Qt::AlignLeft);
|
|
||||||
chart->addSeries(data);
|
|
||||||
this->setChart(chart);
|
|
||||||
data->attachAxis(axis_x);
|
|
||||||
data->attachAxis(axis_y);
|
|
||||||
}
|
|
||||||
|
|
||||||
GChartView::~GChartView() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void GChartView::wheelEvent(QWheelEvent *event) {
|
|
||||||
// auto degrees = event->angleDelta();
|
|
||||||
// auto cursor_y = event->position().y();
|
|
||||||
// auto cur_range_max = axis_y->max();
|
|
||||||
// auto cur_range_min = axis_y->min();
|
|
||||||
//
|
|
||||||
// // left and top corner is origin
|
|
||||||
// auto pos_max = chart->mapToPosition(QPointF(0, cur_range_min)).y();
|
|
||||||
// auto pos_min = chart->mapToPosition(QPointF(0, cur_range_max)).y();
|
|
||||||
// auto new_range = 5.0;
|
|
||||||
// auto center = (qreal) 0.0;
|
|
||||||
// if (cursor_y < pos_max && cursor_y > pos_min) {
|
|
||||||
// center = chart->mapToValue(QPointF(0, cursor_y)).y();
|
|
||||||
// auto cur_range = (cur_range_max - cur_range_min);
|
|
||||||
// if (degrees.y() > 0)
|
|
||||||
// new_range = cur_range * 0.8;
|
|
||||||
// else
|
|
||||||
// new_range = cur_range / 0.8;
|
|
||||||
// }
|
|
||||||
// // setup new axis
|
|
||||||
// auto new_max = (center + new_range / 2.0);
|
|
||||||
// auto new_min = (center - new_range / 2.0);
|
|
||||||
// if (new_max > 2.5) {
|
|
||||||
// new_min -= (new_max - 2.5);
|
|
||||||
// new_max = 2.5;
|
|
||||||
// }
|
|
||||||
// if (new_min < -2.5) {
|
|
||||||
// new_max += (-2.5 - new_min);
|
|
||||||
// new_min = -2.5;
|
|
||||||
// new_max = new_max > 2.5 ? 2.5 : new_max;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// axis_y->setRange(new_min, new_max);
|
|
||||||
// qDebug() << "new_min: " << new_min << " " << new_max;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GChartView::append(qreal x, qreal y) {
|
|
||||||
data->append(x, y);
|
|
||||||
while (data->count() > MAX_DATA) {
|
|
||||||
data->remove(0);
|
|
||||||
}
|
|
||||||
auto end = data->at(data->count()-1);
|
|
||||||
if (end.x() > MAX_DATA) {
|
|
||||||
axis_x->setRange(end.x()-MAX_DATA, end.x());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
40
GChartView.h
40
GChartView.h
@ -1,40 +0,0 @@
|
|||||||
//
|
|
||||||
// Created by zong on 6/10/22.
|
|
||||||
//
|
|
||||||
|
|
||||||
#ifndef COM_GCHARTVIEW_H
|
|
||||||
#define COM_GCHARTVIEW_H
|
|
||||||
|
|
||||||
#include <QChartView>
|
|
||||||
|
|
||||||
#include <QLineSeries>
|
|
||||||
#include <QValueAxis>
|
|
||||||
#include <QChart>
|
|
||||||
#include <QSplineSeries>
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
|
||||||
namespace Ui { class gchartview; }
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
class GChartView : public QChartView {
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit GChartView(QWidget *parent = nullptr);
|
|
||||||
~GChartView() override;
|
|
||||||
void wheelEvent(QWheelEvent *even) override;
|
|
||||||
|
|
||||||
private:
|
|
||||||
// QLineSeries* data;
|
|
||||||
QSplineSeries *data;
|
|
||||||
QValueAxis* axis_x;
|
|
||||||
QValueAxis* axis_y;
|
|
||||||
QChart* chart;
|
|
||||||
public:
|
|
||||||
|
|
||||||
void append(qreal x, qreal y);
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif //COM_GCHARTVIEW_H
|
|
1
main.cpp
1
main.cpp
@ -5,7 +5,6 @@
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
main_window win;
|
main_window win;
|
||||||
win.read_data();
|
|
||||||
win.show();
|
win.show();
|
||||||
return QApplication::exec();
|
return QApplication::exec();
|
||||||
}
|
}
|
||||||
|
154
main.ui
154
main.ui
@ -6,8 +6,8 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>2028</width>
|
<width>1489</width>
|
||||||
<height>1035</height>
|
<height>875</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
@ -16,47 +16,29 @@
|
|||||||
<widget class="QWidget" name="centralwidget">
|
<widget class="QWidget" name="centralwidget">
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QSplitter" name="splitter_2">
|
<widget class="QSplitter" name="splitter">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
<enum>Qt::Horizontal</enum>
|
<enum>Qt::Horizontal</enum>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="widget" native="true">
|
<widget class="QWidget" name="layoutWidget">
|
||||||
<property name="maximumSize">
|
|
||||||
<size>
|
|
||||||
<width>300</width>
|
|
||||||
<height>16777215</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
<item row="0" column="0">
|
<item row="3" column="1">
|
||||||
<widget class="QPushButton" name="portUpdBtn">
|
<widget class="QComboBox" name="stopbitsComboBox">
|
||||||
<property name="text">
|
<item>
|
||||||
<string>port</string>
|
<property name="text">
|
||||||
</property>
|
<string>1</string>
|
||||||
</widget>
|
</property>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1">
|
<item>
|
||||||
<widget class="QComboBox" name="portsComboBox"/>
|
<property name="text">
|
||||||
</item>
|
<string>1.5</string>
|
||||||
<item row="1" column="0">
|
</property>
|
||||||
<widget class="QLabel" name="label">
|
</item>
|
||||||
<property name="text">
|
<item>
|
||||||
<string>Baudrate</string>
|
<property name="text">
|
||||||
</property>
|
<string>2</string>
|
||||||
</widget>
|
</property>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="1">
|
|
||||||
<widget class="QLineEdit" name="baudLineEdit">
|
|
||||||
<property name="text">
|
|
||||||
<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>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="1">
|
<item row="2" column="1">
|
||||||
@ -83,32 +65,13 @@
|
|||||||
</item>
|
</item>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="0">
|
<item row="0" column="0">
|
||||||
<widget class="QLabel" name="label_3">
|
<widget class="QPushButton" name="portUpdBtn">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Stopbis</string>
|
<string>port</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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">
|
<item row="4" column="0">
|
||||||
<widget class="QLabel" name="label_4">
|
<widget class="QLabel" name="label_4">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
@ -116,6 +79,9 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="0" column="1">
|
||||||
|
<widget class="QComboBox" name="portsComboBox"/>
|
||||||
|
</item>
|
||||||
<item row="4" column="1">
|
<item row="4" column="1">
|
||||||
<widget class="QLineEdit" name="tmpFilenameEdit">
|
<widget class="QLineEdit" name="tmpFilenameEdit">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
@ -123,13 +89,41 @@
|
|||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="5" column="0">
|
<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">
|
||||||
<widget class="QPushButton" name="portOpenBtn">
|
<widget class="QPushButton" name="portOpenBtn">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>open</string>
|
<string>open</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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">
|
<item row="6" column="0">
|
||||||
<spacer name="verticalSpacer">
|
<spacer name="verticalSpacer">
|
||||||
<property name="orientation">
|
<property name="orientation">
|
||||||
@ -138,34 +132,21 @@
|
|||||||
<property name="sizeHint" stdset="0">
|
<property name="sizeHint" stdset="0">
|
||||||
<size>
|
<size>
|
||||||
<width>20</width>
|
<width>20</width>
|
||||||
<height>574</height>
|
<height>40</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QSplitter" name="splitter">
|
<widget class="QTextEdit" name="hexTextEdit">
|
||||||
<property name="orientation">
|
<property name="readOnly">
|
||||||
<enum>Qt::Vertical</enum>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QTextEdit" name="hexTextEdit">
|
|
||||||
<property name="readOnly">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
<widget class="QTextEdit" name="asciiTextEdit">
|
|
||||||
<property name="readOnly">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="GChartView" name="plotChartView">
|
<widget class="QTextEdit" name="asciiTextEdit">
|
||||||
<property name="minimumSize">
|
<property name="readOnly">
|
||||||
<size>
|
<bool>true</bool>
|
||||||
<width>600</width>
|
|
||||||
<height>0</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
@ -177,20 +158,13 @@
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>2028</width>
|
<width>1489</width>
|
||||||
<height>31</height>
|
<height>31</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
<widget class="QStatusBar" name="statusbar"/>
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
</widget>
|
</widget>
|
||||||
<customwidgets>
|
|
||||||
<customwidget>
|
|
||||||
<class>GChartView</class>
|
|
||||||
<extends>QGraphicsView</extends>
|
|
||||||
<header>GChartView.h</header>
|
|
||||||
</customwidget>
|
|
||||||
</customwidgets>
|
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
<slots>
|
<slots>
|
||||||
|
190
main_window.cpp
190
main_window.cpp
@ -5,8 +5,10 @@
|
|||||||
#include "main_window.h"
|
#include "main_window.h"
|
||||||
#include <QtSerialPort/QSerialPort>
|
#include <QtSerialPort/QSerialPort>
|
||||||
#include <QtSerialPort/QSerialPortInfo>
|
#include <QtSerialPort/QSerialPortInfo>
|
||||||
#include <QChartView>
|
#include <iostream>
|
||||||
#include <QWidget>
|
#include <queue>
|
||||||
|
#include <QFile>
|
||||||
|
#include<QMessageBox>
|
||||||
|
|
||||||
static bool check_exist(QString str) {
|
static bool check_exist(QString str) {
|
||||||
for (auto &port: QSerialPortInfo::availablePorts()) {
|
for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||||
@ -16,81 +18,26 @@ static bool check_exist(QString str) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QTextStream receivefileStream;
|
|
||||||
|
|
||||||
|
|
||||||
void main_window::config_enable(bool ena) {
|
|
||||||
portsComboBox->setEnabled(ena);
|
|
||||||
stopbitsComboBox->setEnabled(ena);
|
|
||||||
baudLineEdit->setEnabled(ena);
|
|
||||||
dataBitsComboBox->setEnabled(ena);
|
|
||||||
tmpFilenameEdit->setEnabled(ena);
|
|
||||||
portUpdBtn->setEnabled(ena);
|
|
||||||
if (ena) {
|
|
||||||
portOpenBtn->setText("open"), is_open = false;
|
|
||||||
if (receivefile.isOpen())
|
|
||||||
receivefile.close();
|
|
||||||
if (rawDataFile.isOpen())
|
|
||||||
rawDataFile.close();
|
|
||||||
} else {
|
|
||||||
portOpenBtn->setText("close"), is_open = true;
|
|
||||||
auto filename = tmpFilenameEdit->text();
|
|
||||||
receivefile.setFileName(filename);
|
|
||||||
receivefile.open(QIODevice::WriteOnly);
|
|
||||||
receivefileStream.setDevice(&receivefile);
|
|
||||||
rawDataFile.setFileName(filename + "raw");
|
|
||||||
rawDataFile.open(QIODevice::WriteOnly);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void main_window::setUiComponent() {
|
void main_window::setUiComponent() {
|
||||||
this->ui = new Ui::MainWindow;
|
this->ui = new Ui::MainWindow;
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
portsComboBox = ui->portsComboBox;
|
portsComboBox = ui->portsComboBox;
|
||||||
stopbitsComboBox = ui->stopbitsComboBox;
|
|
||||||
baudLineEdit = ui->baudLineEdit;
|
|
||||||
dataBitsComboBox = ui->dataBitsComboBox;
|
|
||||||
tmpFilenameEdit = ui->tmpFilenameEdit;
|
|
||||||
|
|
||||||
hexTextEdit = ui->hexTextEdit;
|
hexTextEdit = ui->hexTextEdit;
|
||||||
asciiTextEdit = ui->asciiTextEdit;
|
tmpFilenameEdit = ui->tmpFilenameEdit;
|
||||||
|
|
||||||
plotChartView = ui->plotChartView;
|
|
||||||
|
|
||||||
portOpenBtn = ui->portOpenBtn;
|
portOpenBtn = ui->portOpenBtn;
|
||||||
|
portsComboBox = ui->portsComboBox;
|
||||||
portUpdBtn = ui->portUpdBtn;
|
portUpdBtn = ui->portUpdBtn;
|
||||||
statusbar = ui->statusbar;
|
statusbar = ui->statusbar;
|
||||||
|
stopbitsComboBox = ui->stopbitsComboBox;
|
||||||
QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port()));
|
QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port()));
|
||||||
QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port()));
|
QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port()));
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void main_window::port_error_handler(QSerialPort::SerialPortError err) {
|
|
||||||
switch (err) {
|
|
||||||
case QSerialPort::NoError:
|
|
||||||
break;
|
|
||||||
case QSerialPort::DeviceNotFoundError:
|
|
||||||
case QSerialPort::PermissionError:
|
|
||||||
case QSerialPort::OpenError:
|
|
||||||
case QSerialPort::WriteError:
|
|
||||||
case QSerialPort::ReadError:
|
|
||||||
case QSerialPort::ResourceError:
|
|
||||||
case QSerialPort::UnsupportedOperationError:
|
|
||||||
case QSerialPort::UnknownError:
|
|
||||||
case QSerialPort::TimeoutError:
|
|
||||||
case QSerialPort::NotOpenError:
|
|
||||||
this->config_enable(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main_window::main_window(QWidget *parent) : QMainWindow(parent) {
|
main_window::main_window(QWidget *parent) : QMainWindow(parent) {
|
||||||
setUiComponent();
|
setUiComponent();
|
||||||
QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
|
QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
|
||||||
QObject::connect(&port, &QSerialPort::errorOccurred, this, &main_window::port_error_handler);
|
QFont font("Courier");
|
||||||
// QFont font("Courier");
|
hexTextEdit->setFont(font);
|
||||||
// hexTextEdit->setFont(font);
|
|
||||||
QSerialPortInfo info;
|
QSerialPortInfo info;
|
||||||
auto tmp = QSerialPortInfo::availablePorts();
|
auto tmp = QSerialPortInfo::availablePorts();
|
||||||
portsComboBox->clear();
|
portsComboBox->clear();
|
||||||
@ -117,86 +64,93 @@ void main_window::update_ui_port() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main_window::open_port() {
|
void main_window::open_port() {
|
||||||
if (port.isOpen()) {
|
if (is_open) {
|
||||||
port.close();
|
port.close();
|
||||||
config_enable(true);
|
QFile file;
|
||||||
statusbar->showMessage("close port success", 500);
|
auto filename = tmpFilenameEdit->text();
|
||||||
|
file.setFileName(filename);
|
||||||
|
file.open(QIODevice::Append);
|
||||||
|
auto val = hexTextEdit->toPlainText();
|
||||||
|
hexTextEdit->clear();
|
||||||
|
file.write(val.toUtf8());
|
||||||
|
file.close();
|
||||||
|
is_open = false;
|
||||||
|
portOpenBtn->setText("open");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (portsComboBox->count() == 0) { return; }
|
if (portsComboBox->count() == 0) { return; }
|
||||||
auto cur_port = portsComboBox->currentText();
|
auto cur_port = portsComboBox->currentText();
|
||||||
if (!check_exist(cur_port)) return;
|
if (!check_exist(cur_port)) return;
|
||||||
port.setPortName(cur_port);
|
port.setPortName(cur_port);
|
||||||
|
|
||||||
auto stopbits = stopbitsComboBox->currentText();
|
auto stopbits = stopbitsComboBox->currentText();
|
||||||
if (stopbits == QString("1")) port.setStopBits(QSerialPort::OneStop);
|
if (stopbits == QString("1")) port.setStopBits(QSerialPort::OneStop);
|
||||||
else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop);
|
else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop);
|
||||||
else if (stopbits == QString("2")) port.setStopBits(QSerialPort::TwoStop);
|
else if (stopbits == QString("2")) port.setStopBits(QSerialPort::TwoStop);
|
||||||
else {
|
else {
|
||||||
msgBox.setText("wrong stopbits settings"), msgBox.exec();
|
QMessageBox box;
|
||||||
|
box.setText("wrong stopbits settings");
|
||||||
|
box.exec();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO: read info from config
|
|
||||||
//get baudrate
|
|
||||||
port.setBaudRate(baudLineEdit->text().toInt());
|
|
||||||
|
|
||||||
// get databits
|
// TODO: read info from config
|
||||||
auto databits = dataBitsComboBox->currentText();
|
port.setBaudRate(115200);
|
||||||
if (databits == QString("5")) port.setDataBits(QSerialPort::Data5);
|
port.setDataBits(QSerialPort::Data8);
|
||||||
else if (databits == QString("6")) port.setDataBits(QSerialPort::Data6);
|
port.setParity(QSerialPort::NoParity);
|
||||||
else if (databits == QString("7")) port.setDataBits(QSerialPort::Data7);
|
|
||||||
else if (databits == QString("8"))
|
|
||||||
port.setParity(QSerialPort::NoParity);
|
|
||||||
port.setFlowControl(QSerialPort::NoFlowControl);
|
port.setFlowControl(QSerialPort::NoFlowControl);
|
||||||
if (port.open(QIODevice::ReadWrite)) {
|
if (port.open(QIODevice::ReadWrite)) {
|
||||||
config_enable(false);
|
is_open = true;
|
||||||
statusbar->showMessage("open port success", 500);
|
portOpenBtn->setText("close");
|
||||||
} else {
|
statusbar->showMessage("open port success");
|
||||||
config_enable(true);
|
} else
|
||||||
}
|
is_open = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <QLineSeries>
|
||||||
|
|
||||||
|
static uint64_t rest = 0;
|
||||||
|
|
||||||
|
static QLineSeries linedata;
|
||||||
|
static int cnt = 0;
|
||||||
|
static long long line_cnt = 0;
|
||||||
|
|
||||||
double t = 0;
|
|
||||||
void main_window::read_data() {
|
void main_window::read_data() {
|
||||||
static qint64 val = 0;
|
|
||||||
auto data = port.readAll();
|
auto data = port.readAll();
|
||||||
// save data to file
|
// for (int i = 0; i < data.size(); i++) {
|
||||||
receivefileStream << data;
|
// linedata.append(line_cnt++, int(data[i]));
|
||||||
// auto pre_cursor = hexTextEdit->textCursor();
|
|
||||||
// QString new_text = QString(data.toHex(' ').toUpper()) + " ";
|
|
||||||
// hexTextEdit->moveCursor(QTextCursor::End);
|
|
||||||
// bool move_to_end = pre_cursor == hexTextEdit->textCursor();
|
|
||||||
// hexTextEdit->insertPlainText(new_text);
|
|
||||||
// if (!move_to_end) hexTextEdit->setTextCursor(pre_cursor);
|
|
||||||
// auto tmp = hexTextEdit->toPlainText();
|
|
||||||
//
|
|
||||||
// asciiTextEdit->setText(asciiTextEdit->toPlainText() + QString(data));
|
|
||||||
// // move cursor to end
|
|
||||||
// asciiTextEdit->moveCursor(QTextCursor::End);
|
|
||||||
// // clean up ascii text nad hex text if too long
|
|
||||||
// if (asciiTextEdit->toPlainText().size() > 10000) {
|
|
||||||
// asciiTextEdit->setText(asciiTextEdit->toPlainText().right(1000));
|
|
||||||
// }
|
// }
|
||||||
// if (hexTextEdit->toPlainText().size() > 10000) {
|
cnt += data.size();
|
||||||
// hexTextEdit->setText(hexTextEdit->toPlainText().right(1000));
|
QString new_text = QString(data.toHex(' ').toUpper()) + " ";
|
||||||
// }
|
hexTextEdit->moveCursor(QTextCursor::End);
|
||||||
// parse data to line (ascii data)
|
hexTextEdit->insertPlainText(new_text);
|
||||||
// auto lines = QString(data).split('\n');
|
if (cnt > 1e3) {
|
||||||
|
QFile file;
|
||||||
|
auto filename = tmpFilenameEdit->text();
|
||||||
|
file.setFileName(filename);
|
||||||
|
file.open(QIODevice::Append);
|
||||||
|
auto val = hexTextEdit->toPlainText();
|
||||||
|
hexTextEdit->clear();
|
||||||
|
file.write(val.toUtf8());
|
||||||
|
file.close();
|
||||||
|
cnt = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// // if line contains 5 float number, then give the first one to plot
|
#include <QChart>
|
||||||
// for (auto &line: lines) {
|
#include <QChartView>
|
||||||
// auto tmp = line.split(' ');
|
#include <QWidget>
|
||||||
//// if (tmp.size() != 5) continue;
|
|
||||||
// // convert first number to float (check if it is a number)
|
void main_window::update_chart() {
|
||||||
// bool ok = false;
|
// TODO: memory leakage
|
||||||
// auto data = tmp[0].toFloat(&ok);
|
QChart *chart = new QChart();
|
||||||
// if (!ok) continue;
|
linedata.append(1, 10);
|
||||||
// if (data > 6) data=0.0;
|
linedata.append(10, 7);
|
||||||
// t += 1;
|
chart->addSeries(&linedata);
|
||||||
// plotChartView->append(t, data);
|
chart->createDefaultAxes();
|
||||||
// qDebug() << data;
|
chart->setTitle("WIP");
|
||||||
// }
|
QChartView *chartview = new QChartView(chart);
|
||||||
|
chartview->show();
|
||||||
|
|
||||||
}
|
}
|
@ -7,25 +7,7 @@
|
|||||||
|
|
||||||
#include "ui_main.h"
|
#include "ui_main.h"
|
||||||
#include <QSerialPort>
|
#include <QSerialPort>
|
||||||
#include <QFile>
|
|
||||||
#include <QThread>
|
#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 {
|
class main_window : public QMainWindow {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@ -38,8 +20,6 @@ private:
|
|||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
bool is_open = false;
|
bool is_open = false;
|
||||||
QSerialPort port;
|
QSerialPort port;
|
||||||
QFile receivefile;
|
|
||||||
QFile rawDataFile;
|
|
||||||
|
|
||||||
QWidget *centralwidget;
|
QWidget *centralwidget;
|
||||||
QHBoxLayout *horizontalLayout;
|
QHBoxLayout *horizontalLayout;
|
||||||
@ -62,10 +42,6 @@ private:
|
|||||||
QTextEdit *asciiTextEdit;
|
QTextEdit *asciiTextEdit;
|
||||||
QMenuBar *menubar;
|
QMenuBar *menubar;
|
||||||
QStatusBar *statusbar;
|
QStatusBar *statusbar;
|
||||||
GChartView *plotChartView;
|
|
||||||
|
|
||||||
QMessageBox msgBox;
|
|
||||||
// GLineChart lineChart;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setUiComponent();
|
void setUiComponent();
|
||||||
@ -78,9 +54,8 @@ public slots:
|
|||||||
|
|
||||||
void read_data();
|
void read_data();
|
||||||
|
|
||||||
void port_error_handler(QSerialPort::SerialPortError err);
|
void update_chart();
|
||||||
|
|
||||||
void config_enable(bool ena);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
146
main_windows.cpp
146
main_windows.cpp
@ -1,75 +1,75 @@
|
|||||||
////
|
|
||||||
//// Created by zong on 5/18/22.
|
|
||||||
////
|
|
||||||
//
|
//
|
||||||
//#include "main_windows.h"
|
// Created by zong on 5/18/22.
|
||||||
//#include <QtSerialPort/QSerialPort>
|
|
||||||
//#include <QtSerialPort/QSerialPortInfo>
|
|
||||||
//#include <iostream>
|
|
||||||
//#include <queue>
|
|
||||||
//
|
//
|
||||||
//static bool check_exist(QString str) {
|
|
||||||
// for (auto &port: QSerialPortInfo::availablePorts()) {
|
#include "main_windows.h"
|
||||||
// if (str == port.portName())
|
#include <QtSerialPort/QSerialPort>
|
||||||
// return true;
|
#include <QtSerialPort/QSerialPortInfo>
|
||||||
// }
|
#include <iostream>
|
||||||
// return false;
|
#include <queue>
|
||||||
//}
|
|
||||||
//
|
static bool check_exist(QString str) {
|
||||||
//main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||||
// ui->setupUi(this);
|
if (str == port.portName())
|
||||||
// QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
|
return true;
|
||||||
// QSerialPortInfo info;
|
}
|
||||||
// auto tmp = QSerialPortInfo::availablePorts();
|
return false;
|
||||||
// ui->comboBox->clear();
|
}
|
||||||
// for (auto &port: tmp) {
|
|
||||||
// qDebug() << port.portName();
|
main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||||
// ui->comboBox->addItem(port.portName());
|
ui->setupUi(this);
|
||||||
// }
|
QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
|
||||||
//
|
QSerialPortInfo info;
|
||||||
//}
|
auto tmp = QSerialPortInfo::availablePorts();
|
||||||
//
|
ui->comboBox->clear();
|
||||||
//main_windows::~main_windows() {
|
for (auto &port: tmp) {
|
||||||
// delete ui;
|
qDebug() << port.portName();
|
||||||
//}
|
ui->comboBox->addItem(port.portName());
|
||||||
//
|
}
|
||||||
//void main_windows::update_ui_port() {
|
|
||||||
// ui->comboBox->clear();
|
}
|
||||||
// for (auto &port: QSerialPortInfo::availablePorts()) {
|
|
||||||
// ui->comboBox->addItem(port.portName());
|
main_windows::~main_windows() {
|
||||||
// }
|
delete ui;
|
||||||
//}
|
}
|
||||||
//
|
|
||||||
//void main_windows::open_port() {
|
void main_windows::update_ui_port() {
|
||||||
// if (is_open) {
|
ui->comboBox->clear();
|
||||||
// port.close();
|
for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||||
// is_open = false;
|
ui->comboBox->addItem(port.portName());
|
||||||
// ui->pushButton_2->setText("open");
|
}
|
||||||
// return;
|
}
|
||||||
// }
|
|
||||||
// if (ui->comboBox->count() == 0) { return; }
|
void main_windows::open_port() {
|
||||||
// auto cur_port = ui->comboBox->currentText();
|
if (is_open) {
|
||||||
// if (!check_exist(cur_port)) return;
|
port.close();
|
||||||
// port.setPortName(cur_port);
|
is_open = false;
|
||||||
// port.setStopBits(QSerialPort::OneStop);
|
ui->pushButton_2->setText("open");
|
||||||
// port.setBaudRate(115200);
|
return;
|
||||||
// port.setDataBits(QSerialPort::Data8);
|
}
|
||||||
// port.setParity(QSerialPort::NoParity);
|
if (ui->comboBox->count() == 0) { return; }
|
||||||
// port.setFlowControl(QSerialPort::NoFlowControl);
|
auto cur_port = ui->comboBox->currentText();
|
||||||
// if (port.open(QIODevice::ReadWrite)) {
|
if (!check_exist(cur_port)) return;
|
||||||
// is_open = true;
|
port.setPortName(cur_port);
|
||||||
// ui->pushButton_2->setText("close");
|
port.setStopBits(QSerialPort::OneStop);
|
||||||
// qDebug() << "open port success";
|
port.setBaudRate(115200);
|
||||||
// } else
|
port.setDataBits(QSerialPort::Data8);
|
||||||
// is_open = false;
|
port.setParity(QSerialPort::NoParity);
|
||||||
//
|
port.setFlowControl(QSerialPort::NoFlowControl);
|
||||||
//}
|
if (port.open(QIODevice::ReadWrite)) {
|
||||||
//
|
is_open = true;
|
||||||
//void main_windows::read_data() {
|
ui->pushButton_2->setText("close");
|
||||||
// auto data = port.readAll();
|
qDebug() << "open port success";
|
||||||
// qDebug() << data;
|
} else
|
||||||
// auto tmp = data.toHex(' ').toUpper();
|
is_open = false;
|
||||||
// auto new_text = QString(tmp);
|
|
||||||
// ui->textEdit->insertPlainText(new_text);
|
}
|
||||||
//// auto text = ui->textEdit-
|
|
||||||
//}
|
void main_windows::read_data() {
|
||||||
|
auto data = port.readAll();
|
||||||
|
qDebug() << data;
|
||||||
|
auto tmp = data.toHex(' ').toUpper();
|
||||||
|
auto new_text = QString(tmp);
|
||||||
|
ui->textEdit->insertPlainText(new_text);
|
||||||
|
// auto text = ui->textEdit-
|
||||||
|
}
|
||||||
|
@ -1,36 +1,36 @@
|
|||||||
////
|
|
||||||
//// Created by zong on 5/18/22.
|
|
||||||
////
|
|
||||||
//
|
//
|
||||||
//#ifndef UNTITLED_MAIN_WINDOWS_H
|
// Created by zong on 5/18/22.
|
||||||
//#define UNTITLED_MAIN_WINDOWS_H
|
|
||||||
//
|
//
|
||||||
//#include "ui_main.h"
|
|
||||||
//#include <QSerialPort>
|
#ifndef UNTITLED_MAIN_WINDOWS_H
|
||||||
//#include <QThread>
|
#define UNTITLED_MAIN_WINDOWS_H
|
||||||
//
|
|
||||||
//class main_windows : public QMainWindow {
|
#include "ui_main.h"
|
||||||
//Q_OBJECT
|
#include <QSerialPort>
|
||||||
//public:
|
#include <QThread>
|
||||||
// explicit main_windows(QWidget *parent = nullptr);
|
|
||||||
//
|
class main_windows : public QMainWindow {
|
||||||
// ~main_windows() override;
|
Q_OBJECT
|
||||||
//
|
public:
|
||||||
//private:
|
explicit main_windows(QWidget *parent = nullptr);
|
||||||
// Ui::MainWindow *ui;
|
|
||||||
// bool is_open = false;
|
~main_windows() override;
|
||||||
//
|
|
||||||
//
|
private:
|
||||||
// QSerialPort port;
|
Ui::MainWindow *ui;
|
||||||
//
|
bool is_open = false;
|
||||||
//public slots:
|
|
||||||
//
|
|
||||||
// void update_ui_port();
|
QSerialPort port;
|
||||||
//
|
|
||||||
// void open_port();
|
public slots:
|
||||||
//
|
|
||||||
// void read_data();
|
void update_ui_port();
|
||||||
//};
|
|
||||||
//
|
void open_port();
|
||||||
//
|
|
||||||
//#endif //UNTITLED_MAIN_WINDOWS_H
|
void read_data();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //UNTITLED_MAIN_WINDOWS_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user