I have several programs made using Delphi 6 with Indy components and am trying to figure out a way to put a TCP connection on hold in case of an unforeseen disconnect.
We connect via TCP to PLC's on our factory floor and for whatever reason (operator messing with things they shouldn't be, switches going down temporarily, faulty wire, or whatever the case may be) the connection is ended.
This causes our programs to lock up hard. As soon as a disconnect happens between PLC and PC, the program will linger for a short amount of time and finally lock up.
Is there a way to put the connection on hold when a disconnect between PLC and PC happens, and reestablish connection without locking the program up?
Perhaps also is there a way to do this with TIdTCPClient
as well?
If I need to be more descriptive please say so and I will try to be.
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
i : Integer;
begin
begin
if NOT IdTCPServer1.Active then exit;
try
Comm_Status := True;
{************************************************
Clear PC Buffer before Read
************************************************}
for i := 0 to giBufferSize -1 do begin
gabToPCBuffer[i] := 0;
end;
{************************************************
Recieve the buffer from PLC
************************************************}
// WE READ
AThread.Connection.ReadBuffer(gabToPCBuffer, giBufferSize);
StateMachineLogic; //Sort Through buffer and copy to server buffer
// WE SEND back to PLC
AThread.Connection.WriteBuffer(gabToPLCBuffer, giBufferToPLC );
except
//;
end;
end;
end;
If a client connection to a TCP server is gracefully disconnected from the client, the OnDisconnect event will be triggered. Your thread will also be marked terminated, so you should check for this in your loop.
However, for non-graceful disconnections, the TCP server will immediately error on the next read or write. You have wrapped your read and write in an empty
try...except
block. Instead, you should handle the error and break out of the loop.For Delphi 6, you should update your Indy components to the latest Indy v9 release.