Kaynağa Gözat

modify:控件遮罩,PLC连接状态修改

reconfiguration
ZhaoGang 2 ay önce
ebeveyn
işleme
7ead856bbb
7 değiştirilmiş dosya ile 117 ekleme ve 105 silme
  1. +44
    -76
      BPASmartClient.Academy/50L/PlcControl.cs
  2. +2
    -0
      BPASmartClient.Academy/App.xaml
  3. +30
    -0
      BPASmartClient.Academy/Converter/BoolToDoubleConverter.cs
  4. +28
    -0
      BPASmartClient.Academy/Converter/BoolToVisibilityReverseConverter.cs
  5. +2
    -2
      BPASmartClient.Academy/View/ReactionKettle50LView.xaml
  6. +1
    -27
      BPASmartClient.Academy/View/ReactionKettle50LView.xaml.cs
  7. +10
    -0
      BPASmartClient.Academy/ViewModel/ReactionKettle50LViewModel.cs

+ 44
- 76
BPASmartClient.Academy/50L/PlcControl.cs Dosyayı Görüntüle

@@ -16,14 +16,10 @@ namespace BPASmartClient.Academy._50L
private volatile static PlcControl _Instance;
public static PlcControl GetInstance => _Instance ?? (_Instance = new PlcControl());
private PlcControl() { }
public bool IsConnect => true;
public bool IsConnect => comm != null && comm.IsConnected();
public DeviceStatus DeviceStatus { get; set; } = new DeviceStatus();
private ICommSiemens comm = CommHelper.Siemens;
public Action<bool> IsConnectChanged { get; set; }
public string id { get; set; } = "";
private bool _lastConnectState;
private volatile uint _readFailureCount;
private Timer _readTrendDataTimer;
public void Init()
{
Task.Factory.StartNew(() =>
@@ -31,10 +27,8 @@ namespace BPASmartClient.Academy._50L
CommHelper.CreateSiemens(CpuType.S71500, Json<DevicePar>.Data.IpAddress).OnSuccess(s =>
{
comm = s.Content;
_lastConnectState = true;
IsConnectChanged?.Invoke(true);
WriteHeartBeat();
Read();
//_readTrendDataTimer = new(ReadTrendData, null,0,2000);
});
});

@@ -46,7 +40,6 @@ namespace BPASmartClient.Academy._50L
{
TaskManage.GetInstance.StartLong(() =>
{
_readFailureCount = 0;
comm.Read<bool[]>("DB1.DBX2000.0", 31).OnSuccess(s =>
{
//Data[FeedbackData.旋转电机启停反馈] = s.Content[0];
@@ -86,7 +79,7 @@ namespace BPASmartClient.Academy._50L
DeviceStatus.ColdWaterTankVacuumValve = s.Content[16];
DeviceStatus.CompressorRunning = s.Content[17];
#endregion
}).OnFailure((s) => { _readFailureCount++; });
});


comm.Read<float[]>("DB1.DBD2006", 10).OnSuccess(s =>
@@ -140,7 +133,7 @@ namespace BPASmartClient.Academy._50L
_isReadSuccess = true;
}
});
comm.Read<bool>("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content ).OnFailure((s) => { _readFailureCount++; });
comm.Read<bool>("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content );

comm.Read<bool[]>("DB1.DBX2004.0", 16).OnSuccess(s =>
{
@@ -149,80 +142,31 @@ namespace BPASmartClient.Academy._50L
AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").AbnormalNegativePressureFan = s.Content[2];
AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").BrinePumpError = s.Content[3];
AlarmHelper.GetInstance.Get<AlarmData>("50L反应釜").CompressorError = s.Content[4];
}).OnFailure((s) => { _readFailureCount++; });

if (Delay.GetInstance("心跳").Start(!DeviceStatus.HeartBeat, 2))
{
Write(BoolAddEnum.心跳, true);
}
//if (_readFailureCount > 3 && _lastConnectState)
//{
// _lastConnectState = false;
// IsConnectChanged?.Invoke(false);
//}
//if (_readFailureCount == 0 && !_lastConnectState)
//{
// _lastConnectState = true;
// IsConnectChanged?.Invoke(true);
//}
IsConnectChanged?.Invoke(this.IsConnect);
});
Thread.Sleep(100);
}, "数据获取", true);
//ThreadManage.GetInstance.StartLong(() =>
//{
// if (true)//产品开始测试数据
// {
// }
//}, "实时曲线数据");

}

