#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtWidgets/QWidget>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QComboBox>
#include <QFileInfo>
#include <QFileDialog>
#include <QDebug>
#include <mthread.h>
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow();
private:
Mthread *Thread;
void send_filename();
void send_offset();
void setWindow();
void setflash();
QGridLayout *layout1;
QLineEdit *path_edit;
QPushButton *browser_button;
QLabel *flash_label;
QComboBox *flash_combox;
QLabel *offset_label;
QLineEdit *offset_edit;
QLabel *status_label;
QLabel *sdisplay_label;
QPushButton *program_button;
QString file_full;
QString file_name;
QString file_path;
QFileInfo fi;
bool ok;
private slots:
void setfile();
void STATUS_RECEIVE(const QString &s1, const QString &s2, const QString &s3);
void loading();
};
#endif // MAINWINDOW_H
#ifndef MTHREAD_H
#define MTHREAD_H
#include <QThread>
#include <QtSerialPort/QSerialPort>
#include <QWaitCondition>
#include <QDebug>
#include <QMutex>
#include <QWaitCondition>
#include <QFileInfo>
#include <QFileDialog>
class Mthread : public QThread
{
Q_OBJECT
public:
Mthread();
~Mthread();
void receive_filename(const QString &s);
void receive_offset(const QString &flashchip,const QString &offset);
void readfile();
void COLLOC();
unsigned long int crc32(char *ptr,unsigned int len);
QMutex mutex;
QWaitCondition cond;
QString FileName;
QString Offset;
QString Flashchip;
unsigned long int crc;
unsigned long int length;
unsigned long int rsv[2];
unsigned char data;
char *pc1;
char *pc;
bool ok;
unsigned long int offset1;
signals:
void STATUS_SEND(const QString &s1, const QString &s2, const QString &s3);
protected:
void run();
};
#endif // MTHREAD_H
#include <mainwindow.h>
#include <QtWidgets/QApplication>
int main(int argc,char *argv[])
{
QApplication a(argc,argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
MainWindow::MainWindow()
{
setWindow();
}
//設置窗體
void MainWindow::setWindow()
{
layout1 = new QGridLayout;
path_edit = new QLineEdit;
browser_button = new QPushButton("Browser");
flash_label = new QLabel("Flash:");
flash_combox = new QComboBox;
offset_label = new QLabel("Offset 0x");
offset_edit = new QLineEdit;
offset_edit->setMaxLength(6);
QRegExp regx("[a-fA-F0-9]+$");
QValidator *validator_offset = new QRegExpValidator(regx,offset_edit);
offset_edit->setValidator(validator_offset);
status_label = new QLabel("Status:");
sdisplay_label = new QLabel;
program_button = new QPushButton("Program");
layout1->addWidget(path_edit,0,0,1,7);
layout1->addWidget(browser_button,0,7,1,1);
layout1->addWidget(flash_label,1,0,1,1);
layout1->addWidget(flash_combox,1,1,1,3);
layout1->addWidget(offset_label,1,4,1,1);
layout1->addWidget(offset_edit,1,5,1,3);
layout1->addWidget(status_label,2,0,1,1);
layout1->addWidget(sdisplay_label,2,1,1,7);
layout1->addWidget(program_button,3,3,1,2);
setLayout(layout1);
setflash();
Thread = new Mthread;
Thread->start();
connect(browser_button,SIGNAL(clicked()),this,SLOT(setfile()));
connect(program_button,SIGNAL(clicked()),this,SLOT(loading()));
connect(Thread,SIGNAL(STATUS_SEND(QString,QString,QString)),this,SLOT(STATUS_RECEIVE(QString,QString,QString)));
}
//FLASH型號
void MainWindow::setflash()
{
flash_combox->addItem("MX29LV320EBTI");
}
//打開文件
void MainWindow::setfile()
{
file_full = QFileDialog::getOpenFileName(this);
fi = QFileInfo(file_full);
file_name = fi.fileName();
file_path = fi.absoluteFilePath();
path_edit->setText(file_full);
}
//發送文件名稱
void MainWindow::send_filename()
{
Thread->receive_filename(path_edit->text());
}
//發送偏移地址和FLASH型號
void MainWindow::send_offset()
{
Thread->receive_offset(this->flash_combox->currentText(),this->offset_edit->text());
}
//返回狀態
void MainWindow::STATUS_RECEIVE(const QString &s1, const QString &s2, const QString &s3)
{
sdisplay_label->setText(tr("偏移地址:%1;長度:%2;%3!").arg(s2).arg(s1).arg(s3));
}
//下載程序
void MainWindow::loading()
{
QString offset = offset_edit->text();
int offset_len = offset.toInt(&ok,16);
if(offset_len>=0&&offset_len<=4194303&&!offset.isEmpty()) //判斷偏移地址是否合法
{
if(!path_edit->text().isEmpty()) //判斷是否打開文件
{
send_filename();
send_offset();
}
else
{
sdisplay_label->setText("No file name specified");
}
}
else if(offset.isEmpty())
{
sdisplay_label->setText("請輸入偏移地址!");
}
else
{
sdisplay_label->setText("偏移地址不合法!");
}
}
#include "mthread.h"
Mthread::Mthread()
{
}
Mthread::~Mthread()
{
}
void Mthread::run()
{
readfile();
}
void Mthread::receive_filename(const QString &s)
{
QMutexLocker locker(&mutex);
this->FileName = s;
if (!isRunning())
start();
else
cond.wakeOne();
}
void Mthread::receive_offset(const QString &flashchip,const QString &offset)
{
QMutexLocker locker(&mutex);
this->Offset = offset;
this->Flashchip = flashchip;
if (!isRunning())
start();
else
cond.wakeOne();
}
void Mthread::COLLOC()
{
}
//判斷出在哪個secter
void Mthread::readfile()
{
int OFFSET2 = Offset.toInt(&ok,16);
offset1 = OFFSET2;
int OFFSET = OFFSET2+1;
int num = 1;
int num1;
int num9=9;
int OFFSET1=OFFSET;
if(OFFSET<=65536&&OFFSET>=0)
{
while(OFFSET1>8192)
{
OFFSET1=OFFSET1-8192;
num++;
}
num1=num;
}
else
{
while(OFFSET1>131072)
{
OFFSET1=OFFSET1-65536;
num++;
}
num=num+8;
num1=num;
}
QFile file(FileName);
QSerialPort serial;
serial.setPortName("/dev/ttyS1");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);
if (file.open(QFile::ReadOnly))
{
while (!file.atEnd())
{
if(num<72)
{
while(num1==num&&num<=8)
{
length = 2*(num*8192-OFFSET+1);
qDebug()<<"num1==num&&num<=8"<<length;
break;
}
while(num1==num&&num>8)
{
length = 2*((num-7)*65536-OFFSET+1);
qDebug()<<"num1==num&&num>8"<<length;
break;
}
while(num1!=num&&num<=8)
{
length = 16384;
qDebug()<<"num1!=num&&num<=8"<<length;
break;
}
while(num1!=num&&num>=num9&&num<=71)
{
length = 131072;
qDebug()<<"num1!=num&&num>=num9"<<length;
break;
}
qint64 line_len;
pc1 = (char*)malloc(length);
line_len = file.read(pc1,(qint64)length);
qDebug()<<line_len<<length;
length = line_len;
pc = (char*)malloc(length+16);
for(unsigned long int i=0;i<length;i++)
{
pc[16+i]=pc1[i];
}
free(pc1);
for(int i=7;i>3;i--)
{ //char s[32];
pc[i]=(char)(length>>(8*(7-i)));
//sprintf(s,"%d",*(pc+i));
//qDebug()<<s;
}
for(int i=0;i<8;i++)
{
pc[8+i]=0;
}
crc = crc32(pc+4,length+12);
for(int i=0;i<4;i++)
{
pc[3-i]=(crc>>(8*i));
//char s[32];
//sprintf(s,"%d",crc);
//qDebug()<<s;
}
unsigned long int len =length+16;
QString sendlen =QString::number(len,10);
QString offset2 = QString::number(offset1,16);
QString loading="正在下載";
QString load_seccess ="下載成功!";
QString load_false2="下載失敗!";
//寫數據
char *pc2;
pc2 = pc;
QByteArray response2;
for(unsigned long int i=0;i<(length+16);i++)
{
QByteArray response3 = QByteArray(pc2+i,(qint64)1);
response2+=response3;
}
serial.write(response2);
while(serial.waitForBytesWritten(500))
{
emit this->STATUS_SEND(sendlen,offset2,loading);
}
free(pc);
num++;
offset1 +=(length/2);
//反饋信息
int load_num = 0;
if(serial.waitForReadyRead(5000))
{
QByteArray response1 = serial.readAll();
QString feedback(response1);
if(feedback.startsWith("aa"))
{
emit STATUS_SEND(sendlen,offset2,load_seccess);
for(int i=0;i<5000;i++);
}
else
{
serial.write(response2);
while(serial.waitForBytesWritten(500))
{
emit this->STATUS_SEND(sendlen,offset2,loading);
}
while(serial.waitForReadyRead(5000)&&load_num<3)
{
QByteArray response1 = serial.readAll();
QString feedback(response1);
if(feedback.startsWith("aa"))
{
emit STATUS_SEND(sendlen,offset2,load_seccess);
for(int i=0;i<5000;i++);
break;
}
else if(load_num==2)
{
emit STATUS_SEND(sendlen,offset2,load_false2);
for(int i=0;i<500;i++);
file.close();
}
else
{
serial.write(response2);
while(serial.waitForBytesWritten(500))
{
emit this->STATUS_SEND(sendlen,offset2,loading);
}
load_num++;
}
}
}
}
else
{
emit STATUS_SEND(sendlen,offset2,load_false2);
for(int i=0;i<5000;i++);
file.close();
}
response2.clear();
}
else
{
QString CROSS = "越界!";
emit this->STATUS_SEND(CROSS,CROSS,CROSS);
file.close();
break;
}
}
}
}
unsigned long int Mthread::crc32(char *ptr,unsigned int len)
{
unsigned long int crc = 0;
unsigned long int CHECK_CODE = 0xedb88320;
unsigned long int i;
while(len--)
{
crc ^= *ptr++;
for(i = 0;i < 8;i++)
{
if(crc & 0x1)
{
crc = (crc>>1) ^ CHECK_CODE;
}
else
{
crc >>= 1;
}
}
}
return crc;
}