diff --git a/.gitignore b/.gitignore index 53bd18d..a9bce52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target .idea/ /cmake-build-* +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 026559b..9bea4d9 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ 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 WIN32 main.cpp main_window.cpp main_window.h) target_link_libraries(com diff --git a/GChartView.cpp b/GChartView.cpp index 0ddcb27..6512d6d 100644 --- a/GChartView.cpp +++ b/GChartView.cpp @@ -5,16 +5,33 @@ // 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 +#include +#include +#include +#include +#include +#include +#define MAX_DATA 200 GChartView::GChartView(QWidget *parent) : QChartView(parent) { - data = new QLineSeries(); + 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, 5); - axis_y->setRange(-2.6, 2.6); + 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"); @@ -31,53 +48,49 @@ 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; - - +// 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() > 5000) { -// data->remove(0); -// } -// qDebug() << x << y; -// auto beg = data->at(0); + while (data->count() > MAX_DATA) { + data->remove(0); + } auto end = data->at(data->count()-1); - if (end.x() > 5) { - axis_x->setRange(end.x()-5, end.x()); + if (end.x() > MAX_DATA) { + axis_x->setRange(end.x()-MAX_DATA, end.x()); } } diff --git a/GChartView.h b/GChartView.h index 75ec7ae..13d8f38 100644 --- a/GChartView.h +++ b/GChartView.h @@ -10,6 +10,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE namespace Ui { class gchartview; } @@ -24,7 +25,8 @@ public: void wheelEvent(QWheelEvent *even) override; private: - QLineSeries* data; +// QLineSeries* data; + QSplineSeries *data; QValueAxis* axis_x; QValueAxis* axis_y; QChart* chart; diff --git a/main_window.cpp b/main_window.cpp index 10a3497..96e7010 100755 --- a/main_window.cpp +++ b/main_window.cpp @@ -5,8 +5,6 @@ #include "main_window.h" #include #include -#include -#include #include #include @@ -46,7 +44,6 @@ void main_window::config_enable(bool ena) { } - void main_window::setUiComponent() { this->ui = new Ui::MainWindow; ui->setupUi(this); @@ -130,6 +127,7 @@ void main_window::open_port() { 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); @@ -139,9 +137,16 @@ void main_window::open_port() { return; } // TODO: read info from config - port.setBaudRate(115200); - port.setDataBits(QSerialPort::Data8); - port.setParity(QSerialPort::NoParity); + //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); port.setFlowControl(QSerialPort::NoFlowControl); if (port.open(QIODevice::ReadWrite)) { config_enable(false); @@ -153,126 +158,45 @@ void main_window::open_port() { } -static int cnt = 0; -QByteArray first_frame; -bool first = true; - -#include - -static int receive_num_cnt = -2; -static int receive_byte_cnt = 0; -#define MAX_BUF_LENGTH 1e3 -const double b[4] = {1.0000, -2.6236, 2.3147, -0.6855}; -const double a[4] = {0.0007, 0.0021, 0.0021, 0.0007}; -std::deque in_data; -std::deque out_data; - - +double t = 0; void main_window::read_data() { - static double sum_b = 0; - static double sum_a = 0; - for(int i=0;i<4;i++){ - sum_a += a[i]; - sum_b += b[i]; - - } static qint64 val = 0; auto data = port.readAll(); - 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); -// pre_cursor = asciiTextEdit->textCursor(); -//// asciiTextEdit->moveCursor(QTextCursor::End); -//// asciiTextEdit->insertPlainText(QString(data)); -// if (!move_to_end) asciiTextEdit->setTextCursor(pre_cursor); - - auto tmp = hexTextEdit->toPlainText(); - if (tmp.length() > MAX_BUF_LENGTH) { -// hexTextEdit->setPlainText(tmp.last((int) MAX_BUF_LENGTH)); -// hexTextEdit->moveCursor(QTextCursor::End); - hexTextEdit->clear(); - } -// tmp = asciiTextEdit->toPlainText(); -// if (tmp.length() > MAX_BUF_LENGTH) { -// asciiTextEdit->setPlainText(tmp.last(int(MAX_BUF_LENGTH))); -// asciiTextEdit->moveCursor(QTextCursor::End); + // 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)); // } -// data processing - if (first) { - if (first_frame.length() < 50) { - first_frame += data; - return; - } - int zero_cnt = 0; - do { - zero_cnt = 0; - first_frame.remove(0, 1); -// qDebug() << first_frame; - while (first_frame.size() > 0 && first_frame[0] != 0) { - first_frame.remove(0, 1); - } +// if (hexTextEdit->toPlainText().size() > 10000) { +// hexTextEdit->setText(hexTextEdit->toPlainText().right(1000)); +// } + // parse data to line (ascii data) +// auto lines = QString(data).split('\n'); - for (int i = 0; i < 4 && i < first_frame.size(); i++) { - zero_cnt += (first_frame[i] == 0 ? 1 : 0); - } -// qDebug() <<"zero_cnt "<< zero_cnt ; -// qDebug() << first_frame; - } while (zero_cnt != 1); - data = first_frame; - first = false; - } - // TODO: BUG int data overflow - rawDataFile.write(data); - for (int i = 0; i < data.size(); i++) { - if (receive_byte_cnt % 4 == 0) { - if (data[i] != 0) { - msgBox.setText("Error occurred, please restart transmission."); - msgBox.exec(); - this->open_port(); - first = true; - receive_num_cnt = -2; - receive_byte_cnt = 0; - return; - } - receive_num_cnt++; - qint64 real_number = val; - if ((real_number & 0x800000) != 0) { - // negative number - real_number &= 0x7fffff; - real_number = -(((real_number ^ 0x7fffff) + 1)); - } -// qDebug() << "val: " << val; -// qDebug() << "real_number: " << real_number; - receivefileStream << real_number << " "; - double scaled = (double) real_number / (double) (0x7fffff) * 2.5; - - -// in_data.push_front(scaled); - in_data.push_back(scaled); -// out_data.push_back(0); - if (in_data.size() < 5) - out_data.push_back(0); - else { - double tmp = b[0] * in_data[0] + b[1] * in_data[1] + b[2] * in_data[2] + b[3] * in_data[3]; - tmp = tmp - a[1] * out_data[0] - a[2] * out_data[1] - a[3] * out_data[2]; -// tmp = tmp * sum_b/ sum_a; - out_data.push_back(tmp); - qDebug() << tmp; - out_data.pop_front(); - in_data.pop_front(); - } - -// scaled = (double) out_data[0] / (double) (0x7fffff) * 2.5; - if (receive_num_cnt %50 == 0) - plotChartView->append((double) receive_num_cnt / 1000.0, out_data[0]); - val = 0; - } - val <<= 8; - val |= (data[i] & 0xFF); - receive_byte_cnt++; - } -} +// // 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; +// } +} \ No newline at end of file