private void ReadTrendData(object? state)
private void WriteHeartBeat()
{
if (comm.IsConnected())
Task.Factory.StartNew( async() =>
{
comm.Read<float[]>("DB1.DBD2006", 10).OnSuccess(s =>
while (true)
{
//Data[FeedbackData.反应釜温度] = s.Content[0];
//Data[FeedbackData.反应釜蒸汽压力] = s.Content[1];
//Data[FeedbackData.反应釜蒸汽流量] = s.Content[2];
//Data[FeedbackData.冷凝水罐温度] = s.Content[3];
//Data[FeedbackData.冷凝水罐湿度] = s.Content[4];
//Data[FeedbackData.负压流量] = s.Content[5];
//Data[FeedbackData.称重水罐重量] = s.Content[6];
//Data[FeedbackData.反应釜编码器值] = s.Content[7];
//Data[FeedbackData.蒸汽比例阀实际开度] = s.Content[8];
DeviceStatus.ReactTemp = s.Content[0];
DeviceStatus.ReactSteamPressure = s.Content[1];
DeviceStatus.ReactSteamFlow = s.Content[2];
DeviceStatus.ColdWaterTankTemp = s.Content[3];
DeviceStatus.ColdWaterTankHumidity = s.Content[4];
DeviceStatus.VacuumFlow = s.Content[5];
DeviceStatus.WaterTankWeight = s.Content[6];
DeviceStatus.ReactEncoderValue = s.Content[7];
DeviceStatus.SteamProportValveRealOpening = s.Content[8];
DeviceStatus.BrineTankWeight = s.Content[9];


SamplingData sampling = new SamplingData()
if (this.IsConnect)
{
Temperature = s.Content[0],
SteamPressure = s.Content[1],
SteamFlowRate = s.Content[2],
CondensateWaterTemperature = s.Content[3],
CondensateWaterHumidity = s.Content[4],
NegativePressureFlowRate = s.Content[5],
WeighingWaterTankWeight = s.Content[6],
ReactEncoderValue = s.Content[7],
ProportionalValveOpening = s.Content[8],
BrineTankWeight = s.Content[9]
};
SqliteOperate.GetInstance.Add(sampling);
SqliteOperate.GetInstance.RealtimeChart(sampling);
//var tokenSource = new CancellationTokenSource();
//tokenSource.CancelAfter(2000);
//await WriteAsync(BoolAddEnum.心跳, true,tokenSource.Token);
Write(BoolAddEnum.心跳, true);
}
//IsConnectChanged?.Invoke(this.IsConnect);
await Task.Delay(2500);

});
}
}
},TaskCreationOptions.LongRunning);
}
public OperateResult Write(BoolAddEnum add, bool value)
{
@@ -248,5 +192,29 @@ namespace BPASmartClient.Academy._50L
}
}

public async Task<OperateResult> WriteAsync(BoolAddEnum add, bool value,CancellationToken cancellationToken=default)
{
if (boolAdds.ContainsKey(add))
{
try
{
var result = await Task.Run(() =>
{
return comm.Write(boolAdds[add], value);
}, cancellationToken);
return result;

}
catch (OperationCanceledException)
{
return OperateResult.Failed($"操作被取消");
}
}
else
{
return OperateResult.Failed($"未查找到该地址{add.ToString()}");
}
}

}
}

+ 2
- 0
BPASmartClient.Academy/App.xaml Dosyayı Görüntüle

@@ -41,6 +41,8 @@
<ResourceDictionary>
<convert:DeviceCtrlConvert x:Key="deviceConvert" />
<convert:BoolToColorConverter x:Key="BoolToColorConverter" TrueColor="Green" FalseColor="Red"/>
<convert:BoolToVisibilityReverseConverter x:Key="BoolToVisibilityReverseConverter"/>
<convert:BoolToDoubleConverter x:Key="BoolToDoubleConverter" TrueValue="1" FalseValue="0.5"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>



