diff --git a/main.cpp b/main.cpp index aca4f0d..2ca6c59 100755 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); main_window win; + win.read_data(); win.show(); return QApplication::exec(); } diff --git a/main.ui b/main.ui index d02a4b1..70ac1c3 100755 --- a/main.ui +++ b/main.ui @@ -6,8 +6,8 @@ 0 0 - 1489 - 875 + 1089 + 943 @@ -16,29 +16,47 @@ - + Qt::Horizontal - + + + + 300 + 16777215 + + - - - - - 1 - - - - - 1.5 - - - - - 2 - - + + + + port + + + + + + + + + + Baudrate + + + + + + + 115200 + + + + + + + Databits + @@ -65,13 +83,32 @@ - - + + - port + Stopbis + + + + + 1 + + + + + 1.5 + + + + + 2 + + + + @@ -79,9 +116,6 @@ - - - @@ -89,41 +123,13 @@ - - - - Baudrate - - - - - - - Databits - - - - + open - - - - Stopbis - - - - - - - 115200 - - - @@ -132,21 +138,34 @@ 20 - 40 + 574 - - - true + + + Qt::Vertical + + + true + + + + + true + + - - - true + + + + 600 + 0 + @@ -158,13 +177,20 @@ 0 0 - 1489 + 1089 31 + + + QChartView + QGraphicsView +
qchartview.h
+
+
diff --git a/main_window.cpp b/main_window.cpp index 33d6669..f7497d0 100755 --- a/main_window.cpp +++ b/main_window.cpp @@ -5,13 +5,8 @@ #include "main_window.h" #include #include -#include #include -#include -#include #include -#include -#include #include #include @@ -34,8 +29,7 @@ void main_window::config_enable(bool ena) { portOpenBtn->setText("open"), is_open = false; if (receivefile.isOpen()) receivefile.close(); - } - else { + } else { portOpenBtn->setText("close"), is_open = true; auto filename = tmpFilenameEdit->text(); receivefile.setFileName(filename); @@ -44,6 +38,7 @@ void main_window::config_enable(bool ena) { } + void main_window::setUiComponent() { this->ui = new Ui::MainWindow; ui->setupUi(this); @@ -55,11 +50,45 @@ void main_window::setUiComponent() { hexTextEdit = ui->hexTextEdit; asciiTextEdit = ui->asciiTextEdit; + + plotChartView = ui->plotChartView; + portOpenBtn = ui->portOpenBtn; portUpdBtn = ui->portUpdBtn; statusbar = ui->statusbar; + QObject::connect(portOpenBtn, SIGNAL(clicked()), this, SLOT(open_port())); QObject::connect(portUpdBtn, SIGNAL(clicked()), this, SLOT(update_ui_port())); + lineChart.chart->setTitle("WIP"); + lineChart.axis_x->setRange(0, 5); + lineChart.axis_y->setRange(-2.5, 2.5); + lineChart.axis_x->setTickCount(11); + lineChart.axis_y->setTickCount(11); + lineChart.chart->addAxis(lineChart.axis_x, Qt::AlignBottom); + lineChart.chart->addAxis(lineChart.axis_y, Qt::AlignLeft); + lineChart.chart->addSeries(lineChart.data); + plotChartView->setChart(lineChart.chart); + lineChart.data->attachAxis(lineChart.axis_x); + lineChart.data->attachAxis(lineChart.axis_y); + +// linedata = new QLineSeries(); +// chart = new QChart(); +// chart->setTitle("WIP"); +// QValueAxis *axis_x = new QValueAxis(); +// axis_x->setRange(0, 5); +// axis_x->setTickCount(11); +// QValueAxis *axis_y = new QValueAxis(); +// axis_y->setRange(-2.5, 2.5); +// axis_y->setTickCount(11); +// chart->legend()->hide(); +// chart->addAxis(axis_x, Qt::AlignBottom); +// chart->addAxis(axis_y, Qt::AlignLeft); +// chart->addSeries(linedata); +// plotChartView->setChart(chart); +// linedata->attachAxis(axis_x); +// linedata->attachAxis(axis_y); + + } void main_window::port_error_handler(QSerialPort::SerialPortError err) { @@ -84,8 +113,8 @@ 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(); @@ -101,7 +130,7 @@ main_window::main_window(QWidget *parent) : QMainWindow(parent) { } main_window::~main_window() { - delete ui; +// delete ui; } void main_window::update_ui_port() { @@ -116,7 +145,7 @@ void main_window::open_port() { port.close(); config_enable(true); statusbar->showMessage("close port success"); - return ; + return; } if (portsComboBox->count() == 0) { return; } auto cur_port = portsComboBox->currentText(); @@ -136,7 +165,6 @@ void main_window::open_port() { port.setParity(QSerialPort::NoParity); port.setFlowControl(QSerialPort::NoFlowControl); if (port.open(QIODevice::ReadWrite)) { - config_enable(false); statusbar->showMessage("open port success"); } else { @@ -147,13 +175,48 @@ void main_window::open_port() { static uint64_t rest = 0; - -static QLineSeries linedata; static int cnt = 0; static long long line_cnt = 0; +QByteArray zero_finder; +int zero_pos = -1; +int buf_cnt = 0; +int receive_cnt = 0; void main_window::read_data() { auto data = port.readAll(); + static int val = 0; + if (zero_pos == -1) { + if (zero_finder.size() < 10) + zero_finder += data; + if (zero_finder.size() < 10) { + return; + } + int zero_cnt = 0; + for (int i = 0; i < 4; i++) { + if (zero_finder[i] == 0) { + zero_cnt++; + zero_pos = i; + } + } + for (int i = 4; i < zero_finder.size(); i++) { + if (zero_cnt == 1) { + break; + } + if (zero_finder[i - 4] == 0) + zero_cnt--; + if (zero_finder[i] == 0) { + zero_cnt++; + zero_pos = i; + } + } + + for (int i = zero_pos+1; i < zero_finder.size(); i++) { + + + } + } + + asciiTextEdit->append(QString(data)); cnt += data.size(); QString new_text = QString(data.toHex(' ').toUpper()) + " "; @@ -161,22 +224,11 @@ void main_window::read_data() { hexTextEdit->insertPlainText(new_text); auto val = hexTextEdit->toPlainText(); receivefile.write(val.toUtf8()); - if (hexTextEdit->toPlainText().length() > 1e3){ + if (hexTextEdit->toPlainText().length() > 1e3) { hexTextEdit->clear(); asciiTextEdit->clear(); } + lineChart.data->append(0, 0.5); + lineChart.data->append(0.5, 1); } - -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(); - -} \ No newline at end of file diff --git a/main_window.h b/main_window.h index 9073319..5a0b462 100755 --- a/main_window.h +++ b/main_window.h @@ -9,7 +9,23 @@ #include #include #include +#include #include +#include +#include + +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 @@ -45,8 +61,12 @@ private: QTextEdit *asciiTextEdit; QMenuBar *menubar; QStatusBar *statusbar; + QChartView *plotChartView; QMessageBox box; +// QChart* chart; +// QLineSeries* linedata; + GLineChart lineChart; private: void setUiComponent(); @@ -59,9 +79,6 @@ public slots: void read_data(); - void update_chart(); - - void port_error_handler(QSerialPort::SerialPortError err); void config_enable(bool ena);