2020-04-06 23:48:08 +08:00
# include " pm3process.h "
2020-04-21 19:12:44 +08:00
PM3Process : : PM3Process ( QThread * thread , QObject * parent ) : QProcess ( parent )
2020-04-06 23:48:08 +08:00
{
2020-04-21 19:12:44 +08:00
moveToThread ( thread ) ;
2020-04-07 18:16:00 +08:00
setProcessChannelMode ( PM3Process : : MergedChannels ) ;
2020-04-11 23:53:06 +08:00
isRequiringOutput = false ;
requiredOutput = new QString ( ) ;
2020-04-21 19:12:44 +08:00
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
2020-04-18 02:01:42 +08:00
serialListener - > setInterval ( 1000 ) ;
serialListener - > setTimerType ( Qt : : VeryCoarseTimer ) ;
connect ( serialListener , & QTimer : : timeout , this , & PM3Process : : onTimeout ) ;
2020-04-07 18:16:00 +08:00
}
2020-04-06 23:48:08 +08:00
2020-04-21 19:12:44 +08:00
void PM3Process : : connectPM3 ( const QString path , const QString port )
2020-04-07 18:16:00 +08:00
{
2020-04-21 19:12:44 +08:00
setRequiringOutput ( true ) ;
2020-04-06 23:48:08 +08:00
2020-04-07 18:16:00 +08:00
// using "-f" option to make the client output flushed after every print.
2020-04-21 19:12:44 +08:00
start ( path , QStringList ( ) < < port < < " -f " , QProcess : : Unbuffered | QProcess : : ReadWrite ) ;
if ( waitForStarted ( 10000 ) )
2020-04-18 02:01:42 +08:00
{
2020-04-21 19:12:44 +08:00
while ( waitForReadyRead ( 1000 ) )
;
setRequiringOutput ( false ) ;
QString result = * requiredOutput ;
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 ) ;
}
else
kill ( ) ;
2020-04-18 02:01:42 +08:00
}
2020-04-06 23:48:08 +08:00
}
2020-04-11 23:53:06 +08:00
void PM3Process : : setRequiringOutput ( bool st )
{
isRequiringOutput = st ;
if ( isRequiringOutput )
requiredOutput - > clear ( ) ;
}
QByteArray PM3Process : : readLine ( qint64 maxlen )
{
QByteArray buff ;
buff = QProcess : : readLine ( maxlen ) ;
if ( isRequiringOutput )
requiredOutput - > append ( buff ) ;
return buff ;
}
QString PM3Process : : getRequiredOutput ( )
{
return * requiredOutput ;
}
2020-04-15 00:38:43 +08:00
bool PM3Process : : waitForReadyRead ( int msecs )
{
return QProcess : : waitForReadyRead ( msecs ) ;
}
2020-04-16 00:39:10 +08:00
2020-04-18 02:01:42 +08:00
void PM3Process : : setSerialListener ( const QString & name , bool state )
{
if ( state )
{
portInfo = new QSerialPortInfo ( name ) ;
serialListener - > start ( ) ;
2020-04-21 19:12:44 +08:00
qDebug ( ) < < serialListener - > thread ( ) ;
2020-04-18 02:01:42 +08:00
}
else
{
serialListener - > stop ( ) ;
delete portInfo ;
}
}
void PM3Process : : onTimeout ( )
{
qDebug ( ) < < portInfo - > isBusy ( ) ;
if ( ! portInfo - > isBusy ( ) )
{
2020-04-21 19:12:44 +08:00
kill ( ) ;
emit PM3StatedChanged ( false ) ;
2020-04-18 02:01:42 +08:00
setSerialListener ( " " , false ) ;
}
}
2020-04-21 19:12:44 +08:00
void PM3Process : : testThread ( )
{
qDebug ( ) < < " PM3: " < < QThread : : currentThread ( ) ;
}