[SOLVED] TProcess with Octave 4.2 On Win32 lazarus / 13-General ---------------------------------------------------------------------- trayres · 2017-02-13 07:38 Has anyone tried this recently with Octave 4.2 on Win32? There is a thread from 2009 on this located here: http://forum.lazarus.freepascal.org/index.php?topic=6693.0 Question XPosted to SO here: http://stackoverflow.com/questions/42214845/interfacing-octave-and- lazarus-freepascal-with-tprocess If I use the octave-cli.exe, I get the TProcess.Running property to be True, but I'm unable to grab bytes from it. It looks like the octave.vbs script calls octave-gui.exe within /bin, then passes --no-gui and 1 to it; I'm not quite sure what's going on. Code below, primitive  project as attachment! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Process; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1;  POctave: TProcess; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  if (not POctave.Running) then  begin    POctave.Executable := 'C:\Octave\Octave-4.2.0\bin\octave-cli.exe';    POctave.Parameters.Add('--no-gui');    POctave.Options := [poUsePipes];    WriteLn('-- Executing octave --');    POctave.Execute;  end; end; procedure TForm1.Button2Click(Sender: TObject);var  command: string;  Buffer: string;  BytesAvailable: DWord;  BytesRead: longint;  NoMoreOutput: boolean;begin  command := 'pwd' + #10;  if (POctave.Running) then    POctave.Input.Write(command, Length(command));   if (POctave.Running) then  begin    BytesAvailable := POctave.Output.NumBytesAvailable;    BytesRead := 0;    while BytesAvailable > 0 do    begin      SetLength(Buffer, BytesAvailable);      BytesRead := POctave.OutPut.Read(Buffer[1], BytesAvailable);      WriteLn(Buffer);      BytesAvailable := POctave.Output.NumBytesAvailable;      NoMoreOutput := False;    end;  end;end; initialization  POctave := TProcess.Create(nil); finalization  POctave.Free; end. ---------------------------------------------------------------------- Cyrax · 2017-02-13 12:07 Add Sleep(10); clause before you check TProcess.Output.NumBytesAvailable. OS/the process needs some time to fill up the pipe(s). After the process has done its execution, check again if there is something left in the pipe(s). ---------------------------------------------------------------------- trayres · 2017-02-13 17:38 I added Sleep(10): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  command: string;  Buffer: string;  BytesAvailable: DWord;  BytesRead: longint;  NoMoreOutput: boolean;begin  command := 'pwd' + #10;  if (POctave.Running) then    POctave.Input.Write(command, Length(command));   Sleep(10);  if (POctave.Running) then  begin    BytesAvailable := POctave.Output.NumBytesAvailable;    BytesRead := 0;    while BytesAvailable > 0 do    begin      SetLength(Buffer, BytesAvailable);      BytesRead := POctave.OutPut.Read(Buffer[1], BytesAvailable);      WriteLn(Buffer);      BytesAvailable := POctave.Output.NumBytesAvailable;      NoMoreOutput := False;    end;  end;end; I'm still not seeing anything in the buffer  I also tried @LeLeDumbo's OctaveGUI with 4.2 and wasn't able to get it to work. ---------------------------------------------------------------------- Cyrax · 2017-02-13 20:00 Add Sleep inside the while loop, too. ---------------------------------------------------------------------- trayres · 2017-02-13 20:09 Quote from: Cyrax on February 13, 2017, 09:00:25 pmAdd Sleep inside the while loop, too. Added it per recommendation; still no joy Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  command: string;  Buffer: string;  BytesAvailable: DWord;  BytesRead: longint;  NoMoreOutput: boolean;begin  command := 'pwd'; // + #10;  if (POctave.Running) then    POctave.Input.Write(command, Length(command));   Sleep(50);  if (POctave.Running) then  begin    Sleep(50);    BytesAvailable := POctave.Output.NumBytesAvailable;    BytesRead := 0;    while BytesAvailable > 0 do    begin      SetLength(Buffer, BytesAvailable);      BytesRead := POctave.OutPut.Read(Buffer[1], BytesAvailable);      WriteLn(Buffer);      BytesAvailable := POctave.Output.NumBytesAvailable;      Sleep(10);      NoMoreOutput := False;    end;  end;end;   I also just tried changing the Octave command to end with #13#10, just in case it was some kind of platform specific 'return' key silliness: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  command: ansistring;  Buffer: string;  BytesAvailable: DWord;  BytesRead: longint;  NoMoreOutput: boolean;begin  command := 'pwd'#13#10;  if (POctave.Running) then    POctave.Input.Write(command, Length(command));  Sleep(100);   if (POctave.Running) thenbegin    Sleep(100);    BytesAvailable := POctave.Output.NumBytesAvailable;    BytesRead := 0;    while BytesAvailable > 0 do    begin      Sleep(100);      SetLength(Buffer, BytesAvailable);      BytesRead := POctave.OutPut.Read(Buffer[1], BytesAvailable);      WriteLn(Buffer);      BytesAvailable := POctave.Output.NumBytesAvailable;      NoMoreOutput := False;    end;  end;end;   Still no go. I also tried pointing at octave-gui.exe instead of octave-cli.exe. However! The following solved it (I started from @LeLeDumbo's code and noticed he wasn't calling Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i POctave.Input.Write(command, Length(command));  He was calling: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i POctave.Input.Write(command[1], Length(command)); The final result: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  command: ansistring;  Buffer: string;  BytesAvailable: DWord;  BytesRead: longint;  NoMoreOutput: boolean;begin  command := 'pwd'#10;  if (POctave.Running) then begin    WriteLn(command[1]);    POctave.Input.Write(command[1], Length(command));  //<----command[1] instead of command  end;  Sleep(20);   if (POctave.Running) thenbegin    Sleep(20);    BytesAvailable := POctave.Output.NumBytesAvailable;  BytesRead := 0;    while BytesAvailable > 0 do    begin      Sleep(20);      SetLength(Buffer, BytesAvailable);      BytesRead := POctave.OutPut.Read(Buffer[1], BytesAvailable);      WriteLn(Buffer);      BytesAvailable := POctave.Output.NumBytesAvailable;      NoMoreOutput := False;    end;  end;end;   Now it runs like a champ! I did writeln[0] - and remembered something from many, many moons ago: https://en.wikipedia.org/wiki/Comparison_of_Pascal_and_C#Strings They're length prefixed. I'm...an idiot.