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

Bo Berglund bo.berglund at gmail.com
Fri Sep 8 10:34:14 CEST 2017


On Thu, 07 Sep 2017 13:48:27 +0200, Bo Berglund
<bo.berglund at gmail.com> wrote:

>I think I will have to create a bogus image file where I have a
>regular pattern of bytes so I can see when the hickups occur in the
>log. In a normal memory file most of the data is zero, so there are
>long stretches of zero bytes sent. Therefore it is not easy to see
>where the losses occur, I only see that the last part of the file
>(which contains some data) has been displaced to lower addresses.

I did so and discovered that loss of data started after some 48-50
Kbytes had been transferred. At that point a few bytes were missing.
But the losses got progressively worse as the transfer went on and in
the end there was about 50 K lost.

Then I tested a few things (like adding delays etc) but with no real
difference.

FINALLY FOUND A SOLUTION...
----------------------------
Finally I realized that the relayer was very busy during the transfer
since it was displaying the relayed data in a log window (a listbox) I
had put on the screen!

In the RelayData procedure there are two calls like this for the two
data directions:

  LogHex('Rx', Buf); //Incoming TCP/IP data
and
  LogHex('Tx', Buf); //Incoming RS232 data

The LogHex procedure does this:

procedure TfrmMain.LogHex(Prefix: AnsiString; var Buf: TIdBytes);
var
  i, l: integer;
  sLine,
  sTime: AnsiString;
begin
  sTime := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz', Now()) + '  ';
  l := Length(Buf);
  sLine := Prefix + ': ';
  for i := 0 to l-1 do
  begin
    sLine := sLine + IntToHex(Buf[i],2);
  end;
  lbxLog.Items.Add(sTime + ' ' + sLine);
  lbxLog.ItemIndex := lbxLog.Items.Count-1;
end;

Then I added a checkbox on the application form to switch on/off this
logging to the listbox.
I put this check as the first line in the LogHex function:

  if not ckbShowLogData.Checked then exit;

Next I unchecked the checkbox before doing the long transfer, and
lo-and-behold(!) now there are no losses anymore!

So the action of writing text to the listbox in the LogHex function
was actually causing the application to lose incoming serial data!

Serial buffer size?
-------------------
I thought that upping the serial buffers to 60 K would have helped but
apparently not. Maybe the change I did was not used after all?

What I did regarding buffer change:
1) Rightclicked SerSetParams() and selected "Find declaration"
2) This brought up the serial.pp unit at the declaration
3) Used Ctrl-Shft-downarrow to find the implementation
4) This had to be repeated for SerSetParamsPrivate()
5) In this function body the first line was this:
const   bufSize= 2048;

which I changed to:
const   bufSize= 61440;

6) Then I rebuilt my application, but I have no idea if this actually
caused any buffer change. I know too little about the inner workings
of the Lazarus/FPC system...

So it might still use the tiny 2K buffer???


-- 
Bo Berglund
Developer in Sweden




More information about the fpc-pascal mailing list