diff --git a/S7.Net/PLC.cs b/S7.Net/PLC.cs index 284b81b..853c4ed 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 /// @@ -39,6 +42,30 @@ 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 => readTimeout; + set + { + readTimeout = value; + if (tcpClient != null) tcpClient.ReceiveTimeout = readTimeout; + } + } + + /// 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 => writeTimeout; + set + { + writeTimeout = value; + if (tcpClient != null) tcpClient.SendTimeout = writeTimeout; + } + } /// /// Returns true if a connection to the PLC can be established @@ -106,6 +133,7 @@ namespace S7.Net Slot = slot; MaxPDUSize = 240; } + /// /// Close connection to PLC /// @@ -117,6 +145,17 @@ namespace S7.Net } } + private void ConfigureConnection() + { + if (tcpClient == null) + { + return; + } + + tcpClient.ReceiveTimeout = ReadTimeout; + tcpClient.SendTimeout = 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..291a938 100644 --- a/S7.Net/PlcAsynchronous.cs +++ b/S7.Net/PlcAsynchronous.cs @@ -44,6 +44,7 @@ namespace S7.Net private async Task ConnectAsync() { tcpClient = new TcpClient(); + ConfigureConnection(); await tcpClient.ConnectAsync(IP, 102); stream = tcpClient.GetStream(); } diff --git a/S7.Net/PlcSynchronous.cs b/S7.Net/PlcSynchronous.cs index bdc40bf..95d1baa 100644 --- a/S7.Net/PlcSynchronous.cs +++ b/S7.Net/PlcSynchronous.cs @@ -50,6 +50,7 @@ namespace S7.Net try { tcpClient = new TcpClient(); + ConfigureConnection(); tcpClient.Connect(IP, 102); stream = tcpClient.GetStream(); }