我正在尝试将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();
}

我正在努力实现:
  • 启动进程-国际象棋引擎。
  • 向流程引擎发送命令。
  • 接收答案(能够显示,处理它)。
  • 继续通信(步骤2、3)。
  • 关闭进程。

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

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

    我想要做:
  • 开始过程:

    引擎引擎;
    engine.startProcess();
  • Comunicate(发送用户并接收引擎移动):

    engine.Write(“a2a3”);
    engine.Write(“b2b3”);
    engine.Write(“c2c3”);
  • 关闭进程。

  • 我尽力找到答案(帮助,谷歌)。您能帮我找到解决方案吗?

    谢谢!

    最佳答案

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

  • 您可以在writeBuf.clear()成员函数中调用Engine::Write,请不要这样做。
  • 我猜您的外部程序通过寻找换行符来分隔命令。请尝试类似engine.Write( "a2a3\n" )的方法。
  • 10-07 16:20
    查看更多