#include "mainwindow.h"
#include "ui_mainwindow.h"
#define yr_recbuf_delay_time (10)
#define yr_recieve_delay_time (200)
#define yr_send_size (128)
unsigned char ID_CHECK_NUM[] = {0xAA,0x05,0x01,0xB5,0x00,0x00,0x65,0xEB,0xAA,0x0D};
unsigned char ID_FPGA_NUM[] = {0x55,0x04,0xB5,0x33,0x01,0x42,0xEB,0xAA};
//char ID_CHECK_NUM[] = {0x01,0x02,0x03,0x0d,0x24,0x56};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//user
system_init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::system_init()
{
//port config
global_port.setParity(QSerialPort::NoParity);
global_port.setDataBits(QSerialPort::Data8);
global_port.setStopBits(QSerialPort::OneStop);
connect(&global_port,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
//combox
ui->comboBox->clear();
QStringList list;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
list << info.portName();
qSort(list.begin(),list.end());
ui->comboBox->addItems(list);
//timer
m_pTimer = new QTimer();
m2_pTimer = new QTimer();
connect(m_pTimer, SIGNAL(timeout()), this, SLOT(onTimeout()));
connect(m2_pTimer, SIGNAL(timeout()), this, SLOT(onTime2out()));
//Firmware_size
Firmware_size = 0;
Firmware_send_size = 0;
}
//================================================================================================
void MainWindow::on_pushButton_2_clicked() //串口刷新按键
{
ui->comboBox->clear();
QStringList list;
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
list << info.portName();
qSort(list.begin(),list.end());
ui->comboBox->addItems(list);
ui->label_3->setText("串口搜索完成");
}
void MainWindow::on_pushButton_clicked() //串口开关
{
if(global_port.isOpen())//如果串口已经打开了 先给他关闭了
{
global_port.clear();
global_port.close();
ui->pushButton->setText("打开");
ui->label_3->setText("关闭成功!");
ui->comboBox->setEnabled(true);
ui->pushButton_2->setEnabled(true);
}else{
global_port.setPortName(ui->comboBox->currentText());
if(!global_port.open(QIODevice::ReadWrite)){//用ReadWrite 的模式尝试打开串口
ui->label_3->setText("打开失败!");
return;
}
global_port.setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);//设置波特率和读写方向
global_port.setDataBits(QSerialPort::Data8); //数据位为8位
global_port.setFlowControl(QSerialPort::NoFlowControl);//无流控制
global_port.setParity(QSerialPort::NoParity); //无校验位
global_port.setStopBits(QSerialPort::OneStop); //一位停止位
ui->pushButton->setText("关闭");
ui->label_3->setText("打开成功!");
ui->comboBox->setEnabled(false);
ui->pushButton_2->setEnabled(false);
}
}
//================================================================================================
void MainWindow::on_toolButton_clicked() //打开文件按键
{
QString Firmware_fileName = QFileDialog::getOpenFileName(this, tr("打开"), " ", tr("Allfile(*.*);;mp3file(*.mp3)"));
ui->lineEdit->setText(Firmware_fileName);
QFile file(ui->lineEdit->text());
if(file.exists()){
QFileInfo info(ui->lineEdit->text());
ui->label_3->setText("添加了"+info.fileName()+"文件 大小:"+ QString::number(Firmware_size/1024)+"k");
}
}
void MainWindow::on_pushButton_3_clicked() //下载按键
{
QFile file(ui->lineEdit->text());
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(nullptr, "提示", "请输入正确的文件路径! ");
return;
}
QFileInfo info(ui->lineEdit->text());
Firmware_size = ((info.size()/128)+1)*128;
ui->progressBar->setMaximum(Firmware_size);
//==========crc check
QDataStream BinFileData(&file);
char *pBuff = new char[Firmware_size];
memset(pBuff,0,Firmware_size);
BinFileData.readRawData(pBuff,static_cast<int>(info.size()));
Firmware_crc = crc_wu(pBuff,Firmware_size,0);
ui->plainTextEdit->clear();
ui->plainTextEdit->appendPlainText(QString::number(Firmware_size,16));
ui->plainTextEdit->appendPlainText(QString::number(Firmware_crc,16));
//=====
if(global_port.isOpen()){
QByteArray data((char *)ID_CHECK_NUM,sizeof(ID_CHECK_NUM));
QByteArray data_Firmware_size = intToByte(Firmware_size);
QByteArray data_crc = intToByte(Firmware_crc);
data.append(data_Firmware_size);
data.append(data_crc);
qDebug() << data.toHex();
global_port.write(data);
Firmware_send_size = 0;
ui->pushButton_3->setEnabled(false);
ui->pushButton_3->setText("下载中");
m2_pTimer->start(yr_recieve_delay_time);
}else{
QMessageBox::critical(nullptr, "提示", "请先打开串口! ");
}
file.close();
}
//==========================================================================================================发送控制部分
void MainWindow::onTime2out(){ //发送定时
ui->progressBar->setValue(0);
ui->label_3->setText("文件下载出错!");
Firmware_send_size = 0;
ui->pushButton_3->setEnabled(true);
ui->pushButton_3->setText("重新下载");
m2_pTimer->stop();
}
int MainWindow::send_file(){ //发送函数
QFile file(ui->lineEdit->text());
char buffer[yr_send_size] = {0};
if (!file.open(QIODevice::ReadOnly)) {
return -1;
}
//QFileInfo read_file_info(file);
QDataStream in(&file);
in.skipRawData(Firmware_send_size);
in.setByteOrder(QDataStream::BigEndian);
in.readRawData(buffer,yr_send_size);
QByteArray DataAllArray;
DataAllArray = QByteArray(buffer,yr_send_size);
global_port.write(DataAllArray,yr_send_size);
file.close();
m2_pTimer->stop();
m2_pTimer->start(yr_recieve_delay_time);
return 1;
}
//==========================================================================================================接收控制部分
void MainWindow::onTimeout(){ //接收定时
QByteArray hexData = readbyte.toHex();
if(readbyte.size() == 1) return;
qDebug() << readbyte.size();
ui->plainTextEdit->appendPlainText("------------------------");
if(ui->radioButton->isChecked())
ui->plainTextEdit->appendPlainText(hexData);
else
ui->plainTextEdit->appendPlainText(readbyte);
//================检测fpga透传ack,检测开始下载
if(Firmware_send_size == 0){
if(readbyte.contains((char *)ID_FPGA_NUM)){
QByteArray data((char *)ID_CHECK_NUM,sizeof(ID_CHECK_NUM));
QByteArray data_Firmware_size = intToByte(Firmware_size);
QByteArray data_crc = intToByte(Firmware_crc);
data.append(data_Firmware_size);
data.append(data_crc);
qDebug() << data.toHex();
global_port.write(data);
m2_pTimer->stop();
m2_pTimer->start(yr_recieve_delay_time);
readbyte.clear();
m_pTimer->stop();
return;
}else if(readbyte.contains((char *)ID_CHECK_NUM)){
ui->label_3->setText("建立连接,正在下载...");
}else{
readbyte.clear();
m_pT
评论0