From 7ead856bbbf85a4433b83ca82d47036dc8623cb1 Mon Sep 17 00:00:00 2001 From: ZhaoGang <15196688790@163.com> Date: Fri, 18 Oct 2024 11:34:35 +0800 Subject: [PATCH] =?UTF-8?q?modify:=E6=8E=A7=E4=BB=B6=E9=81=AE=E7=BD=A9?= =?UTF-8?q?=EF=BC=8CPLC=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.Academy/50L/PlcControl.cs | 120 +++++++----------- BPASmartClient.Academy/App.xaml | 2 + .../Converter/BoolToDoubleConverter.cs | 30 +++++ .../BoolToVisibilityReverseConverter.cs | 28 ++++ .../View/ReactionKettle50LView.xaml | 4 +- .../View/ReactionKettle50LView.xaml.cs | 28 +--- .../ViewModel/ReactionKettle50LViewModel.cs | 10 ++ 7 files changed, 117 insertions(+), 105 deletions(-) create mode 100644 BPASmartClient.Academy/Converter/BoolToDoubleConverter.cs create mode 100644 BPASmartClient.Academy/Converter/BoolToVisibilityReverseConverter.cs diff --git a/BPASmartClient.Academy/50L/PlcControl.cs b/BPASmartClient.Academy/50L/PlcControl.cs index 3f45325e..c7f0b6ba 100644 --- a/BPASmartClient.Academy/50L/PlcControl.cs +++ b/BPASmartClient.Academy/50L/PlcControl.cs @@ -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 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.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("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("DB1.DBD2006", 10).OnSuccess(s => @@ -140,7 +133,7 @@ namespace BPASmartClient.Academy._50L _isReadSuccess = true; } }); - comm.Read("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content ).OnFailure((s) => { _readFailureCount++; }); + comm.Read("DB1.DBX1003.7", 1).OnSuccess(s => DeviceStatus.HeartBeat = s.Content ); comm.Read("DB1.DBX2004.0", 16).OnSuccess(s => { @@ -149,80 +142,31 @@ namespace BPASmartClient.Academy._50L AlarmHelper.GetInstance.Get("50L反应釜").AbnormalNegativePressureFan = s.Content[2]; AlarmHelper.GetInstance.Get("50L反应釜").BrinePumpError = s.Content[3]; AlarmHelper.GetInstance.Get("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("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 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()}"); + } + } + } } diff --git a/BPASmartClient.Academy/App.xaml b/BPASmartClient.Academy/App.xaml index ca2506cc..3045296c 100644 --- a/BPASmartClient.Academy/App.xaml +++ b/BPASmartClient.Academy/App.xaml @@ -41,6 +41,8 @@ + + diff --git a/BPASmartClient.Academy/Converter/BoolToDoubleConverter.cs b/BPASmartClient.Academy/Converter/BoolToDoubleConverter.cs new file mode 100644 index 00000000..3a990700 --- /dev/null +++ b/BPASmartClient.Academy/Converter/BoolToDoubleConverter.cs @@ -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(); + } + } +} diff --git a/BPASmartClient.Academy/Converter/BoolToVisibilityReverseConverter.cs b/BPASmartClient.Academy/Converter/BoolToVisibilityReverseConverter.cs new file mode 100644 index 00000000..6ebd448e --- /dev/null +++ b/BPASmartClient.Academy/Converter/BoolToVisibilityReverseConverter.cs @@ -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(); + } + } +} diff --git a/BPASmartClient.Academy/View/ReactionKettle50LView.xaml b/BPASmartClient.Academy/View/ReactionKettle50LView.xaml index 95d98c7d..aff2d277 100644 --- a/BPASmartClient.Academy/View/ReactionKettle50LView.xaml +++ b/BPASmartClient.Academy/View/ReactionKettle50LView.xaml @@ -245,7 +245,7 @@ + Foreground="Red" Text="设 备 未 连 接" Visibility="{Binding PLCIsConnected,Converter={StaticResource BoolToVisibilityReverseConverter}}" /> + IsHitTestVisible="{Binding PLCIsConnected}" Opacity="{Binding PLCIsConnected,Converter={StaticResource BoolToDoubleConverter}}"> diff --git a/BPASmartClient.Academy/View/ReactionKettle50LView.xaml.cs b/BPASmartClient.Academy/View/ReactionKettle50LView.xaml.cs index 0396a29a..0bd96304 100644 --- a/BPASmartClient.Academy/View/ReactionKettle50LView.xaml.cs +++ b/BPASmartClient.Academy/View/ReactionKettle50LView.xaml.cs @@ -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) diff --git a/BPASmartClient.Academy/ViewModel/ReactionKettle50LViewModel.cs b/BPASmartClient.Academy/ViewModel/ReactionKettle50LViewModel.cs index 37920420..1977bf09 100644 --- a/BPASmartClient.Academy/ViewModel/ReactionKettle50LViewModel.cs +++ b/BPASmartClient.Academy/ViewModel/ReactionKettle50LViewModel.cs @@ -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; }