Add Sleep inside the while loop, too.
Added it per recommendation; still no joy
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:
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) then
begin
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
POctave.Input.Write(command, Length(command));
He was calling:
POctave.Input.Write(command[1], Length(command));
The final result:
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) then
begin
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#StringsThey're length prefixed. I'm...an idiot.