+ 30
- 0
BPASmartClient.Academy/Converter/BoolToDoubleConverter.cs Dosyayı Görüntüle

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace BPASmartClient.Academy.Converter
{
public class BoolToDoubleConverter : IValueConverter
{
public double TrueValue { get; set; }
public double FalseValue { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is bool tempBool)
{
return tempBool ? TrueValue : FalseValue;
}
return FalseValue;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

+ 28
- 0
BPASmartClient.Academy/Converter/BoolToVisibilityReverseConverter.cs Dosyayı Görüntüle

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace BPASmartClient.Academy.Converter
{
public class BoolToVisibilityReverseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value is bool tempBool)
{
return tempBool ? Visibility.Collapsed : Visibility.Visible;
}
return Visibility.Collapsed;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

+ 2
- 2
BPASmartClient.Academy/View/ReactionKettle50LView.xaml Dosyayı Görüntüle

@@ -245,7 +245,7 @@
<TextBlock
x:Name="state" Grid.RowSpan="2" HorizontalAlignment="Center" VerticalAlignment="Center"
Panel.ZIndex="299" d:Visibility="Collapsed" FontSize="100" FontWeight="Black"
Foreground="Red" Text="设 备 未 连 接" Visibility="Visible" />
Foreground="Red" Text="设 备 未 连 接" Visibility="{Binding PLCIsConnected,Converter={StaticResource BoolToVisibilityReverseConverter}}" />
<!--#endregion-->
<!--#region 切换设备类型-->
<Grid
@@ -261,7 +261,7 @@
<!--#endregion-->
<Grid
x:Name="ooo" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" d:Opacity="1.0"
IsHitTestVisible="False" Opacity="0.5">
IsHitTestVisible="{Binding PLCIsConnected}" Opacity="{Binding PLCIsConnected,Converter={StaticResource BoolToDoubleConverter}}">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />


+ 1
- 27
BPASmartClient.Academy/View/ReactionKettle50LView.xaml.cs Dosyayı Görüntüle

@@ -27,35 +27,9 @@ namespace BPASmartClient.Academy.View
{
InitializeComponent();
this.DataContext = ReactionKettle50LViewModel.GetInstance;
this.Loaded += ReactionKettle50LView_Loaded;
}
private bool _lastState = false;
private void ReactionKettle50LView_Loaded(object sender, RoutedEventArgs e)
{
PlcControl.GetInstance.IsConnectChanged = o =>
{
App.Current.Dispatcher.Invoke(() =>
{
if (o && !_lastState)
{
ooo.Opacity = 1;
ooo.IsHitTestVisible = true;
state.Visibility = Visibility.Collapsed;
}
else if(!o && _lastState)
{

ooo.Opacity = 0.5;
ooo.IsHitTestVisible = false;
state.Text = "设 备 未 连 接";
//MessageNotify.GetInstance.ShowRunLog("设备未连接");
state.Visibility = Visibility.Visible;
state.Foreground = Brushes.Red;
}
});
_lastState = o;
};
}


private void open_Click(object sender, RoutedEventArgs e)


+ 10
- 0
BPASmartClient.Academy/ViewModel/ReactionKettle50LViewModel.cs Dosyayı Görüntüle

@@ -62,6 +62,14 @@ namespace BPASmartClient.Academy.ViewModel
ParameterSetView sv = new ParameterSetView();
sv.Show();
});
Task.Factory.StartNew(async () =>
{
while (true)
{
this.PLCIsConnected = PlcControl.GetInstance.IsConnect;
await Task.Delay(500);
}
});
}

@@ -91,6 +99,8 @@ namespace BPASmartClient.Academy.ViewModel
set { _isChecked = value; OnPropertyChanged(); }
}

private bool _plcIsConnected;
public bool PLCIsConnected { get { return _plcIsConnected; } set { _plcIsConnected = value; OnPropertyChanged(); } }

public BPARelayCommand Start { get; set; }
public BPARelayCommand DataRecordCommand { get; set; }


Yükleniyor…
İptal
Kaydet