55using System . Net . Sockets ;
66using S7 . Net . Internal ;
77using S7 . Net . Protocol ;
8+ using S7 . Net . Tcp ;
89using S7 . Net . Types ;
910
1011
@@ -27,8 +28,11 @@ public partial class Plc : IDisposable
2728
2829 private readonly TaskQueue queue = new TaskQueue ( ) ;
2930
31+ private readonly ITcpClientFactory _tcpClientFactory ;
32+
3033 //TCP connection to device
31- private TcpClient ? tcpClient ;
34+ private ITcpClient ? tcpClient ;
35+
3236 private NetworkStream ? _stream ;
3337
3438 private int readTimeout = DefaultTimeout ; // default no timeout
@@ -124,8 +128,9 @@ public int WriteTimeout
124128 /// <param name="rack">rack of the PLC, usually it's 0, but check in the hardware configuration of Step7 or TIA portal</param>
125129 /// <param name="slot">slot of the CPU of the PLC, usually it's 2 for S7300-S7400, 0 for S7-1200 and S7-1500.
126130 /// If you use an external ethernet card, this must be set accordingly.</param>
127- public Plc ( CpuType cpu , string ip , Int16 rack , Int16 slot )
128- : this ( cpu , ip , DefaultPort , rack , slot )
131+ /// <param name="tcpClientFactory">Factory to provide the underlying <see cref="ITcpClient"/> for network communication.</param>
132+ public Plc ( CpuType cpu , string ip , Int16 rack , Int16 slot , ITcpClientFactory ? tcpClientFactory = null )
133+ : this ( cpu , ip , DefaultPort , rack , slot , tcpClientFactory )
129134 {
130135 }
131136
@@ -141,8 +146,9 @@ public Plc(CpuType cpu, string ip, Int16 rack, Int16 slot)
141146 /// <param name="rack">rack of the PLC, usually it's 0, but check in the hardware configuration of Step7 or TIA portal</param>
142147 /// <param name="slot">slot of the CPU of the PLC, usually it's 2 for S7300-S7400, 0 for S7-1200 and S7-1500.
143148 /// If you use an external ethernet card, this must be set accordingly.</param>
144- public Plc ( CpuType cpu , string ip , int port , Int16 rack , Int16 slot )
145- : this ( ip , port , TsapPair . GetDefaultTsapPair ( cpu , rack , slot ) )
149+ /// <param name="tcpClientFactory">Factory to provide the underlying <see cref="ITcpClient"/> for network communication.</param>
150+ public Plc ( CpuType cpu , string ip , int port , Int16 rack , Int16 slot , ITcpClientFactory ? tcpClientFactory = null )
151+ : this ( ip , port , TsapPair . GetDefaultTsapPair ( cpu , rack , slot ) , tcpClientFactory )
146152 {
147153 if ( ! Enum . IsDefined ( typeof ( CpuType ) , cpu ) )
148154 throw new ArgumentException (
@@ -162,7 +168,9 @@ public Plc(CpuType cpu, string ip, int port, Int16 rack, Int16 slot)
162168 /// </summary>
163169 /// <param name="ip">Ip address of the PLC</param>
164170 /// <param name="tsapPair">The TSAP addresses used for the connection request.</param>
165- public Plc ( string ip , TsapPair tsapPair ) : this ( ip , DefaultPort , tsapPair )
171+ /// <param name="tcpClientFactory">Factory to provide the underlying <see cref="ITcpClient"/> for network communication.</param>
172+ public Plc ( string ip , TsapPair tsapPair , ITcpClientFactory ? tcpClientFactory = null )
173+ : this ( ip , DefaultPort , tsapPair , tcpClientFactory )
166174 {
167175 }
168176
@@ -173,7 +181,8 @@ public Plc(string ip, TsapPair tsapPair) : this(ip, DefaultPort, tsapPair)
173181 /// <param name="ip">Ip address of the PLC</param>
174182 /// <param name="port">Port number used for the connection, default 102.</param>
175183 /// <param name="tsapPair">The TSAP addresses used for the connection request.</param>
176- public Plc ( string ip , int port , TsapPair tsapPair )
184+ /// <param name="tcpClientFactory">Factory to provide the underlying <see cref="ITcpClient"/> for network communication.</param>
185+ public Plc ( string ip , int port , TsapPair tsapPair , ITcpClientFactory ? tcpClientFactory = null )
177186 {
178187 if ( string . IsNullOrEmpty ( ip ) )
179188 throw new ArgumentException ( "IP address must valid." , nameof ( ip ) ) ;
@@ -182,6 +191,7 @@ public Plc(string ip, int port, TsapPair tsapPair)
182191 Port = port ;
183192 MaxPDUSize = 240 ;
184193 TsapPair = tsapPair ;
194+ _tcpClientFactory = tcpClientFactory ?? new TcpClientWrapperFactory ( ) ;
185195 }
186196
187197 /// <summary>
0 commit comments