# STM32串口烧录BIN文件、字库文件【QT上位机】
**项目已开源至GitHub,供大家一起学习使用 [*点我传送*](https://github.com/firestaradmin/Uart_Transfer_BIN_to_exFlash)**
## 一.上位机部分
- 使用QT-C++ 编写的上位机 预览图如下
![在这里插入图片描述](uart_xfer_BIN/20200722123313745.png)
- 程序思路:
使用串口通讯进行发送规定的帧数据,上位机会将BIN文件数据分析打包成每一帧传送给下位机。
下位机,接受到数据后,进行ACK应答,程序接受到应答后,继续发送数据,直至完成发送。
### 1.帧协议格式:
![在这里插入图片描述](uart_xfer_BIN/20200722123934499.png)
![在这里插入图片描述](uart_xfer_BIN/20200722123946578.png)
如发送数据:**C5 5C 00 00 04 22 33 44 FF AE 5C C5**
BCC = 除了帧头帧尾和bcc本身的异或
BCC = 00 ^ 00 ^ 04 ^ 22 ^ 33 ^ 44 ^ FF = AE
### 2.QT部分程序
```cpp
//将串口的 readyread 信号绑定到 read_com 这个槽函数上
connect(&mySerial,SIGNAL(readyRead()),this,SLOT(read_COM()));
```
此处读取到下位机的ACK应答后,触发自定义信号[readOK],再进行下一步发送
```cpp
void MainWindow::read_COM()
{
QByteArray mytemp = mySerial.readAll();
if(!mytemp.isEmpty())
{
//qDebug() << mytemp.toHex();
if((mytemp.at(0) & 0xff) != 0xC5)
return;
if((mytemp.at(1) & 0xff) != 0x5C)
return;
if((mytemp.at(5) & 0xff) == 0x00){
ui->textEdit_Log->append("send Data OK!");
sendedBytes += currentLen;
unSendedBytes = file_length - sendedBytes;
if(unSendedBytes < 0)
unSendedBytes = 0;
ui->label_sendedBytes->setNum(sendedBytes);
ui->label_unSendedBytes->setNum(unSendedBytes);
ui->progressBar->setValue(static_cast<int>((static_cast<float>(sendedBytes) / static_cast<float>(file_length)) * 100));
if(unSendedBytes > 0){
emit(recvOK());
}else {
ui->textEdit_Log->append("all data send over!");
ui->pushButton_StartSending->setEnabled(true);
}
}
else {
ui->pushButton_StartSending->setEnabled(true);
ui->textEdit_Log->append("send Data failed!");
ui->textEdit_Log->append(QString("ERROR:%1").arg(mytemp.at(5) & 0xff));
}
//mytemp.clear();
}
}
```
```cpp
//将自定义的信号和发送函数连接
connect(this, &MainWindow::recvOK, this, &MainWindow::sendOnce);
```
```cpp
void MainWindow::sendOnce(){
QByteArray sendBuf, dataBuf;
char file_length0,file_length1;
QString str;
char bcc = 0x00;
if(unSendedBytes >= ui->lineEdit_ByteNum->text().toInt()){
dataBuf = file_array.mid(sendedBytes, ui->lineEdit_ByteNum->text().toInt());
currentLen = ui->lineEdit_ByteNum->text().toInt();
}
else {
dataBuf = file_array.mid(sendedBytes, unSendedBytes);
currentLen = unSendedBytes;
}
//qDebug() << dataBuf.size();
sendBuf.clear();
sendBuf.append("\xC5\x5C");
sendBuf.append(static_cast<char>(0));
file_length0 = static_cast<char>(currentLen / 0xFF);
file_length1 = static_cast<char>(currentLen % 0xFF);
sendBuf.append(file_length0);
sendBuf.append(file_length1);
//qDebug() << sendBuf.toHex().toUpper();
sendBuf.append(dataBuf);
//qDebug() << sendBuf.toHex().toUpper();
for(int i = 2; i < sendBuf.size(); i++){
//str.append((QString("%1 ").arg(file_array.at(i) & 0xff, 2, 16, QLatin1Char('0')).toUpper()));
bcc ^= sendBuf.at(i) & 0xff;
}
sendBuf.append(bcc);
sendBuf.append("\x5C\xC5");
//qDebug() << sendBuf.toHex().toUpper();
for(int i = 5; i < sendBuf.size() - 3; i++){
str.append((QString("%1 ").arg(sendBuf.at(i) & 0xff, 2, 16, QLatin1Char('0')).toUpper()));
}
ui->textEdit_Status->append("---------------------------------------");
ui->textEdit_Status->append(QString("地址[ %1 ] to [ %2 ] 的数据:").arg(sendedBytes).arg(sendedBytes + currentLen - 1));
ui->textEdit_Status->append(str);
ui->textEdit_Status->append(QString("--- BCC校验码: %1 ---").arg(bcc & 0xff, 2, 16, QLatin1Char('0')).toUpper());
ui->textEdit_Log->append(QString("send [ %1 ] to [ %2 ] Data ...").arg(sendedBytes).arg(sendedBytes + currentLen - 1));
mySerial.write(sendBuf);
}
```
## 二.下位机部分
### 1.接收与处理
上位机将BIN文件分成若干可设置数据长度的帧数据,以帧的形式串口发送给下位机,下位机接收帧,检验帧头帧尾和BCC校验码,如果正确无误,将数据烧写至外部储存器,烧写完成后,发送应答给上位机。上位机发送一帧数据后,会等待下位机的应答,接收到应答后,再继续发送下一帧数据,直至全部发送完成。
### 2.ACK应答帧格式
![在这里插入图片描述](uart_xfer_BIN/20200722124646551.png)
### 3.下位机通用C++代码(Lib)
```cpp
//"transfer_BIN_to_EX_FLASH.h"
#ifndef __transfer_BIN_to_EX_FLASH
#define __transfer_BIN_to_EX_FLASH
extern u8 TBEF_uart_recv_finish , TBEF_uart_recving_flag , rTBEF_uart_recv_tim_cnt;
//extern u8 TBEF_recvBuf[400];
//extern u16 TBEF_recvBuf_tail;
void TBEF_uart_receive_process(u8 data);
void TBEF_tim_process(void);
void TBEF_SendACK(u8 ERROR);
void TBEF_clearRecvBuf();
u8 TBEF_framePrasing();
void TBEF_mainFun();
void TBEF_data_CallBack(u8 *dataBuf, u16 len);
#endif
```
```cpp
/************************************************************************************
* Copyright (c), 2020, LXG.
*
* FileName :
* Author :firestaradmin
* Version :1.0
* Date :2020.7.21
* Description :串口接收BIN文件数据烧录至外部储存器
* History :
*
*
*************************************************************************************
帧协议格式:
Byte0 Byte1 Byte2 Byte3 Byte4 Byte5 … last but two Byte last but one Byte Last Byte
0xC5 0x5C XX XX XX XX XX XX 0x5C 0xC5
帧头 帧头 命令 长度高字节 长度低字节 数据 数据 BCC校验码 帧尾 帧尾
如发送数据:C5 5C 00 00 04 22 33 44 FF AE 5C C5
BCC = 除了帧头帧尾和bcc本身的异或
BCC = 00 ^ 00 ^ 04 ^ 22 ^ 33 ^ 44 ^ FF = AE
命令: 0x00 0x01 0x02 0xFF
备注: 数据 数据传输开始 数据传输结束 应答
*************************************************************************************/
#include "stm32f10x.h"
#include "../bsp/transfer_BIN_to_EX_FLASH/transfer_BIN_to_EX_FLASH.h"
#include "../bsp/w25qxx/w25qxx.h"
#include "../BSP/usart/usart.h"
//W25Q64
//容量为8M字节,共有128个Block,2048个Sector
//4Kbytes为一个Sector
//16个扇区为1个Block
//以下表示地址为W25QXX的第一个区块的第0个扇区的第0个地址
#define W25QXX_STORAGE_Block 1
#define W25QXX_STORAGE_Sector 0
#define W25QXX_STORAGE_Sector_OFFSET 0
u32 TBEF_W25QXX_StorageAddress = W25QXX_STORAGE_Block * 4 * 1024 * 16 + 4 * 1024 * W25QXX_STORAGE_Sector + W25QXX_STORAGE_Sector_OFFSET; //要烧录的具体地址
u32 bytesStored = 0; //已经储存的字节
u8 TBEF_recvBuf[1000]; //接受buf,最大单次传输字节数应不大于buf大小-8
u16 TBEF_recvBuf_tail = 0;
u8 TBEF_uart_recv_finish = 0, TBEF_uart_recving_flag = 0, TBEF_uart_recv_tim_cnt = 0;
//用户回调函数,此处修改需要烧录的函数
void TBEF_data_CallBack(u8 *dataBuf, u16 len)
{
//根据需求修改此处
W25QXX_Write(dataBuf, TBEF_W25QXX_StorageAddress + bytesStored, len);//写入flash
bytesStored += len;
}
//串口处理函数,在串口中断中调用,将接收到的字节传入data
void TBEF_uart_receive_process(u8 data)
{
if(TBEF_uart_recv_finish == 0){
TBEF_recvBuf[TBEF_recvBuf_tail++] = data; // 存入缓存数组
TBEF_uart_recving_flag = 1; // 串口 接收标志
TBEF_uart_recv_tim_cnt = 0;
没有合适的资源?快使用搜索试试~ 我知道了~
用于STM32串口烧录BIN文件、字库文件的QT上位机.zip(本人专注嵌入式领域)
共221个文件
h:40个
d:35个
o:35个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 5 浏览量
2024-03-29
20:27:52
上传
评论
收藏 32.32MB ZIP 举报
温馨提示
嵌入式优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人单片机开发经验充足,深耕嵌入式领域,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明,项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要嵌入式物联网单片机相关领域开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注嵌入式领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【建议小白】: 在所有嵌入式开发中硬件部分若不会画PCB/电路,可选择根据引脚定义将其代替为面包板+杜邦线+外设模块的方式,只需轻松简单连线,下载源码烧录进去便可轻松复刻出一样的项目 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能
资源推荐
资源详情
资源评论
收起资源包目录
用于STM32串口烧录BIN文件、字库文件的QT上位机.zip(本人专注嵌入式领域) (221个子文件)
NestlingV2.0_CPP 354KB
NestlingV2.1_F103_UART.uvguix.79183 178KB
uart_transfer_BIN_to_EX-Flash.pro.user.a68afde 24KB
GBK2312等线24_水平扫描高位在前.bin 621KB
GB2312等线24_垂直扫描_高位在前.bin 621KB
GB2312等线16_垂直扫描_高位在前.bin 276KB
stm32f10x_tim.c 104KB
stm32f10x_flash.c 59KB
stm32f10x_rcc.c 49KB
stm32f10x_adc.c 45KB
stm32f10x_i2c.c 43KB
stm32f10x_can.c 43KB
stm32f10x_usart.c 36KB
system_stm32f10x.c 35KB
stm32f10x_fsmc.c 34KB
stm32f10x_spi.c 29KB
stm32f10x_dma.c 28KB
stm32f10x_sdio.c 27KB
stm32f10x_gpio.c 22KB
stm32f10x_dac.c 18KB
core_cm3.c 16KB
stm32f10x_cec.c 11KB
sram.c 10KB
w25qxx.c 9KB
stm32f10x_pwr.c 8KB
stm32f10x_rtc.c 8KB
stm32f10x_bkp.c 8KB
misc.c 7KB
stm32f10x_exti.c 7KB
stm32f10x_wwdg.c 5KB
spi.c 5KB
transfer_BIN_to_EX_FLASH.c 5KB
stm32f10x_dbgmcu.c 5KB
stm32f10x_iwdg.c 5KB
transfer_BIN_to_EX_FLASH.c 4KB
stm32f10x_crc.c 3KB
stm32f10x_it.c 2KB
myTIM.c 2KB
delay.c 2KB
LED.c 670B
usart.cpp 11KB
mainwindow.cpp 9KB
main.cpp 1KB
mythread.cpp 285B
main.cpp 176B
stm32f10x_tim.crf 299KB
stm32f10x_can.crf 295KB
stm32f10x_adc.crf 293KB
stm32f10x_flash.crf 291KB
stm32f10x_fsmc.crf 291KB
stm32f10x_sdio.crf 289KB
usart.crf 287KB
stm32f10x_dma.crf 287KB
main.crf 286KB
stm32f10x_rcc.crf 284KB
stm32f10x_i2c.crf 283KB
stm32f10x_usart.crf 283KB
stm32f10x_dac.crf 283KB
w25qxx.crf 283KB
stm32f10x_spi.crf 281KB
stm32f10x_cec.crf 281KB
stm32f10x_gpio.crf 281KB
stm32f10x_bkp.crf 281KB
transfer_bin_to_ex_flash.crf 281KB
stm32f10x_pwr.crf 280KB
stm32f10x_rtc.crf 280KB
system_stm32f10x.crf 279KB
spi.crf 279KB
stm32f10x_wwdg.crf 278KB
stm32f10x_iwdg.crf 278KB
stm32f10x_dbgmcu.crf 278KB
mytim.crf 278KB
stm32f10x_exti.crf 278KB
misc.crf 278KB
stm32f10x_crc.crf 278KB
delay.crf 277KB
led.crf 277KB
stm32f10x_it.crf 277KB
core_cm3.crf 2KB
transfer_bin_to_ex_flash.d 2KB
main.d 1KB
w25qxx.d 1KB
stm32f10x_dbgmcu.d 1KB
usart.d 1KB
stm32f10x_flash.d 1KB
stm32f10x_usart.d 1KB
stm32f10x_fsmc.d 1KB
stm32f10x_iwdg.d 1KB
stm32f10x_sdio.d 1KB
stm32f10x_gpio.d 1KB
stm32f10x_exti.d 1KB
stm32f10x_wwdg.d 1KB
stm32f10x_rcc.d 1KB
stm32f10x_dac.d 1KB
stm32f10x_crc.d 1KB
stm32f10x_adc.d 1KB
stm32f10x_pwr.d 1KB
stm32f10x_can.d 1KB
stm32f10x_rtc.d 1KB
stm32f10x_i2c.d 1KB
共 221 条
- 1
- 2
- 3
资源评论
阿齐Archie
- 粉丝: 1w+
- 资源: 2301
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功