Refactor(nothing changed actually)

This commit is contained in:
wh201906
2020-04-22 23:13:00 +08:00
parent a325e3b670
commit abfc940dc8
9 changed files with 358 additions and 354 deletions
+17 -17
View File
@@ -4,14 +4,14 @@ PM3Process::PM3Process(QThread* thread, QObject* parent): QProcess(parent)
{
moveToThread(thread);
setProcessChannelMode(PM3Process::MergedChannels);
isRequiringOutput=false;
requiredOutput=new QString();
serialListener=new QTimer(); // if using new QTimer(this), the debug output will show "Cannot create children for a parent that is in a different thread."
isRequiringOutput = false;
requiredOutput = new QString();
serialListener = new QTimer(); // if using new QTimer(this), the debug output will show "Cannot create children for a parent that is in a different thread."
serialListener->moveToThread(this->thread());// I've tried many ways to creat a QTimer instance, but all of the instances are in the main thread(UI thread), so I have to move it manually
serialListener->setInterval(1000);
serialListener->setTimerType(Qt::VeryCoarseTimer);
connect(serialListener,&QTimer::timeout,this,&PM3Process::onTimeout);
connect(this,&PM3Process::readyRead,this,&PM3Process::onReadyRead);
connect(serialListener, &QTimer::timeout, this, &PM3Process::onTimeout);
connect(this, &PM3Process::readyRead, this, &PM3Process::onReadyRead);
}
void PM3Process::connectPM3(const QString path, const QString port)
@@ -19,20 +19,20 @@ void PM3Process::connectPM3(const QString path, const QString port)
setRequiringOutput(true);
// using "-f" option to make the client output flushed after every print.
start(path, QStringList()<<port<<"-f",QProcess::Unbuffered|QProcess::ReadWrite);
start(path, QStringList() << port << "-f", QProcess::Unbuffered | QProcess::ReadWrite);
if(waitForStarted(10000))
{
while(waitForReadyRead(1000))
;
setRequiringOutput(false);
QString result = *requiredOutput;
if(result.indexOf("os: ")!=-1)// make sure the PM3 is connected
if(result.indexOf("os: ") != -1) // make sure the PM3 is connected
{
result = result.mid(result.indexOf("os: "));
result = result.left(result.indexOf("\r\n"));
result = result.mid(3, result.lastIndexOf(" ") - 3);
emit PM3StatedChanged(true,result);
setSerialListener(port,true);
emit PM3StatedChanged(true, result);
setSerialListener(port, true);
}
else
kill();
@@ -41,7 +41,7 @@ void PM3Process::connectPM3(const QString path, const QString port)
void PM3Process::setRequiringOutput(bool st)
{
isRequiringOutput=st;
isRequiringOutput = st;
if(isRequiringOutput)
requiredOutput->clear();
}
@@ -51,13 +51,13 @@ bool PM3Process::waitForReadyRead(int msecs)
return QProcess::waitForReadyRead(msecs);
}
void PM3Process::setSerialListener(const QString& name,bool state)
void PM3Process::setSerialListener(const QString& name, bool state)
{
if(state)
{
portInfo=new QSerialPortInfo(name);
portInfo = new QSerialPortInfo(name);
serialListener->start();
qDebug()<<serialListener->thread();
qDebug() << serialListener->thread();
}
else
{
@@ -68,18 +68,18 @@ void PM3Process::setSerialListener(const QString& name,bool state)
void PM3Process::onTimeout() //when the proxmark3 client is unexpectedly terminated or the PM3 hardware is removed, the isBusy() will return false(only tested on Windows);
{
qDebug()<<portInfo->isBusy();
// qDebug()<<portInfo->isBusy();
if(!portInfo->isBusy())
{
kill();
emit PM3StatedChanged(false);
setSerialListener("",false);
setSerialListener("", false);
}
}
void PM3Process::testThread()
{
qDebug()<<"PM3:"<<QThread::currentThread();
qDebug() << "PM3:" << QThread::currentThread();
}
@@ -95,7 +95,7 @@ void PM3Process::onReadyRead()
requiredOutput->append(out);
if(out != "")
{
qDebug()<<"PM3Process::onReadyRead:" << out;
qDebug() << "PM3Process::onReadyRead:" << out;
emit newOutput(out);
}
+2 -2
View File
@@ -13,7 +13,7 @@ class PM3Process : public QProcess
{
Q_OBJECT
public:
explicit PM3Process(QThread* thread, QObject* parent=nullptr);
explicit PM3Process(QThread* thread, QObject* parent = nullptr);
bool waitForReadyRead(int msecs = 2000);
void testThread();
@@ -32,7 +32,7 @@ private:
QTimer* serialListener;
QSerialPortInfo* portInfo;
signals:
void PM3StatedChanged(bool st, QString info="");
void PM3StatedChanged(bool st, QString info = "");
void newOutput(QString output);
};
+11 -11
View File
@@ -2,18 +2,18 @@
Util::Util(QObject *parent) : QObject(parent)
{
isRequiringOutput=false;
requiredOutput=new QString();
timeStamp=QTime::currentTime();
isRequiringOutput = false;
requiredOutput = new QString();
timeStamp = QTime::currentTime();
}
void Util::processOutput(QString output)
{
qDebug()<<"Util::processOutput:" << output;
qDebug() << "Util::processOutput:" << output;
if(isRequiringOutput)
{
requiredOutput->append(output);
timeStamp=QTime::currentTime();
timeStamp = QTime::currentTime();
}
emit refreshOutput(output);
}
@@ -26,21 +26,21 @@ void Util::execCMD(QString cmd)
QString Util::execCMDWithOutput(QString cmd, unsigned long timeout)
{
QTime currTime=QTime::currentTime();
QTime currTime = QTime::currentTime();
QTime targetTime = QTime::currentTime().addMSecs(timeout);
isRequiringOutput=true;
isRequiringOutput = true;
requiredOutput->clear();
execCMD(cmd);
while( QTime::currentTime() < targetTime)
{
QApplication::processEvents();
if(timeStamp>currTime)
if(timeStamp > currTime)
{
currTime=timeStamp;
targetTime=timeStamp.addMSecs(timeout);
currTime = timeStamp;
targetTime = timeStamp.addMSecs(timeout);
}
}
isRequiringOutput=false;
isRequiringOutput = false;
return *requiredOutput;
}
+1 -1
View File
@@ -16,7 +16,7 @@ public:
explicit Util(QObject *parent = nullptr);
void execCMD(QString cmd);
QString execCMDWithOutput(QString cmd, unsigned long timeout=2000);
QString execCMDWithOutput(QString cmd, unsigned long timeout = 2000);
void delay(unsigned int msec);
public slots:
void processOutput(QString output);