Using snap7 1.4.0 with Delphi XE7.
Playing around with S7-1200 plc and my test program.
I have problem with Client.Connected function.
Let's say I connect to plc and exchange data. At this point everything as expected Client.Connected returns 1.
Then I pull out cable out of plc and expect Client.Connected to return 0, buts it's not. It keeps returning 1.
What I'm missing here?
My code:
~~~~~~~~~~~~~~
Form1.Client.SetConnectionParams(RemoteAddress, LocalTSAP, RemoteTSAP);
while not Terminated do
begin
if Form1.Client.Connected then
begin
Form1.Client.DBRead(100, 0, 4, @real_val);
Form1.Client.DBRead(100, 4, 2, @word_val);
end
else //connect if not connected
begin
LastError :=Form1.Client.Connect;
if LastError = 0 then
//Connected
else
//CliErrorText(LastError);
end;
Sleep(5000);
end;
~~~~~~~~~~~~~~~~~~~
Edit:Sorry, wrong section.
Last edit: AngryBinary 2015-07-11
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Connected function is only a "reminder", there is no way in a client-server scheme to know if a cable was unplugged until a request is performed.
So, your thread code should looks like this:
TMyThread.execute;
Var
CliReturn : integer;
TCPError : boolean;
procedure CheckConnection;
begin
if TCPError then
begin
Client.Disconnect;
repeat
sleep(500);
until (Client.Connect=0) or terminated;
end;
end;
begin
while not Terminated do
begin
CheckConnection;
if not Terminated then
begin
CliReturn:=Client.DBRead(100, 0, 4, @real_val):
//
// other operations
//
TCPError:=(CliReturn and $0000FF)<>0;
end;
Sleep(5000);
end;
end;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Using snap7 1.4.0 with Delphi XE7.
Playing around with S7-1200 plc and my test program.
I have problem with Client.Connected function.
Let's say I connect to plc and exchange data. At this point everything as expected Client.Connected returns 1.
Then I pull out cable out of plc and expect Client.Connected to return 0, buts it's not. It keeps returning 1.
What I'm missing here?
My code:
~~~~~~~~~~~~~~
Form1.Client.SetConnectionParams(RemoteAddress, LocalTSAP, RemoteTSAP);
while not Terminated do
begin
if Form1.Client.Connected then
begin
Form1.Client.DBRead(100, 0, 4, @real_val);
Form1.Client.DBRead(100, 4, 2, @word_val);
end
else //connect if not connected
begin
LastError :=Form1.Client.Connect;
if LastError = 0 then
//Connected
else
//CliErrorText(LastError);
end;
Sleep(5000);
end;
~~~~~~~~~~~~~~~~~~~
Edit:Sorry, wrong section.
Last edit: AngryBinary 2015-07-11
Connected function is only a "reminder", there is no way in a client-server scheme to know if a cable was unplugged until a request is performed.
So, your thread code should looks like this:
TMyThread.execute; Var CliReturn : integer; TCPError : boolean; procedure CheckConnection; begin if TCPError then begin Client.Disconnect; repeat sleep(500); until (Client.Connect=0) or terminated; end; end; begin while not Terminated do begin CheckConnection; if not Terminated then begin CliReturn:=Client.DBRead(100, 0, 4, @real_val): // // other operations // TCPError:=(CliReturn and $0000FF)<>0; end; Sleep(5000); end; end;Thanks for reply, works like charm:)
Sorry, the re was a mistake:
TCPError:=(CliReturn AND $0000FFFF)<>0