Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending Byte List
#4
(08-23-2018, 05:52 AM)krshin Wrote: I have only one problem when I send this array of bytes everything goes fine at the client hardware side, and I need to get some response from that clients over an open channel of communication, so how to get and read this response in this case?

If you know a response will arrive quickly (and you are not delegating the send to the OnExecute event, like I described), then you could simply read the response immediately in your btnSendClick() loop using the same IOHandler that you use to send the command, eg:

Code:
procedure TFServer.btnSendClick(Sender: TObject);
var
  bytes: TIdBytes;
  tmpList: TList;
  contexClient: TIdContext;
  i: Integer;
begin
  SetLength(bytes, 3);
  bytes[0] := $ea;
  bytes[1] := $89;
  bytes[2] := $ef;

  tmpList := IdTCPServer.Contexts.LockList;
  try
    for i := 0 to tmpList.Count-1 do
    begin
      contexClient := TIdContext(tmpList[i]);
      if contexClient <> nil then
      try
        contexClient.Connection.IOHandler.Write(bytes);
        // read response here...
      except
        contexClient.Connection.Disconnect;
      end;
    end;
  finally
    IdTCPServer.Contexts.UnlockList;
  end;
end;

However, it is generally best NOT to wait for responses in loops like that, especially loops running in the UI. You should instead read the response in the OnExecute event (especially if you are delegating sends to OnExecute).

Now, HOW you read a response from a client's IOHandler depends on the particular format of the response. Since you have not provided any details about the format of the communication protocol you are using, I can't help you with that particular coding. But TIdIOHandler has MANY reading methods available, for things like strings, integers, streams, byte arrays, etc.

Reply


Messages In This Thread
Sending Byte List - by krshin - 08-22-2018, 07:19 PM
RE: Sending Byte List - by rlebeau - 08-22-2018, 08:27 PM
RE: Sending Byte List - by krshin - 08-23-2018, 05:52 AM
RE: Sending Byte List - by rlebeau - 08-23-2018, 10:32 PM
RE: Sending Byte List - by CaptRick - 08-29-2018, 11:43 AM
RE: Sending Byte List - by rlebeau - 08-29-2018, 06:22 PM
RE: Sending Byte List - by CaptRick - 08-29-2018, 10:50 PM
RE: Sending Byte List - by CaptRick - 08-31-2018, 10:38 AM
RE: Sending Byte List - by rlebeau - 08-31-2018, 05:10 PM
RE: Sending Byte List - by CaptRick - 08-31-2018, 09:02 PM
RE: Sending Byte List - by rlebeau - 08-31-2018, 09:35 PM
RE: Sending Byte List - by CaptRick - 08-31-2018, 09:46 PM
RE: Sending Byte List - by CaptRick - 09-01-2018, 12:46 PM
RE: Sending Byte List - by CaptRick - 09-04-2018, 03:35 PM
RE: Sending Byte List - by rlebeau - 09-04-2018, 06:32 PM
RE: Sending Byte List - by kudzu - 09-05-2018, 02:28 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)