182 lines
5.3 KiB
C++
Executable File
182 lines
5.3 KiB
C++
Executable File
//
|
|
// Created by zong on 5/18/22.
|
|
//
|
|
|
|
#include "main_window.h"
|
|
#include <QtSerialPort/QSerialPort>
|
|
#include <QtSerialPort/QSerialPortInfo>
|
|
#include <iostream>
|
|
#include <queue>
|
|
#include <QFile>
|
|
#include<QMessageBox>
|
|
#include <cstdint>
|
|
#include <QLineSeries>
|
|
#include <QChart>
|
|
#include <QChartView>
|
|
#include <QWidget>
|
|
|
|
static bool check_exist(QString str) {
|
|
for (auto &port: QSerialPortInfo::availablePorts()) {
|
|
if (str == port.portName())
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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();
|
|
}
|
|
else {
|
|
portOpenBtn->setText("close"), is_open = true;
|
|
auto filename = tmpFilenameEdit->text();
|
|
receivefile.setFileName(filename);
|
|
receivefile.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;
|
|
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");
|
|
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 uint64_t rest = 0;
|
|
|
|
static QLineSeries linedata;
|
|
static int cnt = 0;
|
|
static long long line_cnt = 0;
|
|
|
|
void main_window::read_data() {
|
|
auto data = port.readAll();
|
|
asciiTextEdit->append(QString(data));
|
|
cnt += data.size();
|
|
QString new_text = QString(data.toHex(' ').toUpper()) + " ";
|
|
hexTextEdit->moveCursor(QTextCursor::End);
|
|
hexTextEdit->insertPlainText(new_text);
|
|
auto val = hexTextEdit->toPlainText();
|
|
receivefile.write(val.toUtf8());
|
|
if (hexTextEdit->toPlainText().length() > 1e3){
|
|
hexTextEdit->clear();
|
|
asciiTextEdit->clear();
|
|
}
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
} |