• 基于WPF技术的换热站智能监控系统16--动态数据绑定


    1、实现思路

    1)实时读取到的数据绑定到前台UI控件上,这个通过MVVM模式实现,同时注意实时读取必须通过任务task异步方式,这就需要读取PLC数据。

    2)UI控件的动作,如开或关水泵,必定能够将值写入PLC,这就需要写入PLC数据,此时当上面1的任务执行时,又获取到了新的实时数据,从而改变UI控件

    注意:

    默认启动程序后,就主动读取PLC数据,启动成功后,可以关闭某个水泵

    2、开搞

    1、完善数据块中的变量

    每个水泵有3个数据,即状态,温度,压力,所以有9个变量

    2、完善监控变量

     

    3、完善逻辑代码

    1. private void ReadmyPLC()
    2. {
    3. tasks.Add(Task.Run(async () =>
    4. {
    5. plc.Open();
    6. while (!cts.IsCancellationRequested)
    7. {
    8. await Task.Delay(5000);
    9. //读取float,从左到右参数依次是:数据块类型,数据块号,启始地址,变量类型,读取数量
    10. var result = plc.Read(S7.Net.DataType.DataBlock, 1, 2, S7.Net.VarType.Real, 6);
    11. float[] values = (float[])result;
    12. //水泵1数据
    13. MainData.MomentDataList1[0].Value = values[0];
    14. MainData.MomentDataList1[1].Value = values[1];
    15. //水泵2数据
    16. MainData.MomentDataList2[0].Value = values[2];
    17. MainData.MomentDataList2[1].Value = values[3];
    18. //水泵3数据
    19. MainData.MomentDataList3[0].Value = values[4];
    20. MainData.MomentDataList3[1].Value = values[5];
    21. //读取bool,DB1.DBX0.0是变量的具体地址
    22. var result2 = plc.Read(pumpaddr1); //水泵1状态
    23. Pump1State = bool.Parse(result2.ToString());
    24. if (!Pump1State)
    25. {
    26. WaterDirection1 = "NONE";//停止流动
    27. }
    28. else
    29. {
    30. WaterDirection1 = "EW";//开始流动
    31. }
    32. result2 = plc.Read(pumpaddr2);//水泵2状态
    33. Pump2State = bool.Parse(result2.ToString());
    34. if (!Pump3State)
    35. {
    36. WaterDirection2 = "NONE";
    37. }
    38. else
    39. {
    40. WaterDirection2 = "EW";
    41. }
    42. result2 = plc.Read(pumpaddr3); //水泵3状态
    43. Pump3State = bool.Parse(result2.ToString());
    44. if (!Pump3State)
    45. {
    46. WaterDirection3 = "NONE";
    47. WaterDirection32 = "NONE";
    48. }
    49. else
    50. {
    51. WaterDirection3 = "EW";
    52. WaterDirection32 = "WE";
    53. }
    54. }
    55. }, cts.Token)
    56. );
    57. }

    4、完善控件数据绑定

    1. "West.HeatExchange.Views.MainWindow"
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    6. xmlns:local="clr-namespace:West.HeatExchange.Views"
    7. mc:Ignorable="d"
    8. xmlns:c="clr-namespace:West.HeatExchange.Controls"
    9. xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
    10. WindowStyle="None" AllowsTransparency="True" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip"
    11. Background="#F7F9FA" FontFamily="Microsoft YaHei" Foreground="#333"
    12. Title="MainWindow" Height="650" Width="1200" >
    13. "Top" >
    14. "48"/>
    15. "Auto"/>
    16. "Auto"/>
    17. "50"/>
    18. "45" Grid.Column="0" Background="#3BBAFF" BorderBrush="#EEE" BorderThickness="1" HorizontalAlignment="Left">
    19. "../Assets/Images/Logo_white.png" Margin="8"/>
    20. "4" Grid.Column="1" Background="#5E7593" MouseLeftButtonDown="Border_MouseLeftButtonDown"/>
    21. "换热站远程监控系统" VerticalAlignment="Center" Foreground="White" FontSize="19" Margin="10,0" Grid.Column="1"/>
    22. "{Binding NowTime, StringFormat={}{0:yyyy年MM月dd日HH时mm分ss秒}}" VerticalAlignment="Center" Foreground="White" FontSize="19" Margin="20,0" Grid.Column="3"/>
    23. "../Assets/Images/closewin.png"/>
    24. "Left" Width="280" Background="White" CornerRadius="3" Margin="30,10,10,10" >
    25. "10" ShadowDepth="0" Color="LightGray" Opacity="0.3"/>
    26. "20,10">
    27. "50"/>
    28. "1.2*"/>
    29. "1.2*"/>
    30. "0" Background="#3BBAFF" CornerRadius="3" Margin="0,8">
    31. "Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
    32. "23" Height="23" Margin="10,0"/>
    33. "换热站综合信息详情" Foreground="White" FontSize="12" VerticalAlignment="Center"/>
    34. "1" Background="#FFF0F4F8" Margin="0,5">
    35. "16" VerticalAlignment="Bottom" Background="#FF0E3A52" CornerRadius="8"/>
    36. "True">
    37. "29,225,380,97,440,129,532" LineSmoothness="0" Fill="Transparent"/>
    38. "周一,周二,周三,周四,周五,周六,周日" FontSize="8" Foreground="White">
    39. "0" Step="1"/>
    40. "0" MaxValue="600" Foreground="Transparent">
    41. "#3BBAFF" VerticalAlignment="Top" HorizontalAlignment="Left">
    42. "历史曲线" Foreground="White" FontSize="10" Margin="5,1"/>
    43. "2" Background="#FFF0F4F8" Margin="0,5">
    44. "1">
    45. "Horizontal" VerticalAlignment="Top">
    46. "#FF3BBAFF">
    47. "能耗排名" Foreground="White" FontSize="10" Margin="5,1"/>
    48. "10" VerticalAlignment="Center" Margin="5,0">
    49. "耗水" Foreground="#666"/>
    50. "" FontFamily="../Assets/Fonts/#iconfont"/>
    51. "10" VerticalAlignment="Center">
    52. "耗电" Foreground="#666"/>
    53. "" FontFamily="../Assets/Fonts/#iconfont"/>
    54. "10" VerticalAlignment="Center" Margin="5,0">
    55. "耗热" Foreground="#666"/>
    56. "" FontFamily="../Assets/Fonts/#iconfont"/>
    57. "0,0,10,0">
    58. "20"/>
    59. "" FontFamily="../Assets/Fonts/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF3BBAFF"/>
    60. "1" Minimum="0" Maximum="200" Value="182" Background="LightGray" Foreground="Orange" Height="4"/>
    61. "0,0,10,0">
    62. "20"/>
    63. "" FontFamily="../Assets/Fonts/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF3BBAFF"/>
    64. "1" Minimum="0" Maximum="200" Value="156" Background="LightGray" Foreground="Orange" Height="4"/>
    65. "0,0,10,0">
    66. "20"/>
    67. "" FontFamily="../Assets/Fonts/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF3BBAFF"/>
    68. "1" Minimum="0" Maximum="200" Value="116" Background="LightGray" Foreground="Orange" Height="4"/>
    69. "5" Margin="10,0">
    70. "0,0" EndPoint="1,0">
    71. "#FF3BBAFF" Offset="0"/>
    72. "#113BBAFF" Offset="1"/>
    73. "3" Background="#FFF0F4F8" Margin="0,5">
    74. "16" VerticalAlignment="Bottom" Background="#FF0E3A52" CornerRadius="8"/>
    75. "True">
    76. "319,223,63,257,335,440,178,223,53,117" MaxColumnWidth="6"/>
    77. "设备1,设备2,设备3,设备4,设备5,设备6,设备7,设备8,设备9,设备10" FontFamily="Microsoft YaHei" FontSize="6" Foreground="White">
    78. "1" StrokeThickness="0"/>
    79. "0" MaxValue="600" Foreground="Transparent">
    80. "1" Stroke="#DDD" Step="760"/>
    81. "#FF3BBAFF" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="2">
    82. "故障统计" Foreground="White" FontSize="10" Margin="5,1"/>
    83. "4" Background="#FFF0F4F8" Margin="0,5">
    84. "40" Width="110" Height="110" DisableAnimations="True" StartingRotationAngle="0" HoverPushOut="0" >
    85. "23" Fill="#2BBF6E" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="锻烧车间">
    86. "37" Fill="#3DDFEE" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="模具车间">
    87. "80" Fill="Red" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="电噴车间">
    88. "60" Fill="Blue" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="组装车间">
    89. "#FF3BBAFF" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="2">
    90. "数据报表" Foreground="White" FontSize="10" Margin="5,1"/>
    91. "80" DockPanel.Dock="Top" Background="White" Margin="0,10,20,0" CornerRadius="3">
    92. "10" ShadowDepth="0" Color="LightGray" Opacity="0.3"/>
    93. "1">
    94. "Center" HorizontalAlignment="Center">
    95. "手动模式" IsChecked="True" Style="{StaticResource ModeButtonStyle}"/>
    96. "自动模式" Style="{StaticResource ModeButtonStyle}"/>
    97. "#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center">
    98. "No.1 补水泵" Margin="20,3" HorizontalAlignment="Center"/>
    99. "1" Margin="10,0">
    100. "15"/>
    101. "0" IsChecked="True" Command="{Binding StartCommand}" CommandParameter="1" Grid.ColumnSpan="2" Style="{StaticResource LeftSwitchButtonStyle}" Name="rb"/>
    102. "1" Grid.ColumnSpan="2" Command="{Binding StopCommand}" CommandParameter="1" Style="{StaticResource RightSwitchButtonStyle}"/>
    103. "#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center">
    104. "No.2 补水泵" Margin="20,3" HorizontalAlignment="Center"/>
    105. "1" Margin="10,0">
    106. "15"/>
    107. "True" Grid.ColumnSpan="2" Command="{Binding StartCommand}" CommandParameter="2" Style="{StaticResource LeftSwitchButtonStyle}"/>
    108. "1" Grid.ColumnSpan="2" Command="{Binding StopCommand}" CommandParameter="2" Style="{StaticResource RightSwitchButtonStyle}"/>
    109. "#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center">
    110. "No.3 补水泵" Margin="20,3" HorizontalAlignment="Center"/>
    111. "1" Margin="10,0">
    112. "15"/>
    113. "True" Grid.ColumnSpan="2" Command="{Binding StartCommand}" CommandParameter="3" Style="{StaticResource LeftSwitchButtonStyle}"/>
    114. "1" Grid.ColumnSpan="2" Command="{Binding StopCommand}" CommandParameter="3" Style="{StaticResource RightSwitchButtonStyle}"/>
    115. "#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center">
    116. "循环变频故障" Grid.Row="0" Style="{StaticResource WarningButtonStyle}" IsChecked="True"/>
    117. "补水变频故障" Grid.Row="1" Style="{StaticResource WarningButtonStyle}"/>
    118. "White" Margin="0,10,20,10" CornerRadius="3" >
    119. "10" ShadowDepth="0" Color="LightGray" Opacity="0.3"/>
    120. "7" Width="839" Direction="EW" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="10" Canvas.Top="44"/>
    121. "middle" Panel.ZIndex="3" Canvas.Left="234" Canvas.Top="49" Direction="EW" Height="7" Width="182" CapRadius="3">
    122. "90" >
    123. "7" Width="619" Direction="EW" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="230" Canvas.Top="224"/>
    124. "middle2" Panel.ZIndex="3" Canvas.Left="164" Canvas.Top="139.5" Direction="{Binding WaterDirection2}" Height="7" Width="185" CapRadius="3">
    125. "90" >
    126. "7" Width="399" Direction="{Binding WaterDirection2}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="450" Canvas.Top="296" />
    127. "right" Panel.ZIndex="3" Margin="0" Canvas.Left="223" Canvas.Top="302" Width="131" Direction="{Binding WaterDirection32}" Height="7" CapRadius="1">
    128. "{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Canvas}, Path=ActualHeight}" Y="0">
    129. "90" CenterX="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Canvas}, Path=ActualHeight}" CenterY="0">
    130. "7" Width="79" Direction="{Binding WaterDirection3}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="640" Canvas.Top="426" />
    131. "7" Width="79" Direction="{Binding WaterDirection3}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="540" Canvas.Top="447" />
    132. "7.5" Width="409" Direction="{Binding WaterDirection1}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="10" Canvas.Top="137"/>
    133. "7.5" Width="259" Direction="{Binding WaterDirection2}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="158" Canvas.Top="317"/>
    134. "7.5" Width="459" Direction="{Binding WaterDirection3}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="10" Canvas.Top="447"/>
    135. "7.5" Width="399" Direction="{Binding WaterDirection1}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="450" Canvas.Top="116"/>
    136. "../Assets/Images/d1.jpg" Canvas.Left="35" Canvas.Top="29" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    137. "../Assets/Images/d1.jpg" Canvas.Left="755" Canvas.Top="29" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    138. "../Assets/Images/d1.jpg" Canvas.Left="755" Canvas.Top="279" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    139. "../Assets/Images/d1.jpg" Panel.ZIndex="3" Canvas.Left="95" Canvas.Top="429" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    140. "../Assets/Images/heat_exchange.jpg" Panel.ZIndex="2" Canvas.Left="264" Canvas.Top="20" Height="156" Width="80" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    141. "../Assets/Images/heat_exchange.jpg" Panel.ZIndex="2" Canvas.Left="264" Canvas.Top="200" Height="156" Width="80" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    142. "../Assets/Images/i2.jpg" Canvas.Left="164" Canvas.Top="13" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    143. "../Assets/Images/i2.jpg" Canvas.Left="424" Canvas.Top="13" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    144. "../Assets/Images/i2.jpg" Canvas.Left="524" Canvas.Top="85" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    145. "../Assets/Images/i2.jpg" Canvas.Left="684" Canvas.Top="193" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    146. "../Assets/Images/i2.jpg" Canvas.Left="584" Canvas.Top="265" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    147. "../Assets/Images/f2.jpg" Panel.ZIndex="2" Canvas.Left="64" Canvas.Top="115" Height="33" Width="34" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    148. "../Assets/Images/f2.jpg" Panel.ZIndex="2" Canvas.Left="224" Canvas.Top="425" Height="33" Width="34" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    149. -3个水泵-->
    150. "42" Height="38" IsRunning="{Binding Pump1State}" Canvas.Left="411" Canvas.Top="111" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    151. "42" Height="38" IsRunning="{Binding Pump2State}" Canvas.Left="411" Canvas.Top="291" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    152. "42" Height="38" IsRunning="{Binding Pump3State}" Canvas.Left="611" Canvas.Top="421" HorizontalAlignment="Center" VerticalAlignment="Top"/>
    153. "75" Height="40" Canvas.Left="30" Canvas.Top="71" ItemsSource="{Binding MainData.MomentDataList1}"/>
    154. "75" Height="40" Canvas.Left="135" Canvas.Top="56" ItemsSource="{Binding MainData.MomentDataList1}"/>
    155. "75" Height="40" Canvas.Left="545" Canvas.Top="71" ItemsSource="{Binding MainData.MomentDataList1}"/>
    156. "75" Height="40" Canvas.Left="495" Canvas.Top="252" ItemsSource="{Binding MainData.MomentDataList2}"/>
    157. "75" Height="40" Canvas.Left="595" Canvas.Top="178" ItemsSource="{Binding MainData.MomentDataList2}"/>
    158. "75" Height="40" Canvas.Left="90" Canvas.Top="383" ItemsSource="{Binding MainData.MomentDataList3}"/>
    159. "Normal" Width="140" Height="150" Canvas.Left="415" Canvas.Top="347">

     5、运行效果

    程序启动后,暂时没有数据

    5秒后,数据实时显示,温度压力状态数据与监控表中的数据一致的 

    关闭水泵1,观察管道和风扇情况及监控表数值

     

     

    开启水泵1,管道流动了,风扇转动了 ,PLC数据改变了

     至此,PLC与系统完美对接,实现了全场监控管理。

    走过路过不要错过,点赞关注收藏又圈粉,共同致富,为财务自由作出贡献

  • 相关阅读:
    通过逻辑回归和随机梯度下降法对乳腺癌数据集breastCancer和鸢尾花数据集iris进行线性分类
    无协同资源创新打法,这几个品牌在双11「品牌嘉年华」实现品效双收
    后缀自动机(SAM)——黑盒使用方案
    7天酒店亮相“2022锦江行”17年国民历史酒店再启新程
    02、MongoDB -- MongoDB 的安全配置(创建用户、设置用户权限、启动安全控制、操作数据库命令演示、mongodb 的帮助系统介绍)
    UI设计开发原则
    Linux系统编程_进程间通信第1天:IPC、无名管道pipe和命名管道mkfifo(半双工)、消息队列msgget(全双工)
    算法 - 计数排序(Counting_Sort)
    美国上周初请人数23.2万人是两个月最低水平 美联储加息75基点稳了
    Nacos注册中心和服务消费方式
  • 原文地址:https://blog.csdn.net/hqwest/article/details/139755928