1 Star 0 Fork 0

nongxiaoming/UpdateTool_wifi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mainwindow.cpp 6.74 KB
一键复制 编辑 原始数据 按行查看 历史
nongxiaoming 提交于 2014-07-19 19:38 . 更新配置
#include "mainwindow.h"
#include "ui_mainwindow.h"
#define MAX_DATA_SIZE 1024
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->PageInit ();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::PageInit ()
{
this->step=0;
this->finsh_size=0;
this->HexEdit=new QHexEdit(ui->groupBox_2);
this->HexEdit->setReadOnly(true);
ui->gridLayout_2->addWidget (this->HexEdit);
this->setFixedSize( this->width (),this->height ());
this->udpSocket = new QUdpSocket(this);
QObject::connect (ui->open_action,SIGNAL(triggered()),this,SLOT(on_LoadBtn_clicked()));
QObject::connect (ui->update_action,SIGNAL(triggered()),this,SLOT(on_UpdateBtn_clicked()));
QObject::connect(this->udpSocket,SIGNAL(readyRead()),this,SLOT(data_Receive()));
}
void MainWindow::data_Receive ()
{
QByteArray datagram;//用于存放接收的数据报
datagram= this->TcpSocket->readAll ();
if(datagram[0]==(char)ACK)
{
switch (this->step) {
case 0:
this->step++; //发送文件头完成
this->update_log_output ("发送固件信息完成,开始传输数据...");
this->send_data (); //开始发送数据
break;
case 1:
this->finsh_size+=MAX_DATA_SIZE;
if(this->finsh_size>=this->total_size) //如果发送完则发送完成数据包,否则继续发送下一个数据包
{
this->step++;
}
this->send_data ();
this->update_progressbar ();
break;
case 2: //发送完成命令得到回应,结束发送
this->update_log_output ("固件传输完成,设备刷新固件后会自动启动。");
this->step=0;
this->TcpSocket->close ();
this->finsh_size=0;
break;
default: //发送过程出现异常
this->step=0;
this->TcpSocket->close ();
this->finsh_size=0;
break;
}
}else if(datagram[0]==(char)NAK) //未收到回应
{
this->update_log_output ("固件传输失败,设备未响应。");
this->step=0;
this->TcpSocket->close ();
this->finsh_size=0;
}
}
void MainWindow::on_UpdateBtn_clicked()
{
update_log_output(tr("开始升级..."));
this->step=0;
this->finsh_size=0;
this->IPAddr=new QHostAddress(ui->lineEdit->text ().trimmed ());
this->hostPort=ui->spinBox->value ();
this->TcpSocket->connectToHost (*IPAddr,hostPort);
this->send_data ();
}
void MainWindow::send_data()
{
QByteArray data;
quint16 pack_size=0;
switch(this->step)
{
case 0:
this->pack_num=0;
pack_size=7;
data.append((quint8)SOH);
data.append((char)0x00);
data.append((char)0xff);
data.append((char)((total_size>>24)&0xffUL));
data.append((char)((total_size>>16)&0xffUL));
data.append((char)((total_size>>8)&0xffUL));
data.append((char)((total_size)&0xffUL));
qDebug()<<tr("total size:%1").arg (QString::number (this->total_size));
// qDebug()<<tr("hex:%1").arg (this->total_size,8,16,'0');
this->TcpSocket->write(data,pack_size);
break;
case 1:
this->pack_num++;
pack_size=MAX_DATA_SIZE+3;
data.append((quint8)STX);
data.append(pack_num);
data.append((quint8)(~pack_num));
if(this->total_size-this->finsh_size<MAX_DATA_SIZE)
{
QByteArray fill_data;
fill_data.resize(MAX_DATA_SIZE-(this->total_size-this->finsh_size));
fill_data.fill(0x00);
data.append(&m_buf[this->finsh_size],this->total_size-this->finsh_size);
data.append(fill_data);
}else
{
data.append(&m_buf[this->finsh_size],MAX_DATA_SIZE);
}
this->TcpSocket->write(data,pack_size);
break;
case 2:
this->pack_num++;
pack_size=3;
data.append((quint8)EOT);
data.append(pack_num);
data.append((quint8)(~pack_num));
this->TcpSocket->write(data,pack_size);
break;
default:
this->step=0;
break;
}
}
void MainWindow::on_LoadBtn_clicked()
{
QFileDialog *fileDialog = new QFileDialog(ui->centralWidget);//创建一个QFileDialog对象,构造函数中的参数可以有所添加。
fileDialog->setWindowTitle(tr("打开固件"));//设置文件保存对话框的标题
fileDialog->setAcceptMode(QFileDialog::AcceptOpen);//设置文件对话框为保存模式
fileDialog->setFileMode(QFileDialog::AnyFile);//设置文件对话框弹出的时候显示任何文件,不论是文件夹还是文件
fileDialog->setViewMode(QFileDialog::Detail);//文件以详细的形式显示,显示文件名,大小,创建日期等信息;
//还有另一种形式QFileDialog::List,这个只是把文件的文件名以列表的形式显示出来
fileDialog->setGeometry(10, 30, 300, 200); //设置文件对话框的显示位置
fileDialog->setDirectory(".");//设置文件对话框打开时初始打开的位置
fileDialog->setNameFilter("固件文件(*.bin)");//设置文件类型过滤器
if (fileDialog->exec() == QDialog::Accepted)
{
//注意使用的是QFileDialog::Accepted或者QDialog::Accepted,不是QFileDialog::Accept
QString path = fileDialog->selectedFiles()[0];
QFileInfo fileinfo(path);
QFile file(path);
file.open(QIODevice::ReadOnly);
this->total_size = file.size();
QDataStream in(&file);
m_buf = new char[this->total_size];
in.readRawData(m_buf, this->total_size);
file.close();
QByteArray FWdat = QByteArray(m_buf, this->total_size);
this->HexEdit->setData (FWdat);
ui->fwNamelabel->setText (tr("固件名称:%1").arg (fileinfo.fileName ()));
ui->fwSizelabel->setText (tr("固件大小:%1字节").arg (QString::number (this->total_size)));
ui->fwDate_label->setText (tr("日期:%1").arg (fileinfo.created ().toString ("yyyy-M-d H:m")));
this->update_log_output ("读入固件信息成功,请点击\"升级\"进行升级...");
}
}
void MainWindow::update_progressbar()
{
if(this->finsh_size>this->total_size)
{
this->finsh_size=this->total_size;
ui->progressBar->setValue (100);
}else{
int i=0;
i= (this->finsh_size*100)/this->total_size;
ui->progressBar->setValue (i);
}
}
void MainWindow::update_log_output(QString info)
{
QString time= QDateTime::currentDateTime ().toString ("yyyy-MM-dd hh:mm:ss");
ui->textEdit->append (tr("[%1] %2").arg (time).arg (info));
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/nongxiaoming/UpdateTool_wifi.git
git@gitee.com:nongxiaoming/UpdateTool_wifi.git
nongxiaoming
UpdateTool_wifi
UpdateTool_wifi
master

搜索帮助