279 lines
8.7 KiB
C++
Executable File
279 lines
8.7 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::WriteOnly);
|
|
receivefileStream.setDevice(&receivefile);
|
|
rawDataFile.setFileName(filename + "raw");
|
|
rawDataFile.open(QIODevice::WriteOnly);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
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", 500);
|
|
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 {
|
|
msgBox.setText("wrong stopbits settings"), msgBox.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", 500);
|
|
} else {
|
|
config_enable(true);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
static int cnt = 0;
|
|
QByteArray first_frame;
|
|
bool first = true;
|
|
|
|
#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() {
|
|
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);
|
|
// }
|
|
// 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++;
|
|
}
|
|
}
|
|
|