reading non textual caracters from stdin lazarus / 21-General ---------------------------------------------------------------------- hakelm · 2023-09-05 15:26 I would like to process data from a program, rtl_sd, delivering more or less random bytes at high speed. I am. however, not able to pipe the data into my program. I have tried 2 approaches, neither of them works: var a:byte;  c:i32; begin   c:=0;   While not Eof do   Begin     Read (a);     inc(c);   end;   writeln(c); end. started by: rtl_sdr -f 867705 -n 10000 -|./trigged_rtl_sdr and var a:byte;  c:i32; var f:file of byte; cmd:string; begin   c:=0;   cmd:='rtl_sdr -f 867705 -n 10000 -';     writeln(cmd);     popen(f,cmd,'R');     while (not eof(f)) do     begin       read(f,a);       inc(c);     end;     pclose(f);   end;   writeln(c); end. Any tip is appreciated. H ---------------------------------------------------------------------- bobby100 · 2023-09-05 16:12 Did you take a look at FpPipe? https://www.freepascal.org/docs-html/rtl/baseunix/fppipe.html ---------------------------------------------------------------------- hakelm · 2023-09-05 18:19 Thanks, but I can't see how to apply fpopen. ---------------------------------------------------------------------- Thaddy · 2023-09-05 18:40 Are you on windows? ---------------------------------------------------------------------- hakelm · 2023-09-05 20:02 No I have Ubuntu 22.04 and use Free Pascal Compiler version 3.2.2 . ---------------------------------------------------------------------- hakelm · 2023-09-06 08:55 Reading binary data works with named pipes, see below. But why I can't use stdin,  a normal pipe or popen avoids me. Can somebody please explain. H 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 program trigge_drtl_sdr;{$mode objfpc}{$H+}uses  cthreads, Classes, sysutils, unix;var a:byte;  cmd:string;  s:TFileStream; n:integer;const fn='/home/he/a';begin  cmd:='rm -f '+fn+'; mkfifo '+fn+'; rtl_sdr -f 867705 -n 10000 '+fn+ '&';  fpsystem(cmd);  s:=TFileStream.Create(fn,fmOpenRead);  n:=1;  while (n>0) do  begin  n:=s.read(a,1);    write(inttohex(a,2),' ');  end;end. ---------------------------------------------------------------------- Zvoni · 2023-09-06 09:21 Quote from: hakelm on September 06, 2023, 10:55:44 amReading binary data works with named pipes, see below. But why I can't use stdin,  a normal pipe or popen avoids me. Can somebody please explain. H 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 program trigge_drtl_sdr;{$mode objfpc}{$H+}uses  cthreads, Classes, sysutils, unix, nhe,hefpcvar a:byte;  c:i32; cmd:string;  s:TFileStream; n:i32;const fn='/home/he/a';begin  c:=0;  cmd:='rm -f '+fn+'; mkfifo '+fn+'; rtl_sdr -f 867705 -n 10000 '+fn+ '&';  fpsystem(cmd);  s:=TFileStream.Create(fn,fmOpenRead);  n:=1;  while (n>0) do  begin  n:=s.read(a,1);    write(inttohex(a,2),' ');  end;end.      I'd be more surprised this compiles..... ---------------------------------------------------------------------- hakelm · 2023-09-06 09:24 somehow the semicolon after the uses clause got lost. Sorrry. Proper code: 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 program trigge_drtl_sdr;{$mode objfpc}{$H+}uses  cthreads, Classes, sysutils, unix;var a:byte;  cmd:string;  s:TFileStream; n:integer;const fn='/home/he/a';begin  cmd:='rm -f '+fn+'; mkfifo '+fn+'; rtl_sdr -f 867705 -n 10000 '+fn+ '&';  fpsystem(cmd);  s:=TFileStream.Create(fn,fmOpenRead);  n:=1;  while (n>0) do  begin  n:=s.read(a,1);    write(inttohex(a,2),' ');  end;end. ---------------------------------------------------------------------- tetrastes · 2023-09-06 20:54 Quote from: hakelm on September 06, 2023, 10:55:44 amReading binary data works with named pipes, see below. But why I can't use stdin,  a normal pipe or popen avoids me. Can somebody please explain. You cannot use stdin, because it is Text, not File of byte, and Read(byte) accepts strings '0'...'255', not bytes. Using popen should work, but what is EOF(f) in your case? This code works: 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 uses    baseunix, unix, sysutils; var a: byte;    c: longint;    f: file;    cmd: ansistring;    res: longint;    popen_res: cint; begin  c := 0;  cmd :='echo 0 1 255';  writeln(cmd);  if not FileExists('dummy') then FileCreate('dummy');  Assign(f, 'dummy');  reset(f, 1);  popen_res := popen(f,cmd,'R');  if popen_res <> 0 then     writeln('Error popen: ', fpgeterrno)  else  begin      repeat          BlockRead(f, a, 1, res);          if res = 1 then          begin              inc(c);            write(IntToHex(a, 2), ' ');          end;      until res = 0;      writeln;  end;  writeln(c);  pclose(f);end.