mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 22:38:27 +08:00
S7.Net and S7.Net.Core unification (issue #56): moved files, updated csproj, minor modification for compatibility in SocketClient class.
Signed-off-by: Michele Cattafesta <michele.cattafesta@mesta-automation.com>
This commit is contained in:
@@ -12,7 +12,11 @@ namespace S7.Net
|
||||
{
|
||||
public class Plc : IDisposable
|
||||
{
|
||||
#if NETFX_CORE
|
||||
private SocketClient _mSocket; //TCP connection to device
|
||||
#else
|
||||
private Socket _mSocket; //TCP connection to device
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Ip address of the plc
|
||||
@@ -33,7 +37,7 @@ namespace S7.Net
|
||||
/// Slot of the CPU of the plc
|
||||
/// </summary>
|
||||
public Int16 Slot { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Pings the IP address and returns true if the result of the ping is Success.
|
||||
/// </summary>
|
||||
@@ -41,6 +45,9 @@ namespace S7.Net
|
||||
{
|
||||
get
|
||||
{
|
||||
#if NETFX_CORE
|
||||
return (!string.IsNullOrWhiteSpace(IP));
|
||||
#else
|
||||
using (Ping ping = new Ping())
|
||||
{
|
||||
PingReply result;
|
||||
@@ -54,9 +61,11 @@ namespace S7.Net
|
||||
}
|
||||
return result != null && result.Status == IPStatus.Success;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the socket is connected and polls the other peer (the plc) to see if it's connected.
|
||||
/// This is the variable that you should continously check to see if the communication is working
|
||||
@@ -70,8 +79,13 @@ namespace S7.Net
|
||||
{
|
||||
if (_mSocket == null)
|
||||
return false;
|
||||
|
||||
#if NETFX_CORE
|
||||
return _mSocket.Connected;
|
||||
#else
|
||||
return !((_mSocket.Poll(1000, SelectMode.SelectRead) && (_mSocket.Available == 0)) || !_mSocket.Connected);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
}
|
||||
@@ -128,14 +142,10 @@ namespace S7.Net
|
||||
return LastErrorCode;
|
||||
}
|
||||
|
||||
try {
|
||||
// open the channel
|
||||
_mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
|
||||
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
|
||||
|
||||
IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), 102);
|
||||
try
|
||||
{
|
||||
CreateSocket();
|
||||
IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), 102);
|
||||
_mSocket.Connect(server);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
@@ -324,7 +334,8 @@ namespace S7.Net
|
||||
|
||||
byte[] bReceive = new byte[512];
|
||||
int numReceived = _mSocket.Receive(bReceive, 512, SocketFlags.None);
|
||||
if (bReceive[21] != 0xff) throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
|
||||
if (bReceive[21] != 0xff)
|
||||
throw new Exception(ErrorCode.WrongNumberReceivedBytes.ToString());
|
||||
|
||||
for (int cnt = 0; cnt < count; cnt++)
|
||||
bytes[cnt] = bReceive[cnt + 25];
|
||||
@@ -1084,9 +1095,20 @@ namespace S7.Net
|
||||
return package;
|
||||
}
|
||||
|
||||
private void CreateSocket()
|
||||
{
|
||||
#if NETFX_CORE
|
||||
_mSocket = new SocketClient(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
_mSocket.SetReceiveTimeout(1000);
|
||||
_mSocket.SetSendTimeout(1000);
|
||||
#else
|
||||
_mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
|
||||
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#region IDisposable members
|
||||
#region IDisposable members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
@@ -1099,8 +1121,7 @@ namespace S7.Net
|
||||
}
|
||||
//((IDisposable)_mSocket).Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user