读取完毕
This commit is contained in:
parent
55545b3193
commit
f445328ba9
@ -49,15 +49,13 @@ namespace RIZO.Infrastructure.Helper
|
|||||||
while (attempt < maxRetries)
|
while (attempt < maxRetries)
|
||||||
{
|
{
|
||||||
attempt++;
|
attempt++;
|
||||||
try
|
|
||||||
|
var result = await Task.Run(() => _plc.ConnectServer());
|
||||||
|
_isConnected = result.IsSuccess;
|
||||||
|
if (!_isConnected)
|
||||||
{
|
{
|
||||||
var result = await Task.Run(() => _plc.ConnectServer());
|
|
||||||
_isConnected = result.IsSuccess;
|
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 失败");
|
||||||
return _isConnected;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 失败: {ex.Message}");
|
|
||||||
|
|
||||||
if (attempt < maxRetries)
|
if (attempt < maxRetries)
|
||||||
{
|
{
|
||||||
@ -69,6 +67,7 @@ namespace RIZO.Infrastructure.Helper
|
|||||||
Console.WriteLine("无法建立PLC连接,请检查网络和设备状态");
|
Console.WriteLine("无法建立PLC连接,请检查网络和设备状态");
|
||||||
_plc?.ConnectClose();
|
_plc?.ConnectClose();
|
||||||
_plc = null;
|
_plc = null;
|
||||||
|
return _isConnected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
|
<PackageReference Include="AspectCore.Abstractions" Version="2.4.0" />
|
||||||
<PackageReference Include="HslCommunication" Version="12.6.2" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
|
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="S7netplus" Version="0.20.0" />
|
<PackageReference Include="S7netplus" Version="0.20.0" />
|
||||||
|
|||||||
@ -96,9 +96,9 @@ builder.Services.AddSwaggerConfig();
|
|||||||
builder.Services.AddLogo();
|
builder.Services.AddLogo();
|
||||||
// 添加本地化服务
|
// 添加本地化服务
|
||||||
builder.Services.AddLocalization(options => options.ResourcesPath = "");
|
builder.Services.AddLocalization(options => options.ResourcesPath = "");
|
||||||
|
|
||||||
builder.Services.AddHostedService<MES_PLC_InterationTask>();
|
|
||||||
builder.Services.AddHslCommunication();
|
builder.Services.AddHslCommunication();
|
||||||
|
builder.Services.AddHostedService<MES_PLC_InterationTask>();
|
||||||
|
|
||||||
|
|
||||||
// 在应用程序启动的最开始处调用
|
// 在应用程序启动的最开始处调用
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|||||||
@ -39,21 +39,18 @@ namespace RIZO.Service.PLCbackTask
|
|||||||
_internalCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
_internalCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
||||||
using (s7Helper = new SiemensS7Helper(_plcAddress.IP))
|
using (s7Helper = new SiemensS7Helper(_plcAddress.IP))
|
||||||
{
|
{
|
||||||
|
try
|
||||||
while (!stoppingToken.IsCancellationRequested && !_internalCts.Token.IsCancellationRequested)
|
|
||||||
|
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
|
|
||||||
|
while (!stoppingToken.IsCancellationRequested && !_internalCts.Token.IsCancellationRequested)
|
||||||
|
{
|
||||||
if (!s7Helper.IsConnected)
|
if (!s7Helper.IsConnected)
|
||||||
{
|
{
|
||||||
var isConnected = await s7Helper.ConnectAsync();
|
var isConnected = await s7Helper.ConnectAsync();
|
||||||
if (!isConnected)
|
if (!isConnected)
|
||||||
{
|
{
|
||||||
_logger.Error("Failed to connect to PLC.");
|
_logger.Error("Failed to connect to PLC.");
|
||||||
await Task.Delay(5000, stoppingToken);
|
break;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -178,48 +175,41 @@ namespace RIZO.Service.PLCbackTask
|
|||||||
ProductionLifeStage = 1,
|
ProductionLifeStage = 1,
|
||||||
PasstationType = 3,
|
PasstationType = 3,
|
||||||
PasstationDescription = "出站完毕",
|
PasstationDescription = "出站完毕",
|
||||||
EffectTime= DateTime.Now,
|
EffectTime = DateTime.Now,
|
||||||
OutStationTime= DateTime.Now,
|
OutStationTime = DateTime.Now,
|
||||||
ResultCode=1,
|
ResultCode = 1,
|
||||||
ResultDescription="OK",
|
ResultDescription = "OK",
|
||||||
CreatedTime= DateTime.Now
|
CreatedTime = DateTime.Now
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 出站完毕响应
|
// 出站完毕响应
|
||||||
await s7Helper.WriteBoolAsync(_plcAddress.Write.Resp, true);
|
await s7Helper.WriteBoolAsync(_plcAddress.Write.Resp, true);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Wait for a defined interval before the next poll
|
// Wait for a defined interval before the next poll
|
||||||
await Task.Delay(1000, stoppingToken);
|
await Task.Delay(1000, stoppingToken);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_logger.Error(ex, "Fatal error in PLC polling service");
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
_logger.Info("PLC Polling Service stopped");
|
|
||||||
|
|
||||||
// 确保资源被正确释放
|
}
|
||||||
if (_internalCts != null)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_internalCts.Dispose();
|
_logger.Error(ex, "Fatal error in PLC polling service");
|
||||||
_internalCts = null;
|
throw;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_logger.Info("PLC Polling Service stopped");
|
||||||
|
|
||||||
|
// 确保资源被正确释放
|
||||||
|
if (_internalCts != null)
|
||||||
|
{
|
||||||
|
_internalCts.Dispose();
|
||||||
|
_internalCts = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Console.WriteLine("后台任务关闭!!!!!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user