读取完毕

This commit is contained in:
gcw_MV9p2JJN 2026-02-01 19:19:49 +08:00
parent 55545b3193
commit f445328ba9
4 changed files with 35 additions and 47 deletions

View File

@ -49,15 +49,13 @@ namespace RIZO.Infrastructure.Helper
while (attempt < maxRetries)
{
attempt++;
try
var result = await Task.Run(() => _plc.ConnectServer());
_isConnected = result.IsSuccess;
if (!_isConnected)
{
var result = await Task.Run(() => _plc.ConnectServer());
_isConnected = result.IsSuccess;
return _isConnected;
}
catch (Exception ex)
{
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 失败: {ex.Message}");
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 失败");
if (attempt < maxRetries)
{
@ -69,6 +67,7 @@ namespace RIZO.Infrastructure.Helper
Console.WriteLine("无法建立PLC连接请检查网络和设备状态");
_plc?.ConnectClose();
_plc = null;
return _isConnected;
}
}
}

View File

@ -12,7 +12,6 @@
<ItemGroup>
<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="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="S7netplus" Version="0.20.0" />

View File

@ -96,9 +96,9 @@ builder.Services.AddSwaggerConfig();
builder.Services.AddLogo();
// 添加本地化服务
builder.Services.AddLocalization(options => options.ResourcesPath = "");
builder.Services.AddHostedService<MES_PLC_InterationTask>();
builder.Services.AddHslCommunication();
builder.Services.AddHostedService<MES_PLC_InterationTask>();
// 在应用程序启动的最开始处调用
var app = builder.Build();

View File

@ -39,21 +39,18 @@ namespace RIZO.Service.PLCbackTask
_internalCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
using (s7Helper = new SiemensS7Helper(_plcAddress.IP))
{
while (!stoppingToken.IsCancellationRequested && !_internalCts.Token.IsCancellationRequested)
try
{
try
{
while (!stoppingToken.IsCancellationRequested && !_internalCts.Token.IsCancellationRequested)
{
if (!s7Helper.IsConnected)
{
var isConnected = await s7Helper.ConnectAsync();
if (!isConnected)
{
_logger.Error("Failed to connect to PLC.");
await Task.Delay(5000, stoppingToken);
continue;
break;
}
}
@ -178,48 +175,41 @@ namespace RIZO.Service.PLCbackTask
ProductionLifeStage = 1,
PasstationType = 3,
PasstationDescription = "出站完毕",
EffectTime= DateTime.Now,
OutStationTime= DateTime.Now,
ResultCode=1,
ResultDescription="OK",
CreatedTime= DateTime.Now
EffectTime = DateTime.Now,
OutStationTime = DateTime.Now,
ResultCode = 1,
ResultDescription = "OK",
CreatedTime = DateTime.Now
};
// 出站完毕响应
await s7Helper.WriteBoolAsync(_plcAddress.Write.Resp, true);
}
#endregion
// Wait for a defined interval before the next poll
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)
{
_internalCts.Dispose();
_internalCts = null;
}
}
catch (Exception ex)
{
_logger.Error(ex, "Fatal error in PLC polling service");
throw;
}
finally
{
_logger.Info("PLC Polling Service stopped");
// 确保资源被正确释放
if (_internalCts != null)
{
_internalCts.Dispose();
_internalCts = null;
}
Console.WriteLine("后台任务关闭!!!!!");
}
}
}