日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT 编写 STC系列MCU烧录软件

發(fā)布時(shí)間:2024/8/1 c/c++ 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT 编写 STC系列MCU烧录软件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

由于產(chǎn)品上用到了STC的單片機(jī),而需要自行寫一個(gè)燒錄軟件,該軟件可以實(shí)現(xiàn)STC系列MCU文件的燒錄。

本軟件最終取自開源的庫,因此感謝為開源無私奉獻(xiàn)的人們!

一、將開源的STC的庫,編譯生成exe文件

本示例采用的github上的開源庫:https://github.com/grigorig/stcgal

由于該demo采用的是python庫,用C++不是很容易調(diào)用,因此將其編譯為exe可執(zhí)行文件使用。

具體方法如下:

1、首先將源碼下載或者clone下來。

2、安裝pyinstaller

? ? ? ???pip install pyinstaller

? ? ? ? ?輸入$ pyinstaller -v顯示版本信息表示安裝成功,顯示結(jié)果如下:
? ? ? ? ? ? ? ?3.6

3、進(jìn)入stcgal文件夾,輸入以下命令:

pyinstaller -F __main__.py frontend.py ihex.py models.py options.py protocols.py utils.py

? ? ? 會在當(dāng)前文件夾的dist子文件夾下面生成__main__.exe

? ? ? 然后執(zhí)行? ?__main__.exe? ,會生成如下的幫助命令,表示編譯成功,文件可用

usage: __main__.exe [-h] [-a] [-r RESETCMD] [-P {stc89,stc12a,stc12b,stc12,stc15a,stc15,stc8,usb15,auto}] [-p PORT][-b BAUD] [-l HANDSHAKE] [-o OPTION] [-t TRIM] [-D] [-V][code_image] [eeprom_image]stcgal 1.6 - an STC MCU ISP flash tool (C) 2014-2018 Grigori Goronzy and others https://github.com/grigorig/stcgalpositional arguments:code_image code segment file to flash (BIN/HEX)eeprom_image eeprom segment file to flash (BIN/HEX)optional arguments:-h, --help show this help message and exit-a, --autoreset cycle power automatically by asserting DTR-r RESETCMD, --resetcmd RESETCMDshell command for board power-cycling (instead of DTR assertion)-P {stc89,stc12a,stc12b,stc12,stc15a,stc15,stc8,usb15,auto}, --protocol {stc89,stc12a,stc12b,stc12,stc15a,stc15,stc8,usb15,auto}protocol version (default: auto)-p PORT, --port PORT serial port device-b BAUD, --baud BAUD transfer baud rate (default: 19200)-l HANDSHAKE, --handshake HANDSHAKEhandshake baud rate (default: 2400)-o OPTION, --option OPTIONset option (can be used multiple times, see documentation)-t TRIM, --trim TRIM RC oscillator frequency in kHz (STC15+ series only)-D, --debug enable debug output-V, --version print version info and exit

二、通過命令行測試編譯是否成功
?

./__main__.exe -P stc15 -p COM5 -l 1200 -o clock_source=internal -t 33000 test.hex

具體命令解釋如下,-P為選擇的型號,-p為選擇的串口號 ,-l為波特率? -o為選擇時(shí)鐘源,-t為時(shí)鐘頻率? ? ?最后test.exe表示燒錄的文件的路徑。

如有更具體的參數(shù),請參考https://github.com/grigorig/stcgal/blob/master/doc/USAGE.md文件,上面對命令的解釋更加詳細(xì)。

三、編寫QT軟件

可以調(diào)用qprocess,當(dāng)外部文件命令行調(diào)用該文件,具體代碼如下:

頭文件:

#ifndef STC_ISP_CONFIG_H #define STC_ISP_CONFIG_H#include <QObject> #include <QProcess> #include <log_generate.h>//下載參數(shù) typedef struct {QString ComName; //串口號uint32_t BaudRate; //波特率QString FirmwarePath; //燒錄文件路徑uint32_t CrystalFreq; //晶振 }_STC_ISP_DOWNLOAD_PAR;//燒錄是否成功 typedef enum {ISP_DOWNLOADING = 0,ISP_DOWNLOAD_FAIL,ISP_DOWNLOAD_SUCC, }_ENUM_STC_ISP_DOWNLOAD;extern _ENUM_STC_ISP_DOWNLOAD isp_download_state; //程序下載情況class STC_ISP_Config : public QObject {Q_OBJECT public:explicit STC_ISP_Config(QObject *parent = nullptr);signals:void Signal_STC_ISP_Log(int msgType,QString loginfo);public slots:void Slot_Command_Info_Read(); //讀取 cmd 執(zhí)行完命令void Slot_Command_Error_Read(); //讀取 錯誤輸出信號void Slot_Command_Finished(int exitCode);bool ISP_Write_Program(_STC_ISP_DOWNLOAD_PAR); //寫入程序private:QProcess *m_Process; //用于操作命令行工具 };#endif // STC_ISP_CONFIG_H

