[fpc-pascal] Using Serial in a TCP/RS232 gateway, how to set buffer sizes?

Bo Berglund bo.berglund at gmail.com
Wed Sep 6 21:22:33 CEST 2017


On Wed, 6 Sep 2017 18:58:57 +0000, Mark Morgan Lloyd
<markMLl.fpc-pascal at telemetry.co.uk> wrote:

>> Is there some hidden property that makes it possible to increase thisvalue?
>>What is the maximum size one can set it too?
>
>Not that I was responsible for. Refer to the Windows API for any limits.

I might add that the problem manifests itself on transmission from the
Windows application (Delphi XE5).
I.e. the relaying Lazarus/FPC application loses incoming bytes after
some time of processing the streams. Small data sizes work just fine
but with the 1 Mbytes packet some kbytes are lost.


And here is the core procedure where the relaying happens:

procedure TfrmMain.RelayData;
{This procedure is responsible for transferring the data from one side
to the other.
It will run until one of the ports are closed as indicated by the
flags FTcpConnected
and FComOpen}
var
  Buf: TIdBytes;
  LenRd, BufLen: integer;
  RxSer, TxSer, RXTcp, TxTcp: integer;
begin
  RxSer := 0;
  TxSer := 0;
  RxTcp := 0;
  TxTcp := 0;
  BufLen := 50;
  FTcpComm.ReadTimeout := 30;
  while (FComOpen and FTcpConnected and FRelayOn) do
  begin
    //First check TCP data
    try
      SetLength(Buf, 0);
      if FTcpComm.IOHandler.CheckForDataOnSource(10) then
      begin
        FTcpComm.IOHandler.ReadBytes(Buf, -1, false);
        if Length(Buf) > 0 then
        begin
          Inc(RxTcp, Length(Buf));
          SerWrite(FComH, Buf[0], Length(Buf));
          Inc(TxSer, Length(Buf));
          LogHex('Rx', Buf);
          SetLength(Buf, 0);
        end;
      end;
    except
      on E: Exception do
         Showmessage('TCP read exception: '#13 + E.Message);
    end;

    //Next check serial data
    SetLength(Buf, BufLen);
    LenRd := SerRead(FComH, Buf[0], BufLen);
    if LenRd > 0 then
    begin
      SetLength(Buf, LenRd);
      Inc(RxSer, LenRd);
      try
        FTcpComm.IOHandler.Write(Buf, LenRd);
        Inc(TxTcp, LenRd);
        LogHex('Tx', Buf);
        SetLength(Buf, 0);
      except
        on E: Exception do
           Showmessage('TCP write exception: '#13 + E.Message);
      end;
    end;
    //Finally show data and check events
    stxSerRx.Caption := IntToStr(RxSer);
    stxSerTx.Caption := IntToStr(TxSer);
    stxTcpRx.Caption := IntToStr(RxTcp);
    stxTcpTx.Caption := IntToStr(TxTcp);
    Application.ProcessMessages;
  end;
end;

Where could I be losing incoming serial data?


-- 
Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list