重构,将OpenOCD进程控制相关代码提取为一个独立模块

This commit is contained in:
skythinker 2022-01-31 00:06:57 +08:00
parent a803f3636c
commit 5a66c64a59
6 changed files with 91 additions and 46 deletions

View File

@ -24,7 +24,8 @@ SOURCES += \
helpwindow.cpp \
listwindow.cpp \
main.cpp \
mainwindow.cpp
mainwindow.cpp \
openocd.cpp
HEADERS += \
aboutwindow.h \
@ -33,6 +34,7 @@ HEADERS += \
helpwindow.h \
listwindow.h \
mainwindow.h \
openocd.h \
vartype.h
FORMS += \

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.0, 2022-01-30T18:29:16. -->
<!-- Written by QtCreator 4.11.0, 2022-01-31T00:06:10. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>

View File

@ -35,8 +35,11 @@ MainWindow::MainWindow(QWidget *parent)
tableModel=new QStandardItemModel(this);//创建并初始化表格
initTable();
ocdProcess=new QProcess(0);//创建openocd进程
connect(ocdProcess,SIGNAL(readyReadStandardError()),this,SLOT(slotOCDErrorReady()));
openocd=new OpenOCD();
connect(openocd,&OpenOCD::onErrorOccur,[=](const QString &info){//OpenOCD发生连接错误断开连接并弹框
setConnState(false);
QMessageBox::information(this,"连接错误",info);
});
gdb=new GDBProcess();//创建并启动GDB
gdb->setTempSymbolFileName("tmp");//设定临时符号文件名
@ -51,11 +54,12 @@ MainWindow::~MainWindow()
{
gdb->disconnectFromRemote();
gdb->unloadSymbolFile();
openocd->stop();
}
gdb->stop();//结束gdb进程
delete ui;
delete tableModel;
delete ocdProcess;
delete openocd;
delete gdb;
delete stampTimer;
delete watchTimer;
@ -95,17 +99,6 @@ void MainWindow::closeEvent(QCloseEvent *event)
listWindow->close();
}
//ocd进程发生错误自动断开连接
void MainWindow::slotOCDErrorReady()
{
QString error=ocdProcess->readAllStandardError();
if(error.contains("Error:"))//错误信息中含有Error项则直接断开连接并弹出提示
{
setConnState(false);
QMessageBox::information(this,"连接错误",error);
}
}
//表格被编辑,添加变量或修改变量值
void MainWindow::slotTableEdit(QModelIndex topleft, QModelIndex bottomright)
{
@ -223,9 +216,9 @@ void MainWindow::setConnState(bool connect)
if(connect)//进行连接
{
ui->bt_conn->setEnabled(false);//先禁用连接按钮,防止多次点击
setOCDState(true);//运行openocd进程进行目标连接
openocd->start(ui->cb_interface->currentText(),ui->cb_target->currentText(),3333);//运行openocd进程进行目标连接
sleep(500);//等待500ms
if(ocdProcess->state()==QProcess::Running)//若ocd成功启动
if(openocd->isRunning())//若ocd成功启动
{
gdb->loadSymbolFile(ui->txt_axf_path->text());//设置gdb符号文件
gdb->connectToRemote("localhost:3333");//连接gdb到ocd
@ -254,38 +247,13 @@ void MainWindow::setConnState(bool connect)
tableTimer->stop();
gdb->disconnectFromRemote();//断开gdb
gdb->unloadSymbolFile();//卸载符号文件
setOCDState(false);//结束ocd进程
openocd->stop();
ui->bt_conn->setText("连接目标");
ui->bt_reset->setEnabled(false);//禁用复位按钮
connected=false;//更新连接标志
}
}
//参数true运行ocd进程并连接目标参数false结束ocd进程
void MainWindow::setOCDState(bool connect)
{
if(connect)
{
ocdProcess->setWorkingDirectory(QCoreApplication::applicationDirPath()+"/openocd/bin");//设置工作路径
ocdProcess->setProgram(QCoreApplication::applicationDirPath()+"/openocd/bin/openocd.exe");//设置程序路径
ocdProcess->setNativeArguments(
QString("-f interface/%1 -f target/%2")
.arg(ui->cb_interface->currentText())
.arg(ui->cb_target->currentText()));//设置参数为所选的调试器和目标芯片
ocdProcess->start();
}
else
{
if(ocdProcess->state()==QProcess::NotRunning)
return;
QProcess killProcess(0);//创建新进程用taskkill强行结束ocd进程
killProcess.setProgram("taskkill");
killProcess.setNativeArguments(QString("/F /PID %1").arg(ocdProcess->pid()->dwProcessId));
killProcess.start();
killProcess.waitForFinished();
}
}
//延时指定毫秒数,进行子任务循环
void MainWindow::sleep(uint32_t ms)
{

View File

@ -25,6 +25,7 @@
#include <qnetworkaccessmanager.h>
#include <qnetworkreply.h>
#include <gdbprocess.h>
#include <openocd.h>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -41,7 +42,7 @@ public:
void closeEvent(QCloseEvent *event);
private slots:
void slotOCDErrorReady();
//void slotOCDErrorReady();
void slotTableEdit(QModelIndex topleft, QModelIndex bottomright);
void slotWatchTimerTrig();
void slotTableTimerTrig();
@ -68,6 +69,7 @@ private:
Ui::MainWindow *ui;
QProcess *ocdProcess;
GDBProcess *gdb;//GDB进程控制
OpenOCD *openocd;//OpenOCD进程控制
bool connected=false;//标记当前是否已连接
QStandardItemModel *tableModel;//表格数据
QList<VarInfo> varList;//变量列表
@ -80,7 +82,7 @@ private:
void checkUpdate();
void setStylesheet();
void setConnState(bool connect);
void setOCDState(bool connect);
//void setOCDState(bool connect);
void sleep(uint32_t ms);
void loadConfFileList();
void initTable();

47
openocd.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "openocd.h"
OpenOCD::OpenOCD(QObject *parent) : QObject(parent)
{
process=new QProcess();
connect(process,&QProcess::readyReadStandardError,[=]{
QString error=process->readAllStandardError();
if(error.contains("Error:"))//错误信息中含有Error项则发送信号
{
emit onErrorOccur(error);
}
});
}
OpenOCD::~OpenOCD()
{
delete process;
}
//启动OCD进程并设定调试器类型、芯片类型、gdb端口号
void OpenOCD::start(const QString &chosenInterface,const QString &chosenTarget,int gdbPort)
{
process->setWorkingDirectory(QCoreApplication::applicationDirPath()+"/openocd/bin");//设置工作路径
process->setProgram(QCoreApplication::applicationDirPath()+"/openocd/bin/openocd.exe");//设置程序路径
process->setNativeArguments(
QString("-f interface/%1 -f target/%2 -c \"gdb_port %3\"")
.arg(chosenInterface).arg(chosenTarget).arg(gdbPort));//设置参数为所选的调试器、目标芯片和gdb端口
process->start();
}
//结束OCD进程
void OpenOCD::stop()
{
if(process->state()==QProcess::NotRunning)
return;
QProcess killProcess(0);//创建新进程用taskkill强行结束ocd进程
killProcess.setProgram("taskkill");
killProcess.setNativeArguments(QString("/F /PID %1").arg(process->pid()->dwProcessId));
killProcess.start();
killProcess.waitForFinished();
}
//返回当前OCD进程是否正在运行
bool OpenOCD::isRunning()
{
return process->state()==QProcess::Running;
}

26
openocd.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef OPENOCD_H
#define OPENOCD_H
#include <QObject>
#include <QProcess>
#include <qcoreapplication.h>
#include <windows.h>
class OpenOCD : public QObject
{
Q_OBJECT
public:
explicit OpenOCD(QObject *parent = nullptr);
~OpenOCD();
void start(const QString &chosenInterface,const QString &chosenTarget,int gdbPort);
void stop();
bool isRunning();
signals:
void onErrorOccur(const QString &info);
private:
QProcess *process;
};
#endif // OPENOCD_H