2

我正在尝试将 Qt 控制台应用程序与另一个用 C 编写的控制台应用程序(国际象棋引擎 - TSCP)进行通信。

我创建了这个类:

#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;
    }
}

我的 main.cpp 看起来就像这样:

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

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

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

    return a.exec();
}

我正在努力实现:

  1. 启动进程 - 国际象棋引擎。
  2. 向进程-引擎发送命令。
  3. 接收答案(能够显示、处理)。
  4. 继续通信(步骤 2、3)。
  5. 关闭进程。

第一步和最后一步我没有任何困难 - 它正在工作。问题是我无法与国际象棋引擎(控制台应用程序)正常通信。

我肯定做错了什么!;) 如果我取消注释 process->closeWriteChannel(); 在 void Engine::Write( QByteArray writeBuff ) 函数中,我可以写一个命令(例如“on”开始游戏)并接收正确的输出 - 来自国际象棋引擎的响应(移动)。我无法发送下一个推荐,因为通道已关闭(很明显)。如果此行被评论,我将无法收到任何信息。

我想要做:

  1. 启动流程:

    发动机发动机;engine.startProcess();

  2. 沟通(发送用户并接收引擎移动):

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

  3. 关闭进程。

我尽力找到答案(帮助,谷歌)。你能帮我找到解决办法吗?

谢谢!

4

1 回答 1

1

我发现您的代码有两个问题:

  1. 你调用writeBuf.clear()你的Engine::Write成员函数,不要那样做。
  2. 我猜你的外部程序通过寻找换行符来分隔命令。请尝试类似engine.Write( "a2a3\n" ).
于 2013-02-25T17:38:34.807 回答