添加PLC断线重连机制
This commit is contained in:
@@ -14,6 +14,8 @@ namespace DOAN.Infrastructure.PLC
|
||||
// 私有连接对象
|
||||
private SiemensS7Net siemensTcpNet = null;
|
||||
private readonly string plcAddress;
|
||||
private const int ReconnectDelayMs = 5000;
|
||||
|
||||
public PLCTool()
|
||||
{
|
||||
plcAddress = AppSettings.GetConfig("PLCConfig:Address");
|
||||
@@ -34,21 +36,77 @@ namespace DOAN.Infrastructure.PLC
|
||||
var connect = siemensTcpNet.ConnectServer();
|
||||
if (connect.IsSuccess)
|
||||
{
|
||||
Console.WriteLine($"PLC连接成功,地址{plcAddress}");
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC连接成功,地址{plcAddress}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"PLC连接失败,地址{plcAddress}: {connect.Message}");
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC连接失败,地址{plcAddress}: {connect.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"PLC连接失败,地址{plcAddress}: {e.Message}");
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC连接失败,地址{plcAddress}: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查PLC连接状态
|
||||
/// </summary>
|
||||
/// <returns>连接正常返回true,否则返回false</returns>
|
||||
public bool IsConnected()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 尝试读取一个字节来检查连接状态
|
||||
var result = siemensTcpNet.Read("VB100", 1);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC连接状态检查失败: {result.Message}");
|
||||
}
|
||||
return result.IsSuccess;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC连接状态检查异常: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重连PLC
|
||||
/// </summary>
|
||||
/// <returns>重连成功返回true,失败返回false</returns>
|
||||
public bool ReconnectPLC()
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] 尝试重新连接PLC...");
|
||||
|
||||
// 先关闭现有连接
|
||||
ConnectClose();
|
||||
|
||||
// 重新创建PLC对象
|
||||
this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, plcAddress)
|
||||
{
|
||||
ConnectTimeOut = 5000
|
||||
};
|
||||
|
||||
// 持续尝试重新连接,直到成功
|
||||
int attempt = 0;
|
||||
while (true)
|
||||
{
|
||||
attempt++;
|
||||
if (ConnectPLC())
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC重连成功,尝试次数: {attempt}");
|
||||
return true;
|
||||
}
|
||||
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC重连失败,尝试次数: {attempt}");
|
||||
System.Threading.Thread.Sleep(ReconnectDelayMs);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向PLC写入单个bit值
|
||||
@@ -87,7 +145,7 @@ namespace DOAN.Infrastructure.PLC
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"PLC IO 取值失败,PLC地址{plcAddress},访问地址为{addr},地址个数为{length}");
|
||||
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] PLC IO 取值失败,PLC地址{plcAddress},访问地址为{addr},地址个数为{length}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user