From 0dbe401ce9a1d87dbc03824e59645e2cd27cab46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BC=D0=B8=D1=80=D0=BD=D0=BE=D0=B2=20=D0=92=D0=B8?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=D0=B8=D0=B9?= Date: Mon, 27 May 2019 13:52:38 +0300 Subject: [PATCH] feature: added ReadTimeout and WriteTimeout to PLC class for NetworkStream. --- S7.Net/PLC.cs | 22 ++++++++++++++++++++++ S7.Net/PlcAsynchronous.cs | 1 + S7.Net/PlcSynchronous.cs | 1 + 3 files changed, 24 insertions(+) diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 284b81b..cd44a13 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -39,6 +39,22 @@ namespace S7.Net /// Max PDU size this cpu supports /// public Int16 MaxPDUSize { get; set; } + + /// Gets or sets the amount of time that a read operation blocks waiting for data from PLC. + /// A that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, , specifies that the read operation does not time out. + public int ReadTimeout + { + get; + set; + } = System.Threading.Timeout.Infinite; + + /// Gets or sets the amount of time that a write operation blocks waiting for data to PLC. + /// A that specifies the amount of time, in milliseconds, that will elapse before a write operation fails. The default value, , specifies that the write operation does not time out. + public int WriteTimeout + { + get; + set; + } = System.Threading.Timeout.Infinite; /// /// Returns true if a connection to the PLC can be established @@ -117,6 +133,12 @@ namespace S7.Net } } + private void ConfigureNetworkStream(NetworkStream networkStream) + { + networkStream.ReadTimeout = ReadTimeout; + networkStream.WriteTimeout = WriteTimeout; + } + #region IDisposable Support private bool disposedValue = false; // To detect redundant calls diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs index fe3c552..60851cc 100644 --- a/S7.Net/PlcAsynchronous.cs +++ b/S7.Net/PlcAsynchronous.cs @@ -46,6 +46,7 @@ namespace S7.Net tcpClient = new TcpClient(); await tcpClient.ConnectAsync(IP, 102); stream = tcpClient.GetStream(); + ConfigureNetworkStream(stream); } /// diff --git a/S7.Net/PlcSynchronous.cs b/S7.Net/PlcSynchronous.cs index bdc40bf..556f490 100644 --- a/S7.Net/PlcSynchronous.cs +++ b/S7.Net/PlcSynchronous.cs @@ -52,6 +52,7 @@ namespace S7.Net tcpClient = new TcpClient(); tcpClient.Connect(IP, 102); stream = tcpClient.GetStream(); + ConfigureNetworkStream(stream); } catch (SocketException sex) {