mirror of
https://github.com/S7NetPlus/s7netplus.git
synced 2026-02-17 14:28:25 +08:00
@@ -736,6 +736,27 @@ namespace S7.Net.UnitTest
|
|||||||
Assert.AreEqual(tc.CustomTypes[1].Bools[1], tc2.CustomTypes[1].Bools[1]);
|
Assert.AreEqual(tc.CustomTypes[1].Bools[1], tc2.CustomTypes[1].Bools[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void T24_IsAvailableReturnsFalseIfIPAddressIsNotReachable()
|
||||||
|
{
|
||||||
|
plc.Close();
|
||||||
|
S7TestServer.Stop();
|
||||||
|
|
||||||
|
var unreachablePlc = new Plc(CpuType.S7300, "255.255.255.255", 0, 2);
|
||||||
|
Assert.IsFalse(unreachablePlc.IsAvailable);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void T25_IsAvailableReturnsTrueIfIPAddressIsReachable()
|
||||||
|
{
|
||||||
|
plc.Close();
|
||||||
|
S7TestServer.Stop();
|
||||||
|
S7TestServer.Start();
|
||||||
|
|
||||||
|
var reachablePlc = new Plc(CpuType.S7300, "127.0.0.1", 0, 2);
|
||||||
|
Assert.IsTrue(reachablePlc.IsAvailable);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Private methods
|
#region Private methods
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ namespace S7.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class Plc : IDisposable
|
public class Plc : IDisposable
|
||||||
{
|
{
|
||||||
|
private const int CONNECTION_TIMED_OUT_ERROR_CODE = 10060;
|
||||||
|
|
||||||
private Socket _mSocket; //TCP connection to device
|
private Socket _mSocket; //TCP connection to device
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -39,27 +41,19 @@ namespace S7.Net
|
|||||||
public Int16 Slot { get; private set; }
|
public Int16 Slot { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Pings the IP address and returns true if the result of the ping is Success.
|
/// Returns true if a connection to the plc can be established
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool IsAvailable
|
public bool IsAvailable
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
|
|
||||||
#if NETFX_CORE
|
#if NETFX_CORE
|
||||||
return (!string.IsNullOrWhiteSpace(IP));
|
return (!string.IsNullOrWhiteSpace(IP));
|
||||||
#else
|
#else
|
||||||
using (Ping ping = new Ping())
|
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
|
||||||
{
|
{
|
||||||
PingReply result;
|
return Connect(socket) == ErrorCode.NoError;
|
||||||
try
|
|
||||||
{
|
|
||||||
result = ping.Send(IP);
|
|
||||||
}
|
|
||||||
catch (PingException)
|
|
||||||
{
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
return result != null && result.Status == IPStatus.Success;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -119,6 +113,38 @@ namespace S7.Net
|
|||||||
Slot = slot;
|
Slot = slot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ErrorCode Connect(Socket socket)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), 102);
|
||||||
|
socket.Connect(server);
|
||||||
|
|
||||||
|
return ErrorCode.NoError;
|
||||||
|
}
|
||||||
|
catch (SocketException sex)
|
||||||
|
{
|
||||||
|
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms740668(v=vs.85).aspx
|
||||||
|
if (sex.ErrorCode == CONNECTION_TIMED_OUT_ERROR_CODE)
|
||||||
|
{
|
||||||
|
LastErrorCode = ErrorCode.IPAddressNotAvailable;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LastErrorCode = ErrorCode.ConnectionError;
|
||||||
|
}
|
||||||
|
|
||||||
|
LastErrorString = sex.Message;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LastErrorCode = ErrorCode.ConnectionError;
|
||||||
|
LastErrorString = ex.Message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return LastErrorCode;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Open a socket and connects to the plc, sending all the corrected package and returning if the connection was successful (ErroreCode.NoError) of it was wrong.
|
/// Open a socket and connects to the plc, sending all the corrected package and returning if the connection was successful (ErroreCode.NoError) of it was wrong.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -127,33 +153,13 @@ namespace S7.Net
|
|||||||
{
|
{
|
||||||
byte[] bReceive = new byte[256];
|
byte[] bReceive = new byte[256];
|
||||||
|
|
||||||
try
|
_mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
{
|
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
|
||||||
// check if available
|
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
|
||||||
if (!IsAvailable)
|
|
||||||
{
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
LastErrorCode = ErrorCode.IPAddressNotAvailable;
|
|
||||||
LastErrorString = string.Format("Destination IP-Address '{0}' is not available!", IP);
|
|
||||||
return LastErrorCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
if (Connect(_mSocket) != ErrorCode.NoError)
|
||||||
{
|
{
|
||||||
_mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
return LastErrorCode;
|
||||||
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
|
|
||||||
_mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
|
|
||||||
IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), 102);
|
|
||||||
_mSocket.Connect(server);
|
|
||||||
}
|
|
||||||
catch (Exception ex) {
|
|
||||||
LastErrorCode = ErrorCode.ConnectionError;
|
|
||||||
LastErrorString = ex.Message;
|
|
||||||
return ErrorCode.ConnectionError;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user