Recent

Author Topic: Using named pipes  (Read 5960 times)

JPT

  • Newbie
  • Posts: 3
Using named pipes
« on: November 26, 2014, 02:13:11 pm »
Hi,

I try to exchange messages between two programs via named pipes.
Doesn't work, because they wait forever on opening the pipes even if there is no other program active.
Or crash when another app tries to access the pipes (e.g. cat)

Code: [Select]
program testserver;

{$MODE Delphi}

uses
Classes,SysUtils,Crt;

var
  output: TFileStream;
  input: TFileStream;
  buffer: String[255];
  size: integer;
  ctr: integer;

begin
  output := TFileStream.Create('input.pipe',  fmOpenWrite or fmShareDenyWrite);
  input  := TFileStream.Create('output.pipe', fmOpenRead or  fmShareDenyRead);
 
  try
    buffer:='';
    ctr:=0;
    while not keypressed do begin
      size:= input.read(buffer[1], 1);
      if (size > 0) then begin
        writeln('Request: ', buffer, InttoHex(byte(buffer[1]),2));     
        if buffer[1] = '#128' then begin // data requested
          inc(ctr);
          buffer:= IntToStr(ctr) + '.0'#13#10;
        end;
        writeln('Answer: ', buffer);     
        output.write(buffer[1], length(buffer));
      end;
      sleep(200);
    end;
  finally
    output.free;
    input.free;
  end;
end.

Background:
I have to port an old DOS based app that reads measurements from serial port.
The hardware is still in production, so I don't have access to it.
As you can see, this program is emulating the hardware. the other program looks quite the same, except the file names are opposite and it is requesting data.
I added the fmShareDeny... constants but this did not help.

any idea?

thanks,

Jan


JPT

  • Newbie
  • Posts: 3
Re: Using named pipes
« Reply #1 on: November 26, 2014, 02:18:21 pm »
Changed to mode OBJFPC this changed the behavior:
Start A, opens input, waits on output.
Start B, opens input, waits on output, crashs A
Start A, opens input, waits on output, crashs B

An unhandled exception occurred at $08065ECF :
EFOpenError : Unable to open file "output.pipe"
$08065ECF
$08048166

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11444
  • FPC developer.
Re: Using named pipes
« Reply #2 on: November 26, 2014, 02:51:38 pm »
I don't see any pipes in your examples. You try to open two different normal files "input.pipe" and "output.pipe", and do something with totally unrelated function crt.keypressed!?!?!?

JPT

  • Newbie
  • Posts: 3
Re: Using named pipes
« Reply #3 on: November 26, 2014, 03:00:36 pm »
I created the files using mkfifo.
I can remove the keypressed and use while true...
I don't think this will change anything.

I already tried AssignPipe and FOpen but it seems they both do something completely different.
« Last Edit: November 26, 2014, 03:02:11 pm by JPT »

 

TinyPortal © 2005-2018