读取完毕
This commit is contained in:
parent
55545b3193
commit
f445328ba9
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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" />
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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("后台任务关闭!!!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user