.Net comunication between Matlab and C++

  .net, c++, matlab

I have two different process, one in Matlab and one in C++ under a Windows machine. Actually, I am working to develop a communication between them and, now, I am trying to use the .net APIs.

As a matter of fact, C++ has a main thread that acquire in input a set of data and, according to the input, do stuffs (in other threads) and wait for another input. Matlab has the main task to provide automatically the different inputs and, after that, return the output.

Here the simplified version:

C++

#include "main.h"
#include <string>
#include <iostream>

std::string m = "";

int main(int argc, char* argv[]) {
    while (true)
    {
        std::cin >> m;
        std::cout << "command: " << m << "n";
    }
}

that produce "myecho.exe"

Matlab

echoTest = 'myecho.exe';

proc = System.Diagnostics.Process();
proc.StartInfo.FileName = echoTest;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.Start();

proc.StandardInput.WriteLine('test1')
proc.StandardInput.Flush
while proc.StandardOutput.Peek ~= -1
    disp(proc.StandardOutput.ReadLine);
end

pause(1)

proc.StandardInput.WriteLine('test2')
proc.StandardInput.Flush
while proc.StandardOutput.Peek ~= -1
    disp(proc.StandardOutput.ReadLine);
end

pause(1)

proc.StandardInput.WriteLine('test3')
proc.StandardInput.Flush
while proc.StandardOutput.Peek ~= -1
    disp(proc.StandardOutput.ReadLine);
end

Unfortunately, Matlab is able to send only the first Input and collect the first output. However, the second input/output does not produce any results (peek = -1). Consequentially, the Matlab result is:

>>command: test1

>> 

Have you any idea regarding this strange behavior?!

Source: Windows Questions C++

LEAVE A COMMENT