This commit is contained in:
@@ -28,6 +28,20 @@ namespace MembranePoreTester.ViewModels
|
||||
set => SetProperty(ref _record, value);
|
||||
}
|
||||
|
||||
// 初始化,确保 StationId 在读取PLC前已设置
|
||||
public void Initialize(int stationId)
|
||||
{
|
||||
StationId = stationId;
|
||||
|
||||
// 延迟读取当前PLC值以确保连接稳定
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(200);
|
||||
await ReadCurrentPlcAsync();
|
||||
await ReadSpeedRateAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public List<TestLiquid> Liquids => TestLiquid.Predefined;
|
||||
public List<string> PressureUnits => new() { "Pa", "cmHg", "psi" };
|
||||
public List<string> MembraneTypes => new() { "平板膜", "中空纤维膜" };
|
||||
@@ -140,20 +154,25 @@ namespace MembranePoreTester.ViewModels
|
||||
try
|
||||
{
|
||||
float speedRate = 0;
|
||||
ushort address = 0;
|
||||
switch (StationId)
|
||||
{
|
||||
|
||||
case 1:
|
||||
speedRate = await _plcService. ReadFloatAsync(_plcConfig.HPCoeff11);
|
||||
address = _plcConfig.HPCoeff11;
|
||||
speedRate = await _plcService.ReadFloatAsync(address);
|
||||
Record.SpeedRate1 = speedRate.ToString("F3");
|
||||
break;
|
||||
case 2:
|
||||
address = _plcConfig.HPCoeff12;
|
||||
speedRate = await _plcService.ReadFloatAsync(address);
|
||||
Record.SpeedRate1 = speedRate.ToString("F3");
|
||||
break;
|
||||
case 3:
|
||||
address = _plcConfig.HPCoeff13;
|
||||
speedRate = await _plcService.ReadFloatAsync(address);
|
||||
Record.SpeedRate1 = speedRate.ToString("F3");
|
||||
break;
|
||||
//case 2:
|
||||
// speedRate = await _plcService.ReadFloatAsync(_plcConfig.PressureRate2);
|
||||
// Record.SpeedRate2 = speedRate.ToString("F3");
|
||||
// break;
|
||||
//case 3:
|
||||
// speedRate = await _plcService.ReadFloatAsync(_plcConfig.PressureRate3);
|
||||
// Record.SpeedRate3 = speedRate.ToString("F3");
|
||||
// break;
|
||||
}
|
||||
OnPropertyChanged(nameof(Record));
|
||||
}
|
||||
|
||||
@@ -83,6 +83,33 @@ namespace MembranePoreTester.ViewModels
|
||||
}
|
||||
|
||||
|
||||
|
||||
private PressureModeItem _selectedInTakeMode;
|
||||
private List<PressureModeItem> _inTakeModeList;
|
||||
|
||||
public List<PressureModeItem> InTakeModeList => _inTakeModeList ??= new List<PressureModeItem>
|
||||
{
|
||||
new PressureModeItem { Text = "底部进气", Value = 0 },
|
||||
new PressureModeItem { Text = "顶部进气", Value = 1 }
|
||||
};
|
||||
|
||||
public PressureModeItem SelecteInTakeMode
|
||||
{
|
||||
get => _selectedInTakeMode;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _selectedInTakeMode, value))
|
||||
{
|
||||
Task.Run(async () => await WriteInTakeModeAsync(value?.Text ?? "底部进气"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
private readonly IPlcService _plcService;
|
||||
private readonly PlcConfiguration _plcConfig;
|
||||
private bool _isPressing;
|
||||
@@ -108,6 +135,19 @@ namespace MembranePoreTester.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public string UpAndDown
|
||||
{
|
||||
get => _selectedInTakeMode?.Text ?? "底部";
|
||||
set
|
||||
{
|
||||
var mode = InTakeModeList.FirstOrDefault(m => m.Text == value);
|
||||
if (mode != null)
|
||||
{
|
||||
SelecteInTakeMode = mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string PressButtonText
|
||||
{
|
||||
get => _pressButtonText;
|
||||
@@ -174,7 +214,10 @@ namespace MembranePoreTester.ViewModels
|
||||
// 仅在非抑制模式下将改变写回PLC,避免把PLC读回的值再次写入造成循环
|
||||
if (changed && !_suppressPressureValidation)
|
||||
{
|
||||
_ = _plcService.WriteMultipleRegistersAsync(_plcConfig.PressureUpperLimit, (float)value);
|
||||
ushort address = StationId == 1 ? _plcConfig.PressureUpperLimit
|
||||
: StationId == 2 ? _plcConfig.PressureUpperLimit2
|
||||
: _plcConfig.PressureUpperLimit3;
|
||||
_ = _plcService.WriteMultipleRegistersAsync(address, (float)value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,7 +230,7 @@ namespace MembranePoreTester.ViewModels
|
||||
if (SetProperty(ref _pressureRate, value))
|
||||
{
|
||||
// 值改变时写入PLC
|
||||
_ = _plcService.WriteMultipleRegistersAsync(_plcConfig.PressureRate, (float)value);
|
||||
_ = _plcService.WriteMultipleRegistersAsync(StationId == 1 ? _plcConfig.PressureRate : StationId == 2 ? _plcConfig.PressureRate2 : _plcConfig.PressureRate3, (float)value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,8 +240,13 @@ namespace MembranePoreTester.ViewModels
|
||||
{
|
||||
try
|
||||
{
|
||||
float upperLimit = await _plcService.ReadFloatAsync(_plcConfig.PressureUpperLimit);
|
||||
float rate = await _plcService.ReadFloatAsync(_plcConfig.PressureRate);
|
||||
// 根据工位选择加压上限的PLC地址
|
||||
ushort upperLimitAddress = StationId == 1 ? _plcConfig.PressureUpperLimit
|
||||
: StationId == 2 ? _plcConfig.PressureUpperLimit2
|
||||
: _plcConfig.PressureUpperLimit3;
|
||||
|
||||
float upperLimit = await _plcService.ReadFloatAsync(upperLimitAddress);
|
||||
float rate = await _plcService.ReadFloatAsync(StationId == 1 ? _plcConfig.PressureRate : StationId == 2 ? _plcConfig.PressureRate2 : _plcConfig.PressureRate3);
|
||||
|
||||
|
||||
|
||||
@@ -241,7 +289,7 @@ namespace MembranePoreTester.ViewModels
|
||||
StartCommand = new RelayCommand(async () =>
|
||||
{
|
||||
// 启动PLC
|
||||
await WriteCoilAsync(_plcConfig.StartCoil, true);
|
||||
await WriteCoilAsync(StationId == 1 ? _plcConfig.StartCoil : StationId == 2 ? _plcConfig.StartCoil2 : _plcConfig.StartCoil3, true);
|
||||
|
||||
// 启动孔分布自动采集
|
||||
PoreDistributionVM.StartCollecting();
|
||||
@@ -253,7 +301,7 @@ namespace MembranePoreTester.ViewModels
|
||||
// 停止自动采集
|
||||
PoreDistributionVM.StopCollecting();
|
||||
// 停止PLC
|
||||
await WriteCoilAsync(_plcConfig.StopCoil, true);
|
||||
await WriteCoilAsync(StationId == 1 ? _plcConfig.StopCoil : StationId == 2 ? _plcConfig.StopCoil2 : _plcConfig.StopCoil3, true);
|
||||
});
|
||||
|
||||
|
||||
@@ -274,6 +322,7 @@ namespace MembranePoreTester.ViewModels
|
||||
{
|
||||
await ReadPressureModeAsync();
|
||||
await ReadPressureParametersAsync(); // 新增:读取压力参数
|
||||
await ReadInTakeModeAsync();
|
||||
|
||||
}, TaskScheduler.Default);
|
||||
|
||||
@@ -301,6 +350,44 @@ namespace MembranePoreTester.ViewModels
|
||||
});
|
||||
}
|
||||
|
||||
private async Task ReadInTakeModeAsync()
|
||||
{
|
||||
await SafeExecuteAsync($"ReadInTakeModeAsync{StationId}", async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
ushort address = 0;
|
||||
|
||||
if (StationId == 1)
|
||||
{
|
||||
address = _plcConfig.UpAndDown1;
|
||||
}
|
||||
else if (StationId == 2)
|
||||
{
|
||||
address = _plcConfig.UpAndDown2;
|
||||
}
|
||||
else if (StationId == 3)
|
||||
{
|
||||
address = _plcConfig.UpAndDown3;
|
||||
}
|
||||
|
||||
ushort[] values = await _plcService.ReadHoldingRegistersAsync(address, 1);
|
||||
ushort val = values[0];
|
||||
string newValue = val == 0 ? "底部进气" : "顶部进气";
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
// 更新选中项
|
||||
UpAndDown = newValue;
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"读取出口模式失败: {ex.Message}");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async Task TogglePressAsync()
|
||||
{
|
||||
try
|
||||
@@ -346,11 +433,11 @@ namespace MembranePoreTester.ViewModels
|
||||
{
|
||||
try
|
||||
{
|
||||
bool status = await _plcService.ReadCoilAsync(_plcConfig.EnableCoil); // 读取 M21
|
||||
bool status = await _plcService.ReadCoilAsync(StationId == 1 ? _plcConfig.EnableCoil : StationId == 2 ? _plcConfig.EnableCoil2 : _plcConfig.EnableCoil3); // 读取 M21
|
||||
EnableStatus = status;
|
||||
|
||||
|
||||
bool pressStatus = await _plcService.ReadCoilAsync(_plcConfig.PressCoil);//这里也要更新加压的按钮状态
|
||||
bool pressStatus = await _plcService.ReadCoilAsync(StationId == 1 ? _plcConfig.PressCoil : StationId == 2 ? _plcConfig.PressCoil2 : _plcConfig.PressCoil3);//这里也要更新加压的按钮状态
|
||||
|
||||
_isPressing = pressStatus;
|
||||
|
||||
@@ -401,6 +488,37 @@ namespace MembranePoreTester.ViewModels
|
||||
}
|
||||
|
||||
|
||||
private async Task WriteInTakeModeAsync(string mode)
|
||||
{
|
||||
if (IsDisposed) return;
|
||||
|
||||
ushort val = mode.ToString().Contains("底部") ? (ushort)0 : (ushort)1;
|
||||
try
|
||||
{
|
||||
ushort address = 0;
|
||||
|
||||
if (StationId == 1)
|
||||
{
|
||||
address = _plcConfig.UpAndDown1;
|
||||
}
|
||||
else if (StationId == 2)
|
||||
{
|
||||
address = _plcConfig.UpAndDown2;
|
||||
}
|
||||
else if (StationId == 3)
|
||||
{
|
||||
address = _plcConfig.UpAndDown3;
|
||||
}
|
||||
|
||||
await _plcService.WriteSingleRegisterAsync(address, val);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"写出口模式失败: {ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private bool _isPoreDistributionActive;
|
||||
public bool IsPoreDistributionActive
|
||||
{
|
||||
@@ -426,10 +544,13 @@ namespace MembranePoreTester.ViewModels
|
||||
var station = new StationItem
|
||||
{
|
||||
Name = $"工位 {i}",
|
||||
BubblePointVM = new BubblePointViewModel { StationId = i },
|
||||
PoreDistributionVM = new PoreDistributionViewModel { StationId = i },
|
||||
BubblePointVM = new BubblePointViewModel(),
|
||||
PoreDistributionVM = new PoreDistributionViewModel(),
|
||||
StationId = i
|
||||
};
|
||||
// 在构造完成后显式传入 StationId 以初始化 VM
|
||||
station.BubblePointVM.Initialize(i);
|
||||
station.PoreDistributionVM.Initialize(i);
|
||||
Stations.Add(station);
|
||||
}
|
||||
|
||||
@@ -456,6 +577,11 @@ namespace MembranePoreTester.ViewModels
|
||||
bool highPressAlarm = await plc.ReadCoilAsync(config.HighPressAlarm);
|
||||
bool lowPressAlarm = await plc.ReadCoilAsync(config.LowPressAlarm);
|
||||
|
||||
|
||||
bool Midnight1 = await plc.ReadCoilAsync(config.Midnight1);
|
||||
bool Midnight2 = await plc.ReadCoilAsync(config.Midnight2);
|
||||
bool Midnight3 = await plc.ReadCoilAsync(config.Midnight3);
|
||||
|
||||
// 收集当前报警信息
|
||||
var newAlarms = new List<string>();
|
||||
if (smallFlowAlarm) newAlarms.Add("小流量计报警");
|
||||
@@ -463,6 +589,10 @@ namespace MembranePoreTester.ViewModels
|
||||
if (highPressAlarm) newAlarms.Add("高压超限");
|
||||
if (lowPressAlarm) newAlarms.Add("低压超限");
|
||||
|
||||
if (Midnight1) newAlarms.Add("1工位漏夜");
|
||||
if (Midnight2) newAlarms.Add("2工位漏夜");
|
||||
if (Midnight3) newAlarms.Add("3工位漏夜");
|
||||
|
||||
// 更新UI(避免频繁刷新集合导致界面闪烁,直接替换内容)
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
|
||||
@@ -731,6 +731,18 @@ namespace MembranePoreTester.ViewModels
|
||||
set => SetProperty(ref _stationId, value);
|
||||
}
|
||||
|
||||
// 初始化方法:在 MainViewModel 创建 StationItem 后调用,确保 StationId 已设置
|
||||
public void Initialize(int stationId)
|
||||
{
|
||||
StationId = stationId;
|
||||
// 延迟调用读取模式,确保PLC连接稳定
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(200);
|
||||
await ReadPressureModeAsync();
|
||||
});
|
||||
}
|
||||
|
||||
public void SaveToDatabase()
|
||||
{
|
||||
var entity = new PoreDistributionEntity
|
||||
|
||||
Reference in New Issue
Block a user