This commit is contained in:
guangzong chen 2022-06-16 11:21:33 -04:00
parent 2d0724098b
commit dad61488a1
No known key found for this signature in database
GPG Key ID: E7F2158184087A5A
5 changed files with 226 additions and 186 deletions

1
.gitignore vendored
View File

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

View File

@ -14,11 +14,11 @@ GChartView::GChartView(QWidget *parent) : QChartView(parent) {
chart = new QChart(); chart = new QChart();
chart->setTitle("WIP"); chart->setTitle("WIP");
axis_x->setRange(0, 5); axis_x->setRange(0, 5);
axis_y->setRange(-2.5, 2.5); axis_y->setRange(-2.6, 2.6);
axis_x->setTickType(QValueAxis::TickType::TicksDynamic); // axis_x->setTickType(QValueAxis::TickType::TicksDynamic);
axis_x->setTickInterval(1); axis_x->setTickInterval(1);
axis_x->setLabelFormat("%d"); axis_x->setLabelFormat("%d");
axis_y->setTickCount(21); // axis_y->setTickCount(21);
chart->addAxis(axis_x, Qt::AlignBottom); chart->addAxis(axis_x, Qt::AlignBottom);
chart->addAxis(axis_y, Qt::AlignLeft); chart->addAxis(axis_y, Qt::AlignLeft);
chart->addSeries(data); chart->addSeries(data);
@ -70,13 +70,14 @@ void GChartView::wheelEvent(QWheelEvent *event) {
void GChartView::append(qreal x, qreal y) { void GChartView::append(qreal x, qreal y) {
data->append(x, y); data->append(x, y);
while (data->count() > 5000) { // while (data->count() > 5000) {
data->remove(0); // data->remove(0);
} // }
auto beg = data->at(0); // qDebug() << x << y;
auto end = data->at(data->count()); // auto beg = data->at(0);
auto end = data->at(data->count()-1);
if (end.x() > 5) { if (end.x() > 5) {
axis_x->setRange(beg.x(), end.x()); axis_x->setRange(end.x()-5, end.x());
} }
} }

View File

@ -38,10 +38,10 @@ void main_window::config_enable(bool ena) {
portOpenBtn->setText("close"), is_open = true; portOpenBtn->setText("close"), is_open = true;
auto filename = tmpFilenameEdit->text(); auto filename = tmpFilenameEdit->text();
receivefile.setFileName(filename); receivefile.setFileName(filename);
receivefile.open(QIODevice::Append); receivefile.open(QIODevice::WriteOnly);
receivefileStream.setDevice(&receivefile); receivefileStream.setDevice(&receivefile);
rawDataFile.setFileName(filename + "raw"); rawDataFile.setFileName(filename + "raw");
rawDataFile.open(QIODevice::Append); rawDataFile.open(QIODevice::WriteOnly);
} }
} }
@ -157,84 +157,122 @@ static int cnt = 0;
QByteArray first_frame; QByteArray first_frame;
bool first = true; bool first = true;
#define MAX_BUF_LENGTH 1e4 #include <deque>
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<double> in_data;
std::deque<double> out_data;
void main_window::read_data() { void main_window::read_data() {
static int receive_num_cnt = -2; static double sum_b = 0;
static int receive_byte_cnt = 0; static double sum_a = 0;
static int val = 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 data = port.readAll();
auto pre_cursor =hexTextEdit->textCursor(); auto pre_cursor = hexTextEdit->textCursor();
QString new_text = QString(data.toHex(' ').toUpper()) + " "; QString new_text = QString(data.toHex(' ').toUpper()) + " ";
hexTextEdit->moveCursor(QTextCursor::End); hexTextEdit->moveCursor(QTextCursor::End);
bool move_to_end = pre_cursor == hexTextEdit->textCursor(); bool move_to_end = pre_cursor == hexTextEdit->textCursor();
hexTextEdit->insertPlainText(new_text); hexTextEdit->insertPlainText(new_text);
if (!move_to_end) hexTextEdit->setTextCursor(pre_cursor); if (!move_to_end) hexTextEdit->setTextCursor(pre_cursor);
pre_cursor = asciiTextEdit->textCursor(); // pre_cursor = asciiTextEdit->textCursor();
asciiTextEdit->moveCursor(QTextCursor::End); //// asciiTextEdit->moveCursor(QTextCursor::End);
asciiTextEdit->insertPlainText(QString(data)); //// asciiTextEdit->insertPlainText(QString(data));
if (!move_to_end) asciiTextEdit->setTextCursor(pre_cursor); // if (!move_to_end) asciiTextEdit->setTextCursor(pre_cursor);
auto tmp = hexTextEdit->toPlainText(); auto tmp = hexTextEdit->toPlainText();
if (tmp.length() > MAX_BUF_LENGTH) { if (tmp.length() > MAX_BUF_LENGTH) {
hexTextEdit->setPlainText(tmp.last((int) MAX_BUF_LENGTH)); // hexTextEdit->setPlainText(tmp.last((int) MAX_BUF_LENGTH));
hexTextEdit->moveCursor(QTextCursor::End); // hexTextEdit->moveCursor(QTextCursor::End);
hexTextEdit->clear();
} }
tmp = asciiTextEdit->toPlainText(); // tmp = asciiTextEdit->toPlainText();
if (tmp.length() > MAX_BUF_LENGTH) { // if (tmp.length() > MAX_BUF_LENGTH) {
asciiTextEdit->setPlainText(tmp.last(int(MAX_BUF_LENGTH))); // asciiTextEdit->setPlainText(tmp.last(int(MAX_BUF_LENGTH)));
asciiTextEdit->moveCursor(QTextCursor::End); // asciiTextEdit->moveCursor(QTextCursor::End);
// }
// 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);
}
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++;
} }
// data processing
// if (first) {
// if (data.length() < 10) {
// first_frame += data;
// return;
// }
// while (first_frame.size() > 0 && first_frame[0] != 0) {
// first_frame.remove(0, 1);
// }
// int zero_cnt = 0;
// do {
// zero_cnt = 0;
// while (first_frame.size() > 0 && first_frame[0] != 0) {
// first_frame.remove(0, 1);
// }
//
// for (int i = 0; i < 4 && i < first_frame.size(); i++) {
// zero_cnt += first_frame[i] == 0 ? 1 : 0;
// }
// first_frame.remove(0, 1);
// } while (zero_cnt != 0);
// 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();
// return;
// }
// receive_num_cnt++;
// if (receive_num_cnt == -1)
// continue;
// int real_number = val;
// if (real_number & 0x800000) {
// // negative number
// real_number = -(~real_number & 0x7fffff + 1);
// }
// receivefileStream << real_number << " ";
// double scaled = (double) real_number / (double) (0x7fffff) * 2.5;
// plotChartView->append ((double) receive_num_cnt / 1000.0, scaled);
// val = 0;
// continue;
// }
// val <<= 8;
// val |= data[i];
// }
} }

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