Skip to content

Commit a9b520a

Browse files
Allow ForwardClose and UnregisterSession to close the Socket if connection was already closed by Remote Device
1 parent f3b98a1 commit a9b520a

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

AllenBradleyPointIO/Program.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void Main(string[] args)
5050

5151
while(true)
5252
{
53-
53+
5454
//Read the Inputs Transfered form Target -> Originator
5555
Console.WriteLine("State of first Input byte: " + eeipClient.T_O_IOData[8]);
5656
Console.WriteLine("State of second Input byte: " + eeipClient.T_O_IOData[9]);
@@ -62,6 +62,24 @@ static void Main(string[] args)
6262
eeipClient.O_T_IOData[3] = 8;
6363

6464
System.Threading.Thread.Sleep(500);
65+
66+
//Detect Timeout (Read last Received Message Property)
67+
if (DateTime.Now.Ticks > eeipClient.LastReceivedImplicitMessage.Ticks + (1000 * 10000))
68+
{
69+
try
70+
{
71+
eeipClient.ForwardClose();
72+
eeipClient.UnRegisterSession();
73+
74+
eeipClient.RegisterSession();
75+
eeipClient.ForwardOpen();
76+
}
77+
catch (Exception)
78+
{
79+
Console.WriteLine("Couldn't reconnect to Point I/O");
80+
}
81+
}
82+
6583
}
6684

6785
//Close the Session

EEIP.NET/EIPClient.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,14 @@ public void UnRegisterSession()
255255
encapsulation.Length = 0;
256256
encapsulation.SessionHandle = sessionHandle;
257257

258-
stream.Write(encapsulation.toBytes(), 0, encapsulation.toBytes().Length);
258+
try
259+
{
260+
stream.Write(encapsulation.toBytes(), 0, encapsulation.toBytes().Length);
261+
}
262+
catch (Exception)
263+
{
264+
//Handle Exception to allow to Close the Stream if the connection was closed by Remote Device
265+
}
259266
byte[] data = new Byte[256];
260267
client.Close();
261268
stream.Close();
@@ -727,11 +734,25 @@ public void ForwardClose()
727734
System.Buffer.BlockCopy(encapsulation.toBytes(), 0, dataToWrite, 0, encapsulation.toBytes().Length);
728735
System.Buffer.BlockCopy(commonPacketFormat.toBytes(), 0, dataToWrite, encapsulation.toBytes().Length, commonPacketFormat.toBytes().Length);
729736
encapsulation.toBytes();
730-
731-
stream.Write(dataToWrite, 0, dataToWrite.Length);
737+
try
738+
{
739+
stream.Write(dataToWrite, 0, dataToWrite.Length);
740+
}
741+
catch (Exception e)
742+
{
743+
//Handle Exception to allow Forward close if the connection was closed by the Remote Device before
744+
}
732745
byte[] data = new Byte[564];
733746

734-
Int32 bytes = stream.Read(data, 0, data.Length);
747+
try
748+
{
749+
Int32 bytes = stream.Read(data, 0, data.Length);
750+
}
751+
catch (Exception e)
752+
{
753+
//Handle Exception to allow Forward close if the connection was closed by the Remote Device before
754+
}
755+
735756

736757
//--------------------------BEGIN Error?
737758
if (data[42] != 0) //Exception codes see "Table B-1.1 CIP General Status Codes"

0 commit comments

Comments
 (0)