简体   繁体   中英

(Qt - QProcess) Communication with console application

I m trying to communicate Qt console application with another console application written in C (chess engine - TSCP).

I created this class:

#include "engine.h"

Engine::Engine(QObject *parent) :
    QProcess(parent)
{
}

Engine::~Engine()
{
    delete process;
}

void Engine::startProcess()
{
    process = new QProcess( this );

    process->setReadChannel( QProcess::StandardOutput );

    connect( process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)) );
    connect( process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int,QProcess::ExitStatus)) );
    connect( process, SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()) );
    connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()) );
    connect( process, SIGNAL(started()), this, SLOT(started()) );
    connect( process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(stateChanged(QProcess::ProcessState)) );

    //process->start( "/usr/bin/konsole --nofork -e ./TSCP" );
    process->start( "./TSCP" );
    process->waitForStarted(-1);
}

void Engine::stopProcess()
{
    //process->write( "bye" );
    process->closeWriteChannel();
}

void Engine::Write( QByteArray writeBuff )
{
    writeBuff.clear();
    qDebug() << "Sending command: " + writeBuff;
    process->write( writeBuff );
    //process->closeWriteChannel();
}

QByteArray Engine::Read()
{
    readBuffer = process->readAllStandardOutput();
    return readBuffer;
}

void Engine::error( QProcess::ProcessError error )
{
    qDebug() << "Error!";
    qDebug() << error;
}

void Engine::finished( int exitCode, QProcess::ExitStatus exitStatus )
{
    qDebug() << "The process has finished.";
    qDebug( "Exit code: %i", exitCode );
    qDebug( "Exit status: %i", exitStatus );
}

void Engine::readyReadStandardError()
{
    qDebug() << "Ready to read error.";
    qDebug() << process->readAllStandardError();
}

void Engine::readyReadStandardOutput()
{
    qDebug() << "The output:";
    readBuffer = process->readAllStandardOutput();
    qDebug() << readBuffer;
    //process->closeReadChannel( QProcess::StandardOutput );
    //process->waitForBytesWritten();
}

void Engine::started()
{
    qDebug() << "The process has started.";
}

void Engine::stateChanged( QProcess::ProcessState newState )
{
    switch( newState )
    {
    case 0:
        qDebug() << "The process is not running.";
        break;
    case 1:
        qDebug() << "The process is starting, but the program has not yet been invoked.";
        break;
    case 2:
        qDebug() << "The process is running and is ready for reading and writing.";
        break;
    }
}

My main.cpp looks just like that:

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    qDebug() << "----------------------------------";

    Engine engine;
    engine.startProcess();
    engine.Write( "on" );
    qDebug() << "----------------------------------";

    return a.exec();
}

I am trying to achieve:

  1. Start the process - chess engine.
  2. Send a command to the process - engine.
  3. Receive an answer (be able to display, process it).
  4. Continue the communication (steps 2, 3).
  5. Close the process.

I don't have any difficulties with the first and last step - it's working. The problem is that i can't communicate properly with the chess engine (console application).

I am definitely doing something wrong! ;) If I uncomment process->closeWriteChannel(); in void Engine::Write( QByteArray writeBuff ) function than I can write one commend (for exampe 'on' to start the game) and receive the correct output - response from the chess engine (move). I cannot send the next commend, because the channel is closed (it is obvious). If this line is commented I cannot receive any information.

I would like to do:

  1. Start the process:

    Engine engine; engine.startProcess();

  2. Comunicate (send the user and receive the engine move):

    engine.Write( "a2a3" ); engine.Write( "b2b3" ); engine.Write( "c2c3" );

  3. Close the process.

I did my best to find the answer (help, google). Can you help me to find the solution?

Thank you!

I see two problems with your code:

  1. you call writeBuf.clear() in your Engine::Write member function, don't do that.
  2. I guess your external program separates commands by looking for a newline character. Please try something like engine.Write( "a2a3\\n" ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM