From 800a790b896cc7296967f9e4d9fd5663dde1769a 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: Tue, 28 May 2019 08:56:06 +0300 Subject: [PATCH] feature: added changing ReadTimeout and WriteTimeout for connected tcpClient. --- S7.Net/PLC.cs | 38 +++++++++++++++++++++++++++++--------- S7.Net/PlcAsynchronous.cs | 2 +- S7.Net/PlcSynchronous.cs | 2 +- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index cd44a13..40e148f 100644 --- a/S7.Net/PLC.cs +++ b/S7.Net/PLC.cs @@ -15,6 +15,9 @@ namespace S7.Net private TcpClient tcpClient; private NetworkStream stream; + private int _readTimeout = System.Threading.Timeout.Infinite; + private int _writeTimeout = System.Threading.Timeout.Infinite; + /// /// IP address of the PLC /// @@ -44,17 +47,25 @@ namespace S7.Net /// 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; + get => _readTimeout; + set + { + _readTimeout = value; + ConfigureConnection(); + } + } /// 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; + get => _writeTimeout; + set + { + _writeTimeout = value; + ConfigureConnection(); + } + } /// /// Returns true if a connection to the PLC can be established @@ -122,6 +133,7 @@ namespace S7.Net Slot = slot; MaxPDUSize = 240; } + /// /// Close connection to PLC /// @@ -133,10 +145,18 @@ namespace S7.Net } } - private void ConfigureNetworkStream(NetworkStream networkStream) + private void ConfigureConnection() { - networkStream.ReadTimeout = ReadTimeout; - networkStream.WriteTimeout = WriteTimeout; + if (tcpClient != null) + { + ConfigureConnection(tcpClient); + } + } + + private void ConfigureConnection(TcpClient client) + { + client.ReceiveTimeout = ReadTimeout; + client.SendTimeout = WriteTimeout; } #region IDisposable Support diff --git a/S7.Net/PlcAsynchronous.cs b/S7.Net/PlcAsynchronous.cs index 60851cc..91edb82 100644 --- a/S7.Net/PlcAsynchronous.cs +++ b/S7.Net/PlcAsynchronous.cs @@ -44,9 +44,9 @@ namespace S7.Net private async Task ConnectAsync() { tcpClient = new TcpClient(); + ConfigureConnection(tcpClient); await tcpClient.ConnectAsync(IP, 102); stream = tcpClient.GetStream(); - ConfigureNetworkStream(stream); } /// diff --git a/S7.Net/PlcSynchronous.cs b/S7.Net/PlcSynchronous.cs index 556f490..ad7f737 100644 --- a/S7.Net/PlcSynchronous.cs +++ b/S7.Net/PlcSynchronous.cs @@ -50,9 +50,9 @@ namespace S7.Net try { tcpClient = new TcpClient(); + ConfigureConnection(tcpClient); tcpClient.Connect(IP, 102); stream = tcpClient.GetStream(); - ConfigureNetworkStream(stream); } catch (SocketException sex) {