This commit is contained in:
xyy
2026-04-02 20:10:08 +08:00
parent 47cfa4c05b
commit 7042f94774
4 changed files with 97 additions and 6 deletions

View File

@@ -251,7 +251,7 @@ namespace MembranePoreTester.ViewModels
public ICommand RemoveDataPointCommand { get; }
public ICommand CalculateCommand { get; }
public ICommand GenerateReportCommand { get; }
private System.Windows.Threading.DispatcherTimer _timer; // 添加定时器字段
public PoreDistributionViewModel()
{
_plcService = App.PlcService;
@@ -272,11 +272,24 @@ namespace MembranePoreTester.ViewModels
Record.DataPoints.CollectionChanged += (s, e) => UpdatePlot();
// 加到构造函数最后即可
ClearAllCommand = new RelayCommand(ClearAllData);
RemoveDataPointCommand = new RelayCommand(RemoveSelectedDataPoint, () => SelectedDataPoint != null);
// 启动定时器,每秒读取一次
_timer = new System.Windows.Threading.DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.Tick += async (s, e) =>
{
if (StationId > 0 && !IsDisposed)
{
await ReadSpeedRateAsync();
}
};
_timer.Start();
// 延迟2秒后读取确保连接稳定
Task.Delay(2000).ContinueWith(async _ =>
{
@@ -290,6 +303,38 @@ namespace MembranePoreTester.ViewModels
ClearData();
}
private string _speedRate1;
public string SpeedRate1
{
get => _speedRate1;
set => SetProperty(ref _speedRate1, value);
}
private async Task ReadSpeedRateAsync()
{
try
{
float speedRate = 0;
switch (StationId)
{
case 1:
speedRate = await _plcService.ReadFloatAsync(_plcConfig.HPCoeff11); // 使用正确的地址
SpeedRate1 = speedRate.ToString("F3");
break;
// case 2,3 可类似添加
}
// 如果需要刷新 Record 中的绑定(可选)
OnPropertyChanged(nameof(SpeedRate1));
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"读取加压速率失败: {ex.Message}");
}
}
private async Task ReadPressureModeAsync()
{
await SafeExecuteAsync($"ReadPressureModeAsync{StationId}", async () =>