Compare commits
No commits in common. "0f77f98fe4d89e1f1a94936f96f387e7a945d646" and "f7941a4a5b80420daf11b8eabb77d79ecc35edf1" have entirely different histories.
0f77f98fe4
...
f7941a4a5b
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,2 @@
|
||||
/target
|
||||
.idea/
|
||||
/cmake-build-*
|
||||
build/
|
||||
|
@ -1,6 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
project(com)
|
||||
#set(CMAKE_PREFIX_PATH "C:\\Programs\\Qt\\6.2.4\\msvc2019_64")
|
||||
project(untitled)
|
||||
set(CMAKE_PREFIX_PATH "C:\\Programs\\Qt\\6.2.4\\msvc2019_64")
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
@ -15,10 +15,9 @@ find_package(Qt6 COMPONENTS
|
||||
Charts
|
||||
REQUIRED)
|
||||
|
||||
include_directories(./)
|
||||
add_executable(com main.cpp main_window.cpp main_window.h GChartView.cpp GChartView.h)
|
||||
add_executable(untitled 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::Gui
|
||||
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[]) {
|
||||
QApplication a(argc, argv);
|
||||
main_window win;
|
||||
win.read_data();
|
||||
win.show();
|
||||
return QApplication::exec();
|
||||
}
|
||||
|
154
main.ui
154
main.ui
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>2028</width>
|
||||
<height>1035</height>
|
||||
<width>1489</width>
|
||||
<height>875</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -16,47 +16,29 @@
|
||||
<widget class="QWidget" name="centralwidget">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QSplitter" name="splitter_2">
|
||||
<widget class="QSplitter" name="splitter">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>300</width>
|
||||
<height>16777215</height>
|
||||
</size>
|
||||
</property>
|
||||
<widget class="QWidget" name="layoutWidget">
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="portUpdBtn">
|
||||
<property name="text">
|
||||
<string>port</string>
|
||||
</property>
|
||||
</widget>
|
||||
</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>Baudrate</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="2" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Databits</string>
|
||||
</property>
|
||||
<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="2" column="1">
|
||||
@ -83,32 +65,13 @@
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QLabel" name="label_3">
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="portUpdBtn">
|
||||
<property name="text">
|
||||
<string>Stopbis</string>
|
||||
<string>port</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">
|
||||
@ -116,6 +79,9 @@
|
||||
</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">
|
||||
@ -123,13 +89,41 @@
|
||||
</property>
|
||||
</widget>
|
||||
</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">
|
||||
<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">
|
||||
@ -138,34 +132,21 @@
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>574</height>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<widget class="QSplitter" name="splitter">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
<widget class="QTextEdit" name="hexTextEdit">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</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 class="GChartView" name="plotChartView">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>600</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
<widget class="QTextEdit" name="asciiTextEdit">
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
@ -177,20 +158,13 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>2028</width>
|
||||
<width>1489</width>
|
||||
<height>31</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusbar"/>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
<class>GChartView</class>
|
||||
<extends>QGraphicsView</extends>
|
||||
<header>GChartView.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
<slots>
|
||||
|
190
main_window.cpp
190
main_window.cpp
@ -5,8 +5,10 @@
|
||||
#include "main_window.h"
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <QChartView>
|
||||
#include <QWidget>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <QFile>
|
||||
#include<QMessageBox>
|
||||
|
||||
static bool check_exist(QString str) {
|
||||
for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||
@ -16,81 +18,26 @@ static bool check_exist(QString str) {
|
||||
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() {
|
||||
this->ui = new Ui::MainWindow;
|
||||
ui->setupUi(this);
|
||||
portsComboBox = ui->portsComboBox;
|
||||
stopbitsComboBox = ui->stopbitsComboBox;
|
||||
baudLineEdit = ui->baudLineEdit;
|
||||
dataBitsComboBox = ui->dataBitsComboBox;
|
||||
tmpFilenameEdit = ui->tmpFilenameEdit;
|
||||
|
||||
hexTextEdit = ui->hexTextEdit;
|
||||
asciiTextEdit = ui->asciiTextEdit;
|
||||
|
||||
plotChartView = ui->plotChartView;
|
||||
|
||||
tmpFilenameEdit = ui->tmpFilenameEdit;
|
||||
portOpenBtn = ui->portOpenBtn;
|
||||
portsComboBox = ui->portsComboBox;
|
||||
portUpdBtn = ui->portUpdBtn;
|
||||
statusbar = ui->statusbar;
|
||||
|
||||
stopbitsComboBox = ui->stopbitsComboBox;
|
||||
QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_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) {
|
||||
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();
|
||||
@ -117,86 +64,93 @@ void main_window::update_ui_port() {
|
||||
}
|
||||
|
||||
void main_window::open_port() {
|
||||
if (port.isOpen()) {
|
||||
if (is_open) {
|
||||
port.close();
|
||||
config_enable(true);
|
||||
statusbar->showMessage("close port success", 500);
|
||||
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();
|
||||
is_open = false;
|
||||
portOpenBtn->setText("open");
|
||||
return;
|
||||
}
|
||||
if (portsComboBox->count() == 0) { return; }
|
||||
auto cur_port = portsComboBox->currentText();
|
||||
if (!check_exist(cur_port)) return;
|
||||
port.setPortName(cur_port);
|
||||
|
||||
auto stopbits = stopbitsComboBox->currentText();
|
||||
if (stopbits == QString("1")) port.setStopBits(QSerialPort::OneStop);
|
||||
else if (stopbits == QString("1.5")) port.setStopBits(QSerialPort::OneAndHalfStop);
|
||||
else if (stopbits == QString("2")) port.setStopBits(QSerialPort::TwoStop);
|
||||
else {
|
||||
msgBox.setText("wrong stopbits settings"), msgBox.exec();
|
||||
QMessageBox box;
|
||||
box.setText("wrong stopbits settings");
|
||||
box.exec();
|
||||
return;
|
||||
}
|
||||
// TODO: read info from config
|
||||
//get baudrate
|
||||
port.setBaudRate(baudLineEdit->text().toInt());
|
||||
|
||||
// get databits
|
||||
auto databits = dataBitsComboBox->currentText();
|
||||
if (databits == QString("5")) port.setDataBits(QSerialPort::Data5);
|
||||
else if (databits == QString("6")) port.setDataBits(QSerialPort::Data6);
|
||||
else if (databits == QString("7")) port.setDataBits(QSerialPort::Data7);
|
||||
else if (databits == QString("8"))
|
||||
port.setParity(QSerialPort::NoParity);
|
||||
// TODO: read info from config
|
||||
port.setBaudRate(115200);
|
||||
port.setDataBits(QSerialPort::Data8);
|
||||
port.setParity(QSerialPort::NoParity);
|
||||
port.setFlowControl(QSerialPort::NoFlowControl);
|
||||
if (port.open(QIODevice::ReadWrite)) {
|
||||
config_enable(false);
|
||||
statusbar->showMessage("open port success", 500);
|
||||
} else {
|
||||
config_enable(true);
|
||||
}
|
||||
is_open = true;
|
||||
portOpenBtn->setText("close");
|
||||
statusbar->showMessage("open port success");
|
||||
} 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() {
|
||||
static qint64 val = 0;
|
||||
auto data = port.readAll();
|
||||
// save data to file
|
||||
receivefileStream << data;
|
||||
// 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));
|
||||
// for (int i = 0; i < data.size(); i++) {
|
||||
// linedata.append(line_cnt++, int(data[i]));
|
||||
// }
|
||||
// if (hexTextEdit->toPlainText().size() > 10000) {
|
||||
// hexTextEdit->setText(hexTextEdit->toPlainText().right(1000));
|
||||
// }
|
||||
// parse data to line (ascii data)
|
||||
// auto lines = QString(data).split('\n');
|
||||
cnt += data.size();
|
||||
QString new_text = QString(data.toHex(' ').toUpper()) + " ";
|
||||
hexTextEdit->moveCursor(QTextCursor::End);
|
||||
hexTextEdit->insertPlainText(new_text);
|
||||
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
|
||||
// for (auto &line: lines) {
|
||||
// auto tmp = line.split(' ');
|
||||
//// if (tmp.size() != 5) continue;
|
||||
// // convert first number to float (check if it is a number)
|
||||
// bool ok = false;
|
||||
// auto data = tmp[0].toFloat(&ok);
|
||||
// if (!ok) continue;
|
||||
// if (data > 6) data=0.0;
|
||||
// t += 1;
|
||||
// plotChartView->append(t, data);
|
||||
// qDebug() << data;
|
||||
// }
|
||||
#include <QChart>
|
||||
#include <QChartView>
|
||||
#include <QWidget>
|
||||
|
||||
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();
|
||||
|
||||
}
|
@ -7,25 +7,7 @@
|
||||
|
||||
#include "ui_main.h"
|
||||
#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
|
||||
@ -38,8 +20,6 @@ private:
|
||||
Ui::MainWindow *ui;
|
||||
bool is_open = false;
|
||||
QSerialPort port;
|
||||
QFile receivefile;
|
||||
QFile rawDataFile;
|
||||
|
||||
QWidget *centralwidget;
|
||||
QHBoxLayout *horizontalLayout;
|
||||
@ -62,10 +42,6 @@ private:
|
||||
QTextEdit *asciiTextEdit;
|
||||
QMenuBar *menubar;
|
||||
QStatusBar *statusbar;
|
||||
GChartView *plotChartView;
|
||||
|
||||
QMessageBox msgBox;
|
||||
// GLineChart lineChart;
|
||||
|
||||
private:
|
||||
void setUiComponent();
|
||||
@ -78,9 +54,8 @@ public slots:
|
||||
|
||||
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"
|
||||
//#include <QtSerialPort/QSerialPort>
|
||||
//#include <QtSerialPort/QSerialPortInfo>
|
||||
//#include <iostream>
|
||||
//#include <queue>
|
||||
// Created by zong on 5/18/22.
|
||||
//
|
||||
//static bool check_exist(QString str) {
|
||||
// for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||
// if (str == port.portName())
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
//}
|
||||
//
|
||||
//main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
// ui->setupUi(this);
|
||||
// QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
|
||||
// QSerialPortInfo info;
|
||||
// auto tmp = QSerialPortInfo::availablePorts();
|
||||
// ui->comboBox->clear();
|
||||
// for (auto &port: tmp) {
|
||||
// qDebug() << port.portName();
|
||||
// ui->comboBox->addItem(port.portName());
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
//main_windows::~main_windows() {
|
||||
// delete ui;
|
||||
//}
|
||||
//
|
||||
//void main_windows::update_ui_port() {
|
||||
// ui->comboBox->clear();
|
||||
// for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||
// ui->comboBox->addItem(port.portName());
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//void main_windows::open_port() {
|
||||
// if (is_open) {
|
||||
// port.close();
|
||||
// is_open = false;
|
||||
// ui->pushButton_2->setText("open");
|
||||
// return;
|
||||
// }
|
||||
// if (ui->comboBox->count() == 0) { return; }
|
||||
// auto cur_port = ui->comboBox->currentText();
|
||||
// if (!check_exist(cur_port)) return;
|
||||
// port.setPortName(cur_port);
|
||||
// port.setStopBits(QSerialPort::OneStop);
|
||||
// port.setBaudRate(115200);
|
||||
// port.setDataBits(QSerialPort::Data8);
|
||||
// port.setParity(QSerialPort::NoParity);
|
||||
// port.setFlowControl(QSerialPort::NoFlowControl);
|
||||
// if (port.open(QIODevice::ReadWrite)) {
|
||||
// is_open = true;
|
||||
// ui->pushButton_2->setText("close");
|
||||
// qDebug() << "open port success";
|
||||
// } else
|
||||
// is_open = false;
|
||||
//
|
||||
//}
|
||||
//
|
||||
//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-
|
||||
//}
|
||||
|
||||
#include "main_windows.h"
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
|
||||
static bool check_exist(QString str) {
|
||||
for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||
if (str == port.portName())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||
ui->setupUi(this);
|
||||
QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
|
||||
QSerialPortInfo info;
|
||||
auto tmp = QSerialPortInfo::availablePorts();
|
||||
ui->comboBox->clear();
|
||||
for (auto &port: tmp) {
|
||||
qDebug() << port.portName();
|
||||
ui->comboBox->addItem(port.portName());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main_windows::~main_windows() {
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void main_windows::update_ui_port() {
|
||||
ui->comboBox->clear();
|
||||
for (auto &port: QSerialPortInfo::availablePorts()) {
|
||||
ui->comboBox->addItem(port.portName());
|
||||
}
|
||||
}
|
||||
|
||||
void main_windows::open_port() {
|
||||
if (is_open) {
|
||||
port.close();
|
||||
is_open = false;
|
||||
ui->pushButton_2->setText("open");
|
||||
return;
|
||||
}
|
||||
if (ui->comboBox->count() == 0) { return; }
|
||||
auto cur_port = ui->comboBox->currentText();
|
||||
if (!check_exist(cur_port)) return;
|
||||
port.setPortName(cur_port);
|
||||
port.setStopBits(QSerialPort::OneStop);
|
||||
port.setBaudRate(115200);
|
||||
port.setDataBits(QSerialPort::Data8);
|
||||
port.setParity(QSerialPort::NoParity);
|
||||
port.setFlowControl(QSerialPort::NoFlowControl);
|
||||
if (port.open(QIODevice::ReadWrite)) {
|
||||
is_open = true;
|
||||
ui->pushButton_2->setText("close");
|
||||
qDebug() << "open port success";
|
||||
} else
|
||||
is_open = false;
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
//#define UNTITLED_MAIN_WINDOWS_H
|
||||
// Created by zong on 5/18/22.
|
||||
//
|
||||
//#include "ui_main.h"
|
||||
//#include <QSerialPort>
|
||||
//#include <QThread>
|
||||
//
|
||||
//class main_windows : public QMainWindow {
|
||||
//Q_OBJECT
|
||||
//public:
|
||||
// explicit main_windows(QWidget *parent = nullptr);
|
||||
//
|
||||
// ~main_windows() override;
|
||||
//
|
||||
//private:
|
||||
// Ui::MainWindow *ui;
|
||||
// bool is_open = false;
|
||||
//
|
||||
//
|
||||
// QSerialPort port;
|
||||
//
|
||||
//public slots:
|
||||
//
|
||||
// void update_ui_port();
|
||||
//
|
||||
// void open_port();
|
||||
//
|
||||
// void read_data();
|
||||
//};
|
||||
//
|
||||
//
|
||||
//#endif //UNTITLED_MAIN_WINDOWS_H
|
||||
|
||||
#ifndef UNTITLED_MAIN_WINDOWS_H
|
||||
#define UNTITLED_MAIN_WINDOWS_H
|
||||
|
||||
#include "ui_main.h"
|
||||
#include <QSerialPort>
|
||||
#include <QThread>
|
||||
|
||||
class main_windows : public QMainWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit main_windows(QWidget *parent = nullptr);
|
||||
|
||||
~main_windows() override;
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
bool is_open = false;
|
||||
|
||||
|
||||
QSerialPort port;
|
||||
|
||||
public slots:
|
||||
|
||||
void update_ui_port();
|
||||
|
||||
void open_port();
|
||||
|
||||
void read_data();
|
||||
};
|
||||
|
||||
|
||||
#endif //UNTITLED_MAIN_WINDOWS_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user