From f445328ba93bb18e823d59dfa62e324cf551b4f8 Mon Sep 17 00:00:00 2001 From: gcw_MV9p2JJN Date: Sun, 1 Feb 2026 19:19:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/Helper/SiemensS7Helper.cs | 15 +++-- Infrastructure/RIZO.Infrastructure.csproj | 1 - RIZO.Admin.WebApi/Program.cs | 4 +- .../PLCbackTask/MES_PLC_InterationTask.cs | 62 ++++++++----------- 4 files changed, 35 insertions(+), 47 deletions(-) diff --git a/Infrastructure/Helper/SiemensS7Helper.cs b/Infrastructure/Helper/SiemensS7Helper.cs index acd9d7a..1a11e68 100644 --- a/Infrastructure/Helper/SiemensS7Helper.cs +++ b/Infrastructure/Helper/SiemensS7Helper.cs @@ -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; } } } diff --git a/Infrastructure/RIZO.Infrastructure.csproj b/Infrastructure/RIZO.Infrastructure.csproj index cb58b47..1861483 100644 --- a/Infrastructure/RIZO.Infrastructure.csproj +++ b/Infrastructure/RIZO.Infrastructure.csproj @@ -12,7 +12,6 @@ - diff --git a/RIZO.Admin.WebApi/Program.cs b/RIZO.Admin.WebApi/Program.cs index a82b7a1..73098c2 100644 --- a/RIZO.Admin.WebApi/Program.cs +++ b/RIZO.Admin.WebApi/Program.cs @@ -96,9 +96,9 @@ builder.Services.AddSwaggerConfig(); builder.Services.AddLogo(); // 添加本地化服务 builder.Services.AddLocalization(options => options.ResourcesPath = ""); - -builder.Services.AddHostedService(); builder.Services.AddHslCommunication(); +builder.Services.AddHostedService(); + // 在应用程序启动的最开始处调用 var app = builder.Build(); diff --git a/RIZO.Service/PLCbackTask/MES_PLC_InterationTask.cs b/RIZO.Service/PLCbackTask/MES_PLC_InterationTask.cs index dd2e71c..4fc6d84 100644 --- a/RIZO.Service/PLCbackTask/MES_PLC_InterationTask.cs +++ b/RIZO.Service/PLCbackTask/MES_PLC_InterationTask.cs @@ -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("后台任务关闭!!!!!"); } } }