更新2026

This commit is contained in:
GukSang.Jin
2026-05-16 17:47:46 +08:00
parent 6ed2c7a45e
commit bf58b3e2bd
7 changed files with 188 additions and 82 deletions

View File

@@ -18,6 +18,8 @@ namespace TabletTester2025.ViewModels
private readonly AlarmService _alarm;
private readonly PlcConfiguration _plcConfig;
private DispatcherTimer _timer;
private bool _isConnecting;
private bool _isUpdatingRealtime;
@@ -48,7 +50,7 @@ namespace TabletTester2025.ViewModels
ExportAllCommand = new AsyncRelayCommand(ExportAllAsync);
_timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(500) };
_timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
_timer.Tick += OnTimerTick;
_ = ConnectToPlc();
_timer.Start();
@@ -73,15 +75,53 @@ namespace TabletTester2025.ViewModels
private async Task ConnectToPlc()
{
try { await _plc.ConnectAsync(); PlcStatus = "已连接"; }
catch { PlcStatus = "连接失败"; }
if (_isConnecting)
return;
try
{
_isConnecting = true;
PlcStatus = "连接中";
await _plc.ConnectAsync();
PlcStatus = _plc.IsConnected ? "已连接" : "连接失败";
}
catch
{
PlcStatus = "连接失败";
}
finally
{
_isConnecting = false;
}
}
private async void OnTimerTick(object sender, EventArgs e)
{
CurrentTime = DateTime.Now.ToString("HH:mm:ss");
if (PlcStatus != "已连接") return;
await Tester.UpdateRealTimeData();
if (!_plc.IsConnected)
{
await ConnectToPlc();
return;
}
PlcStatus = "已连接";
if (_isUpdatingRealtime)
return;
try
{
_isUpdatingRealtime = true;
await Tester.UpdateRealTimeData();
}
catch
{
PlcStatus = "连接失败";
}
finally
{
_isUpdatingRealtime = false;
}
}
private async Task ExportAllAsync()