源文件:

#include "stc_isp_config.h" #include <QDebug>_STC_ISP_DOWNLOAD_PAR ISP_DownloadPar; _ENUM_STC_ISP_DOWNLOAD isp_download_state; //程序下載情況 bool isp_finish_flag = true; //默認(rèn)為完成STC_ISP_Config::STC_ISP_Config(QObject *parent) : QObject(parent) {//新的對象聲明m_Process = new QProcess(this);// m_Process->setReadChannel(QProcess::StandardOutput);// m_Process->setProcessChannelMode(QProcess::ForwardedChannels);//可讀時(shí) 觸發(fā)信號connect(m_Process,&QProcess::readyRead,this,&STC_ISP_Config::Slot_Command_Info_Read);connect(m_Process,&QProcess::readyReadStandardError,this,&STC_ISP_Config::Slot_Command_Info_Read);connect(m_Process,SIGNAL(finished(int)),this,SLOT(Slot_Command_Finished(int))); }//燒錄程序 bool STC_ISP_Config::ISP_Write_Program(_STC_ISP_DOWNLOAD_PAR download_par) {ISP_DownloadPar = download_par;isp_download_state = ISP_DOWNLOADING; //正在燒錄//參數(shù)合法性檢查if(download_par.FirmwarePath.length() == 0){emit this->Signal_STC_ISP_Log(LOG_ERROR,QString("燒錄文件路徑無效,請重新選擇燒錄文件!"));return false;}//如果未完成 則退出if(!isp_finish_flag){emit this->Signal_STC_ISP_Log(LOG_ERROR,QString("上次燒錄程序未執(zhí)行完成,請稍后重試!"));return false;}//默認(rèn)處于未完成狀態(tài)isp_finish_flag = false;//文件主路徑QString Command_Main = QString("stcisp.exe");//arg賦值QStringList Command_Args;//型號Command_Args.append("-P");Command_Args.append("stc15");//串口號Command_Args.append("-p");Command_Args.append(download_par.ComName);//波特率Command_Args.append("-l");Command_Args.append("1200");//時(shí)鐘選擇Command_Args.append("-o");Command_Args.append("clock_source=internal");//時(shí)鐘頻率選擇Command_Args.append("-t");Command_Args.append(QString::number(download_par.CrystalFreq));//文件名Command_Args.append(download_par.FirmwarePath);//執(zhí)行m_Process->start(Command_Main,Command_Args);return true; }//讀取信息 void STC_ISP_Config::Slot_Command_Info_Read() {static uint32_t count = 0;QByteArray Temp_Array; //當(dāng)前數(shù)組int CurrentIndex = 0; //當(dāng)前的索引QByteArray read_info = m_Process->readAll();//信息判斷if(read_info.contains("Waiting for MCU, please cycle power")) //重新 上電{emit this->Signal_STC_ISP_Log(LOG_NORMAL,QString("串口連接成功,請重新上電!"));}else if(read_info.contains("Serial port error: could not open port ")) //串口無效或被占用{isp_download_state = ISP_DOWNLOAD_FAIL; //下載失敗emit this->Signal_STC_ISP_Log(LOG_ERROR,QString("打開串口 %1 失敗!").arg(ISP_DownloadPar.ComName));}else if(read_info.contains("Writing flash:")) //燒錄中{emit this->Signal_STC_ISP_Log(LOG_NORMAL,QString("串口燒錄中,請稍等..."));}else if(read_info.contains("Finishing write")){isp_download_state = ISP_DOWNLOAD_SUCC; //下載成功emit this->Signal_STC_ISP_Log(LOG_HEALTH,QString("程序燒錄完成!"));}qDebug() << read_info; }//出錯信息 void STC_ISP_Config::Slot_Command_Error_Read() {QByteArray read_info = m_Process->readAllStandardError(); }//完成 void STC_ISP_Config::Slot_Command_Finished(int exitCode) {isp_finish_flag = true; }

?

總結(jié)

以上是生活随笔為你收集整理的QT 编写 STC系列MCU烧录软件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。