Skip to content

Commit

Permalink
feature: added changing ReadTimeout and WriteTimeout for connected tc…
Browse files Browse the repository at this point in the history
…pClient.
  • Loading branch information
ChipsetSV committed May 28, 2019
1 parent 0dbe401 commit 800a790
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
38 changes: 29 additions & 9 deletions S7.Net/PLC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public partial class Plc : IDisposable
private TcpClient tcpClient;
private NetworkStream stream;

private int _readTimeout = System.Threading.Timeout.Infinite;
private int _writeTimeout = System.Threading.Timeout.Infinite;

/// <summary>
/// IP address of the PLC
/// </summary>
Expand Down Expand Up @@ -44,17 +47,25 @@ public partial class Plc : IDisposable
/// <returns>A <see cref="T:System.Int32" /> that specifies the amount of time, in milliseconds, that will elapse before a read operation fails. The default value, <see cref="F:System.Threading.Timeout.Infinite" />, specifies that the read operation does not time out.</returns>
public int ReadTimeout
{
get;
set;
} = System.Threading.Timeout.Infinite;
get => _readTimeout;
set
{
_readTimeout = value;
ConfigureConnection();
}
}

/// <summary>Gets or sets the amount of time that a write operation blocks waiting for data to PLC. </summary>
/// <returns>A <see cref="T:System.Int32" /> that specifies the amount of time, in milliseconds, that will elapse before a write operation fails. The default value, <see cref="F:System.Threading.Timeout.Infinite" />, specifies that the write operation does not time out.</returns>
public int WriteTimeout
{
get;
set;
} = System.Threading.Timeout.Infinite;
get => _writeTimeout;
set
{
_writeTimeout = value;
ConfigureConnection();
}
}

/// <summary>
/// Returns true if a connection to the PLC can be established
Expand Down Expand Up @@ -122,6 +133,7 @@ public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)
Slot = slot;
MaxPDUSize = 240;
}

/// <summary>
/// Close connection to PLC
/// </summary>
Expand All @@ -133,10 +145,18 @@ public void Close()
}
}

private void ConfigureNetworkStream(NetworkStream networkStream)
private void ConfigureConnection()
{
if (tcpClient != null)
{
ConfigureConnection(tcpClient);
}
}

private void ConfigureConnection(TcpClient client)
{
networkStream.ReadTimeout = ReadTimeout;
networkStream.WriteTimeout = WriteTimeout;
client.ReceiveTimeout = ReadTimeout;
client.SendTimeout = WriteTimeout;
}

#region IDisposable Support
Expand Down
2 changes: 1 addition & 1 deletion S7.Net/PlcAsynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public async Task OpenAsync()
private async Task ConnectAsync()
{
tcpClient = new TcpClient();
ConfigureConnection(tcpClient);
await tcpClient.ConnectAsync(IP, 102);
stream = tcpClient.GetStream();
ConfigureNetworkStream(stream);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion S7.Net/PlcSynchronous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ private void Connect()
try
{
tcpClient = new TcpClient();
ConfigureConnection(tcpClient);
tcpClient.Connect(IP, 102);
stream = tcpClient.GetStream();
ConfigureNetworkStream(stream);
}
catch (SocketException sex)
{
Expand Down

0 comments on commit 800a790

Please sign in to comment.