Compare commits

..

10 Commits

Author SHA1 Message Date
0f77f98fe4
update 2023-11-08 09:35:59 -05:00
guangzong chen
dad61488a1
update 2022-06-16 11:21:33 -04:00
Guangzong
2d0724098b
update 2022-06-13 10:38:50 -04:00
Guangzong
cfd6706fa5 windows compatible 2022-06-10 20:21:11 -04:00
guangzong chen
fc5eb437d0
update chart zoom in and out 2022-06-10 19:19:38 -04:00
guangzong chen
c6db7dc49e
data processing 2022-06-10 00:16:29 -04:00
guangzong chen
e526ddbc05
update chart 2022-06-09 21:34:31 -04:00
guangzong chen
4ec0e050a5
update config settings 2022-06-09 11:09:26 -04:00
guangzong chen
b065b81358
update project name 2022-06-08 14:06:04 -04:00
guangzong chen
16617ed7f7
add ascii display 2022-06-08 14:03:54 -04:00
10 changed files with 490 additions and 253 deletions

4
.gitignore vendored
View File

@ -1,2 +1,4 @@
/target /target
.idea/ .idea/
/cmake-build-*
build/

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.21) cmake_minimum_required(VERSION 3.21)
project(untitled) project(com)
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,9 +15,10 @@ find_package(Qt6 COMPONENTS
Charts Charts
REQUIRED) REQUIRED)
add_executable(untitled main.cpp main_window.cpp main_window.h) include_directories(./)
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(untitled target_link_libraries(com
Qt::Core Qt::Core
Qt::Gui Qt::Gui
Qt::Widgets Qt::Widgets

96
GChartView.cpp Normal file
View File

@ -0,0 +1,96 @@
//
// 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 Normal file
View File

@ -0,0 +1,40 @@
//
// 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

View File

@ -5,6 +5,7 @@
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
View File

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1489</width> <width>2028</width>
<height>875</height> <height>1035</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -16,29 +16,47 @@
<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"> <widget class="QSplitter" name="splitter_2">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </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"> <layout class="QGridLayout" name="gridLayout">
<item row="3" column="1"> <item row="0" column="0">
<widget class="QComboBox" name="stopbitsComboBox"> <widget class="QPushButton" name="portUpdBtn">
<item> <property name="text">
<property name="text"> <string>port</string>
<string>1</string> </property>
</property> </widget>
</item> </item>
<item> <item row="0" column="1">
<property name="text"> <widget class="QComboBox" name="portsComboBox"/>
<string>1.5</string> </item>
</property> <item row="1" column="0">
</item> <widget class="QLabel" name="label">
<item> <property name="text">
<property name="text"> <string>Baudrate</string>
<string>2</string> </property>
</property> </widget>
</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">
@ -65,13 +83,32 @@
</item> </item>
</widget> </widget>
</item> </item>
<item row="0" column="0"> <item row="3" column="0">
<widget class="QPushButton" name="portUpdBtn"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>port</string> <string>Stopbis</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">
@ -79,9 +116,6 @@
</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">
@ -89,41 +123,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="5" 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">
@ -132,21 +138,34 @@
<property name="sizeHint" stdset="0"> <property name="sizeHint" stdset="0">
<size> <size>
<width>20</width> <width>20</width>
<height>40</height> <height>574</height>
</size> </size>
</property> </property>
</spacer> </spacer>
</item> </item>
</layout> </layout>
</widget> </widget>
<widget class="QTextEdit" name="hexTextEdit"> <widget class="QSplitter" name="splitter">
<property name="readOnly"> <property name="orientation">
<bool>true</bool> <enum>Qt::Vertical</enum>
</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="QTextEdit" name="asciiTextEdit"> <widget class="GChartView" name="plotChartView">
<property name="readOnly"> <property name="minimumSize">
<bool>true</bool> <size>
<width>600</width>
<height>0</height>
</size>
</property> </property>
</widget> </widget>
</widget> </widget>
@ -158,13 +177,20 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>1489</width> <width>2028</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>

View File

@ -5,10 +5,8 @@
#include "main_window.h" #include "main_window.h"
#include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo> #include <QtSerialPort/QSerialPortInfo>
#include <iostream> #include <QChartView>
#include <queue> #include <QWidget>
#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()) {
@ -18,26 +16,81 @@ 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;
hexTextEdit = ui->hexTextEdit; stopbitsComboBox = ui->stopbitsComboBox;
baudLineEdit = ui->baudLineEdit;
dataBitsComboBox = ui->dataBitsComboBox;
tmpFilenameEdit = ui->tmpFilenameEdit; tmpFilenameEdit = ui->tmpFilenameEdit;
hexTextEdit = ui->hexTextEdit;
asciiTextEdit = ui->asciiTextEdit;
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()));
QFont font("Courier"); QObject::connect(&port, &QSerialPort::errorOccurred, this, &main_window::port_error_handler);
hexTextEdit->setFont(font); // QFont font("Courier");
// hexTextEdit->setFont(font);
QSerialPortInfo info; QSerialPortInfo info;
auto tmp = QSerialPortInfo::availablePorts(); auto tmp = QSerialPortInfo::availablePorts();
portsComboBox->clear(); portsComboBox->clear();
@ -64,93 +117,86 @@ void main_window::update_ui_port() {
} }
void main_window::open_port() { void main_window::open_port() {
if (is_open) { if (port.isOpen()) {
port.close(); port.close();
QFile file; config_enable(true);
auto filename = tmpFilenameEdit->text(); statusbar->showMessage("close port success", 500);
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 {
QMessageBox box; msgBox.setText("wrong stopbits settings"), msgBox.exec();
box.setText("wrong stopbits settings");
box.exec();
return; return;
} }
// TODO: read info from config // TODO: read info from config
port.setBaudRate(115200); //get baudrate
port.setDataBits(QSerialPort::Data8); port.setBaudRate(baudLineEdit->text().toInt());
port.setParity(QSerialPort::NoParity);
// 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);
port.setFlowControl(QSerialPort::NoFlowControl); port.setFlowControl(QSerialPort::NoFlowControl);
if (port.open(QIODevice::ReadWrite)) { if (port.open(QIODevice::ReadWrite)) {
is_open = true; config_enable(false);
portOpenBtn->setText("close"); statusbar->showMessage("open port success", 500);
statusbar->showMessage("open port success"); } else {
} else config_enable(true);
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;
void main_window::read_data() {
auto data = port.readAll();
// for (int i = 0; i < data.size(); i++) {
// linedata.append(line_cnt++, int(data[i]));
// }
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;
} }
} }
#include <QChart>
#include <QChartView>
#include <QWidget>
void main_window::update_chart() { double t = 0;
// TODO: memory leakage void main_window::read_data() {
QChart *chart = new QChart(); static qint64 val = 0;
linedata.append(1, 10); auto data = port.readAll();
linedata.append(10, 7); // save data to file
chart->addSeries(&linedata); receivefileStream << data;
chart->createDefaultAxes(); // auto pre_cursor = hexTextEdit->textCursor();
chart->setTitle("WIP"); // QString new_text = QString(data.toHex(' ').toUpper()) + " ";
QChartView *chartview = new QChartView(chart); // hexTextEdit->moveCursor(QTextCursor::End);
chartview->show(); // 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) {
// hexTextEdit->setText(hexTextEdit->toPlainText().right(1000));
// }
// parse data to line (ascii data)
// auto lines = QString(data).split('\n');
// // 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;
// }
} }

