Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading returned data via TIdTcpClient?
#1
I am always struggling with this whenever I try to throw together a TCP client application...
I can easily send commands to the server (in this case a WiFi IoT module.
I know it responds to the command but for the life of me I cannot get the reply.
The platform I use is 64 bit FreePascal 3.0.4 + Lazarus 1.8.0 with the IndyLaz package installed


THe function that fails to work as I hoped looks like this (FComm is a TIdTcpClient object):


Code:
function TConfigCommHandler.GetItem(Cmd: TIdBytes; var Data: TIdBytes): boolean;
begin
  Result := false;
  SetLength(Cmd, Length(Cmd) + 1);
  Cmd[Length(Cmd)-1] := ETX;
  FComm.IOHandler.ReadTimeout := 2000;
  try
    FComm.IOHandler.Write(Cmd);
    SetLength(Data, 0);
    FComm.IOHandler.ReadBytes(Data, -1, false); //<== Is this way correct?
    Result := Length(Data) > 0;
    if Result then
    begin
      if Data[Length(Data)-1] <> ETX then //Received packet should end in ETX...
      begin
        FLastError := 'Terminating ETX missing!';
        Result := false;
      end
      else
        SetLength(Data, Length(Data) -1); //Remove trailing ETX
    end
    else
      FLastError := 'No data received within timeout!';
  except
    on E: Exception do
    begin
      Result := false;
      FLastError := 'Exception: ' + E.Message;
      Exit;
    end;
  end;
end;
For example what happens if this ReadBytes function does not get all of the incoming bytes?
I try to detect it via the ETX check but since the data is only at maximum some 25-40 bytes (not possible to know beforehand) I thought it highly unlikely it would be split between packets...

I found the cause regarding this specific problem:
The server did not add the trailing ETX to replies, so all of them were thrown away.
I modified the server code to always add the ETX to all outgoing messages.
But I have a follow-up question:
How can the PC application be made such that it will catch and display all incoming data without first sending a command?
For example spontaneous messages from the device are also interesting...
Reply


Messages In This Thread
Reading returned data via TIdTcpClient? - by BosseB - 03-15-2018, 06:53 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)