#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QSerialPortInfo>
#include <fstream>
#include <iostream>
#include <QDebug>
#include <QString>
#include <QThread>
#include <QTime>
#include <QTimer>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QBitmap>
#include <QPainter>
#define stopport ":/stopload"
#define loadport ":/load"
#define bmostlittle ":/mostlittle"
#define bmostlarge ":/mostlarge"
#define bclose ":/close"
#define porticon ":/portico"
#define closeswitch ":/closeswitch"
#define onswitch ":/onswitch"
#define rclose ":/hclose"
#define rmostlittle ":/hlittel"
#define rmostlarge ":/hlarge"
#define logo ":/12"
#define wlogo ":/13"
#define root "D:"
//构造函数
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
port = new QSerialPort(this);
ui->openport->setIcon(QPixmap(stopport));
ui->little->setPixmap(QPixmap(bmostlittle).scaled(16,16));
ui->large->setPixmap(QPixmap(bmostlarge).scaled(16,16));
ui->close->setPixmap(QPixmap(bclose).scaled(16,16));
ui->icon->setPixmap(QPixmap(porticon).scaled(25,25));
ui->switch_2->setPixmap(QPixmap(closeswitch).scaled(25,25));
ui->openport->setIconSize(QSize(32,32));
ui->openport->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
ui->checknum->setMaximumWidth(50);
ui->parity_out->setMaximumWidth(60);
ui->lengthbtn->setMinimumWidth(70);
ui->checkbtn->setMinimumWidth(100);
ui->binary->setMaximumWidth(60);
ui->rclear->setMinimumWidth(100);
ui->rsave->setMinimumWidth(100);
ui->udapteport->setMinimumWidth(100);
ui->senddata->setMinimumHeight(50);
ui->length->setMaximumWidth(30);
ui->portname->setMaximumWidth(240);
ui->label->setMaximumHeight(30);
ui->stoptimesend->setDisabled(true);
ui->textEdit->setPlaceholderText("请输入16进制格式,并以空格分开.例如 01 3c");
ui->rhex->setChecked(Qt::CheckState(2));
ui->hex->setChecked(Qt::CheckState(2));
//将控件设置为悬停控件,安装事件过滤器
sethoverevent();
//设置无边框
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
//设置窗口标题
setWindowTitle("串口调试助手");
//遍历串口
updateport();
//设置标准波特率
auto Bound = QSerialPortInfo::standardBaudRates();
for(auto val: Bound)
ui->bound->addItem(QString::number(val),val);
ui->bound->setCurrentText("115200");
//实例化子线程对象
portreceiver = new receive_data(this);
//连接主子线程信号
connect(this,&MainWindow::senddataval,portreceiver,&receive_data::receive);
//接受子线程处理后的数据
connect(portreceiver,&receive_data::sendportdata,this,[=](QString val){
//再加一个判断是因为不同模式的数据插入方式不一样insertHtml,insertPlainText
if(ui->rhex->isChecked())
ui->textBrowser->insertHtml(val);
else
ui->textBrowser->insertPlainText(val);
});
//窗口销毁时子线程退出
connect(this,&MainWindow::destroyed,this,[=](){
portreceiver->quit();
});
//链接串口信号
connect(port,&QSerialPort::readyRead,this,[=](){
static int k =0;
k++;
if(k==1)
//启动线程
portreceiver->start();
//每次默认只读10个数据
QByteArray arry = port->read(length);
//将数据发送至子线程处理
emit senddataval(arry,ui->timefreq->isChecked(),ui->rhex->isChecked(),ui->rtext->isChecked(),ui->rflie->isChecked());
});
//进制选择
connect(ui->binary,&QComboBox::currentTextChanged,this,[=](QString text)
{
if(text == "HEX")
{
ui->parity_out->setText(QString("%1").arg(check_num,0,16));
}
else if(text == "DEC")
{
ui->parity_out->setText(QString("%1").arg(check_num,0,10));
}
else if(text == "BIN")
{
ui->parity_out->setText(QString("%1").arg(check_num,0,2));
}
});
//校验选择
connect(ui->parity_check,&QComboBox::currentTextChanged,this,[=](QString text)
{
if(text == "异或校验")
{
ui->parity_in->setPlaceholderText("");
ui->binary->setDisabled(false);
}
else if(text == "奇校验")
{
ui->parity_in->setPlaceholderText("请输入二进制");
ui->binary->setCurrentText("BIN");
ui->binary->setDisabled(true);
}
else if(text == "偶校验")
{
ui->parity_in->setPlaceholderText("请输入二进制");
ui->binary->setCurrentText("BIN");
ui->binary->setDisabled(true);
}
ui->parity_in->clear();
ui->parity_out->clear();
});
//发送模式与显示模式的信号连接
connect(ui->hex,&QRadioButton::clicked,this,[=](){
ui->textEdit->setPlaceholderText("请输入16进制格式,并以空格分开.例如 01 3c");
});
connect(ui->text,&QRadioButton::clicked,this,[=](){
ui->textEdit->setPlaceholderText("");
});
connect(ui->rhex,&QRadioButton::clicked,this,[=](){
ui->rtext->setCheckState(Qt::CheckState(0));
ui->rflie->setCheckState(Qt::CheckState(0));
});
connect(ui->rtext,&QRadioButton::clicked,this,[=](){
ui->rhex->setCheckState(Qt::CheckState(0));
ui->rflie->setCheckState(Qt::CheckState(0));
});
connect(ui->rflie,&QRadioButton::clicked,this,[=](){
ui->rhex->setCheckState(Qt::CheckState(0));
ui->rtext->setCheckState(Qt::CheckState(0));
});
//重写窗口关闭,最小化,最大化,标题栏
connect(this,&MainWindow::hoverval,this,&MainWindow::hoveraction);
connect(this,&MainWindow::clickval,this,&MainWindow::clickaction);
//设置时间+定时发送
QLabel *lab2 = new QLabel(this);
timer1 = new QTimer(this);
timer2 = new QTimer(this);
timer1->start(1000);
connect(timer2,&QTimer::timeout,this,[=](){
if(ui->hex->isChecked())
shex();
else if(ui->text->isChecked())
stext();
});
connect(timer1, &QTimer::timeout, this, [=]()
{
QDateTime curTime = QDateTime::currentDateTime();
QString str = curTime.toString("当前时间: yyyy-MM-dd hh:mm:ss");
lab2->setText(str);
ui->statusbar->addWidget(lab2);
});
connect(ui->timesend,&QCheckBox::stateChanged,this,[=](int state){
{
if(state==2){
timer2->start(ui->timevalue->text().toInt());
ui->stoptimesend->setDisabled(false);
}
else if(state==0){
timer2->stop();
ui->stoptimesend->setDisabled(true);
}
}
});
//设置底部状态栏statusbar,超链接+发送次数
lab4 = new QLabel(this);
lab4->setPixmap(QPixmap(logo).scaled(61,31));
ui->statusbar->addWidget(lab4);//logo
QLabel *lab3 = new QLabel(this);
lab3->setText("发送次数: ");
ui->statusbar->addWidget(lab3);
edit = new QLineEdit(this);
edit->setStyleSheet("QLineEdit{border-color:#999999;border-radius: 7px; border-style: solid; border-width: 1px; padding: 1px;}");
edit->setMaximumWidth(30);
ui->statusbar->addWidget(edit);
QLabel *lab = new QLabel(this);
lab->setText(QStringLiteral("<a style='color: red; font: 10pt \"微软雅黑\";text-decoration: none ;font-size:15px' href =https://blog.csdn.net/m0_75192474?type=blog><u>CSDN博客</u>"));
lab->setOpenExternalLinks(true);
ui->statusbar->addPermanentWidget(lab);
QLabel *lab1 = new QLabel(thi
rqtz
- 粉丝: 753
- 资源: 8
最新资源
- 校园失物招领网站:设计与实现的全流程解析
- 基于java的公司固定资产管理系统.doc
- 基于java+springboot+vue+mysql的学科竞赛管理系统 源码+数据库+论文(高分毕业设计).zip
- 人工智能领域计算断层成像技术研究最新进展综述
- 微藻检测10-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 快速排序算法在Rust语言的实现及其优化
- 2024年超融合网络架构研究与实践报告.pdf
- 埃森哲:2024年360°价值报告(英文版).pdf
- ISACA中国社区2024女性职业现状调查报告.pdf
- 如何看待“适度宽松”的货币政策.pdf
- 双目立体匹配三维重建点云C++ 本工程基于网上开源代码进行修改,内容如下: 1.修改为 VS2015 Debug win32 版本,支持利用特征点和 OpenCV 立体匹配算法进行进行三维重建及显示
- 华为云AI数字人生态赋能千行百业高效发展.pdf
- 金融业数据安全发展与实践报告.pdf
- 候鸟生命线—共筑候鸟迁徙保护网络.pdf
- 2024年全国统一电力市场建设情况及展望报告.pdf
- 2018-2023年粤港澳、京津冀、长三角三大区域高校本科专业调整趋势.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