View File

@ -7,7 +7,25 @@
#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
@ -20,6 +38,8 @@ 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;
@ -42,6 +62,10 @@ 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();
@ -54,8 +78,9 @@ public slots:
void read_data(); void read_data();
void update_chart(); void port_error_handler(QSerialPort::SerialPortError err);
void config_enable(bool ena);
}; };

View File

@ -1,75 +1,75 @@
////
//// Created by zong on 5/18/22.
////
// //
// Created by zong on 5/18/22. //#include "main_windows.h"
//#include <QtSerialPort/QSerialPort>
//#include <QtSerialPort/QSerialPortInfo>
//#include <iostream>
//#include <queue>
// //
//static bool check_exist(QString str) {
#include "main_windows.h" // for (auto &port: QSerialPortInfo::availablePorts()) {
#include <QtSerialPort/QSerialPort> // if (str == port.portName())
#include <QtSerialPort/QSerialPortInfo> // return true;
#include <iostream> // }
#include <queue> // return false;
//}
static bool check_exist(QString str) { //
for (auto &port: QSerialPortInfo::availablePorts()) { //main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
if (str == port.portName()) // ui->setupUi(this);
return true; // QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data()));
} // QSerialPortInfo info;
return false; // auto tmp = QSerialPortInfo::availablePorts();
} // ui->comboBox->clear();
// for (auto &port: tmp) {
main_windows::main_windows(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { // qDebug() << port.portName();
ui->setupUi(this); // ui->comboBox->addItem(port.portName());
QObject::connect(&port, SIGNAL(readyRead()), this, SLOT(read_data())); // }
QSerialPortInfo info; //
auto tmp = QSerialPortInfo::availablePorts(); //}
ui->comboBox->clear(); //
for (auto &port: tmp) { //main_windows::~main_windows() {
qDebug() << port.portName(); // delete ui;
ui->comboBox->addItem(port.portName()); //}
} //
//void main_windows::update_ui_port() {
} // ui->comboBox->clear();
// for (auto &port: QSerialPortInfo::availablePorts()) {
main_windows::~main_windows() { // ui->comboBox->addItem(port.portName());
delete ui; // }
} //}
//
void main_windows::update_ui_port() { //void main_windows::open_port() {
ui->comboBox->clear(); // if (is_open) {
for (auto &port: QSerialPortInfo::availablePorts()) { // port.close();
ui->comboBox->addItem(port.portName()); // is_open = false;
} // ui->pushButton_2->setText("open");
} // return;
// }
void main_windows::open_port() { // if (ui->comboBox->count() == 0) { return; }
if (is_open) { // auto cur_port = ui->comboBox->currentText();
port.close(); // if (!check_exist(cur_port)) return;
is_open = false; // port.setPortName(cur_port);
ui->pushButton_2->setText("open"); // port.setStopBits(QSerialPort::OneStop);
return; // port.setBaudRate(115200);
} // port.setDataBits(QSerialPort::Data8);
if (ui->comboBox->count() == 0) { return; } // port.setParity(QSerialPort::NoParity);
auto cur_port = ui->comboBox->currentText(); // port.setFlowControl(QSerialPort::NoFlowControl);
if (!check_exist(cur_port)) return; // if (port.open(QIODevice::ReadWrite)) {
port.setPortName(cur_port); // is_open = true;
port.setStopBits(QSerialPort::OneStop); // ui->pushButton_2->setText("close");
port.setBaudRate(115200); // qDebug() << "open port success";
port.setDataBits(QSerialPort::Data8); // } else
port.setParity(QSerialPort::NoParity); // is_open = false;
port.setFlowControl(QSerialPort::NoFlowControl); //
if (port.open(QIODevice::ReadWrite)) { //}
is_open = true; //
ui->pushButton_2->setText("close"); //void main_windows::read_data() {
qDebug() << "open port success"; // auto data = port.readAll();
} else // qDebug() << data;
is_open = false; // auto tmp = data.toHex(' ').toUpper();
// 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-
}

View File

@ -1,36 +1,36 @@
////
//// Created by zong on 5/18/22.
////
// //
// Created by zong on 5/18/22. //#ifndef UNTITLED_MAIN_WINDOWS_H
//#define UNTITLED_MAIN_WINDOWS_H
// //
//#include "ui_main.h"
#ifndef UNTITLED_MAIN_WINDOWS_H //#include <QSerialPort>
#define UNTITLED_MAIN_WINDOWS_H //#include <QThread>
//
#include "ui_main.h" //class main_windows : public QMainWindow {
#include <QSerialPort> //Q_OBJECT
#include <QThread> //public:
// explicit main_windows(QWidget *parent = nullptr);
class main_windows : public QMainWindow { //
Q_OBJECT // ~main_windows() override;
public: //
explicit main_windows(QWidget *parent = nullptr); //private:
// Ui::MainWindow *ui;
~main_windows() override; // bool is_open = false;
//
private: //
Ui::MainWindow *ui; // QSerialPort port;
bool is_open = false; //
//public slots:
//
QSerialPort port; // void update_ui_port();
//
public slots: // void open_port();
//
void update_ui_port(); // void read_data();
//};
void open_port(); //
//
void read_data(); //#endif //UNTITLED_MAIN_WINDOWS_H
};
#endif //UNTITLED_MAIN_WINDOWS_H