262 lines
7.9 KiB
C++
Executable File
262 lines
7.9 KiB
C++
Executable File
//
|
|
// Created by zong on 5/18/22.
|
|
//
|
|
|
|
#include "main_window.h"
|
|
#include <QtSerialPort/QSerialPort>
|
|
#include <QtSerialPort/QSerialPortInfo>
|
|
#include <queue>
|
|
#include <cstdint>
|
|
#include <QChartView>
|
|
#include <QWidget>
|
|
|
|
static bool check_exist(QString str) {
|
|
for (auto &port: QSerialPortInfo::availablePorts()) {
|
|
if (str == port.portName())
|
|
return true;
|
|
}
|
|
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::Append);
|
|
receivefileStream.setDevice(&receivefile);
|
|
rawDataFile.setFileName(filename + "raw");
|
|
rawDataFile.open(QIODevice::Append);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
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) {
|
|
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);
|
|
QSerialPortInfo info;
|
|
auto tmp = QSerialPortInfo::availablePorts();
|
|
portsComboBox->clear();
|
|
for (auto &port: tmp) {
|
|
qDebug() << port.portName();
|
|
portsComboBox->addItem(port.portName());
|
|
}
|
|
hexTextEdit->setLineWrapMode(QTextEdit::FixedColumnWidth);
|
|
hexTextEdit->setLineWrapColumnOrWidth(48);
|
|
hexTextEdit->setWordWrapMode(QTextOption::WordWrap);
|
|
hexTextEdit->setFontFamily("Mono");
|
|
|
|
}
|
|
|
|
main_window::~main_window() {
|
|
// delete ui;
|
|
}
|
|
|
|
void main_window::update_ui_port() {
|
|
portsComboBox->clear();
|
|
for (auto &port: QSerialPortInfo::availablePorts()) {
|
|
portsComboBox->addItem(port.portName());
|
|
}
|
|
}
|
|
|
|
void main_window::open_port() {
|
|
if (port.isOpen()) {
|
|
port.close();
|
|
config_enable(true);
|
|
statusbar->showMessage("close port success");
|
|
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 {
|
|
box.setText("wrong stopbits settings"), box.exec();
|
|
return;
|
|
}
|
|
// 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");
|
|
} else {
|
|
config_enable(true);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
static int cnt = 0;
|
|
QByteArray first_frame;
|
|
bool first = true;
|
|
|
|
|
|
void main_window::read_data() {
|
|
static int receive_num_cnt = 0;
|
|
static int receive_byte_cnt = 0;
|
|
static int val = 0;
|
|
auto data = port.readAll();
|
|
if (first) {
|
|
if (data.length() < 10) {
|
|
first_frame += data;
|
|
return;
|
|
}
|
|
while (first_frame[0] != 0) {
|
|
first_frame.remove(0, 1);
|
|
}
|
|
int zero_cnt = 0;
|
|
do {
|
|
zero_cnt = 0;
|
|
while (first_frame[0] != 0) {
|
|
first_frame.remove(0, 1);
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
zero_cnt += first_frame[i] == 0 ? 1 : 0;
|
|
}
|
|
first_frame.remove(0, 1);
|
|
} while (zero_cnt != 0);
|
|
data = first_frame;
|
|
first = false;
|
|
}
|
|
rawDataFile.write(data);
|
|
for (int i = 0; i < data.size(); i++) {
|
|
if (receive_byte_cnt % 4 == 0) {
|
|
if (data[i] != 0) {
|
|
// TODO: raise error
|
|
return;
|
|
}
|
|
receive_num_cnt++;
|
|
int real_number = val;
|
|
if (real_number & 0x800000) {
|
|
// negtive number
|
|
real_number = -(~real_number & 0x7fffff + 1);
|
|
}
|
|
receivefileStream << real_number << " ";
|
|
double scaled = (double) real_number / (double) (0x7fffff) * 2.5;
|
|
lineChart.data->append((double) receive_byte_cnt / 1000.0, scaled);
|
|
while (lineChart.data->count() > 5000) {
|
|
// data more than 5000 second, remove first data
|
|
lineChart.data->remove(0);
|
|
}
|
|
val = 0;
|
|
continue;
|
|
}
|
|
val <<= 8;
|
|
val |= data[i];
|
|
receive_byte_cnt++;
|
|
}
|
|
QString new_text = QString(data.toHex(' ').toUpper()) + " ";
|
|
hexTextEdit->moveCursor(QTextCursor::End);
|
|
hexTextEdit->insertPlainText(new_text);
|
|
auto tmp = hexTextEdit->toPlainText();
|
|
if (tmp.length() > 1e3) {
|
|
hexTextEdit->setPlainText(tmp.last((int) 1e3));
|
|
hexTextEdit->moveCursor(QTextCursor::End);
|
|
}
|
|
tmp = asciiTextEdit->toPlainText();
|
|
if (tmp.length() > 1e3) {
|
|
asciiTextEdit->setPlainText(tmp.last(int(1e3)));
|
|
asciiTextEdit->moveCursor(QTextCursor::End);
|
|
}
|
|
}
|
|
|