[Lazarus] Usage of Serial in a testing program?

Bo Berglund bo.berglund at gmail.com
Sun Sep 9 17:58:35 CEST 2018


On Sun, 09 Sep 2018 16:26:29 +0200, Bo Berglund via Lazarus
<lazarus at lists.lazarus-ide.org> wrote:

>On Sun, 09 Sep 2018 13:33:22 +0200, Bo Berglund via Lazarus
><lazarus at lists.lazarus-ide.org> wrote:
>
>>3) Receiving data
>>----------------
>I forgot to add a question about read timeouts...
>
>4) Read timeout
>---------------
>When running this to read incoming data:
>
>status:= SerRead(serialhandle, s[1], 10);
>
>How can I get out of the call if there never arrives 10 bytes? Is
>there a timeout somewhere so that I can set it to some small value and
>put the SerRead into a thread that can check arriving data and if
>found fire off an event or such?

Ok, I went to the sources and found this:

interface
...
{ Reads a maximum of "Count" bytes of data into the specified buffer.
  Result: Number of bytes read. }
function SerRead(Handle: TSerialHandle; var Buffer; Count: LongInt):
LongInt;

implementation
...
function SerRead(Handle: TSerialHandle; var Buffer; Count: LongInt):
LongInt;
var
  BytesRead: DWORD;
  Timeouts: TCommTimeouts;
begin
  if GetCommTimeouts(Handle, Timeouts) then 
  begin
    Timeouts.ReadIntervalTimeout := MAXDWORD;
    Timeouts.ReadTotalTimeoutConstant := 0;
    SetCommTimeouts(Handle, Timeouts)
  end;
  if not ReadFile(Handle, Buffer, Count, BytesRead, nil) then
    result := 0
  else
    result := BytesRead
end { SerRead } ;

So SerRead seems to read *up to* Count bytes, but does it do this by
checking what is currently stored in the operating system inbound
buffer or does it try to wait until Count bytes are available?

If so how is the timeout that is needed in such a case changed?
Seems to be set at infinity in the SerRead function (MAXDWORD)....
Or is it ReadTotalTimeoutConstant that is used during read?

If you drill further down you will get into ReadFile, but that is a
call into Windows API...


-- 
Bo Berglund
Developer in Sweden



More information about the Lazarus mailing list