手摸手教你撕碎西门子S7通讯协议17--【再爆肝】通讯库应用开发wpf版

news2025/1/13 16:48:06

1、先看颜值

这颜值是采用wpf渲染技术实现的,里面用到很多控件,有第三方控件,也有自定义控件,怎么样,比车模还漂亮吧,超过脸模。

2、实现思路


程序启动时,连接西门子PLC,然后主动读取PLC参数数据,实时显示信息,界面上有控制开关,点击可实现PLC的停止和启动,当停止后,水泵不转动,又可以点击开,启动PLC,这里水泵又转动,同时,水管的水是跟着水泵状态一起变化的 ,界面颜值还是不错的,加上功能完善,得到了爽歪歪的效果,这个实现过程就用到自己开发的通讯库,实现PLC数据的读写。

3、 实现过程

1、创建WPF项目

2、设计UI

<Window x:Class="West.HeatExchange.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:West.HeatExchange.Views"
        mc:Ignorable="d"
        xmlns:c="clr-namespace:West.HeatExchange.Controls"
        xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
        WindowStyle="None" AllowsTransparency="True" WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip"
        Background="#F7F9FA" FontFamily="Microsoft YaHei" Foreground="#333"
        Title="MainWindow" Height="650" Width="1200"  >
    <!--窗体资源-->
    <Window.Resources>
        <!--手动模式/自动模式单选控件样式-->
        <Style TargetType="RadioButton" x:Key="ModeButtonStyle">
            <Setter Property="Background" Value="#FFF0F4F8"/>
            <Setter Property="Foreground" Value="Gray"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <Border Background="{TemplateBinding Background}" Name="bor">
                            <ContentPresenter Margin="30,5"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <!--触发器-->
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="#FF3BBAFF"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <!--水泵开样式-->
        <Style TargetType="RadioButton" x:Key="LeftSwitchButtonStyle">
            <Setter Property="Background" Value="#FFF0F4F8"/>
            <Setter Property="Foreground" Value="Gray"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <Grid>
                            <!--绘制下划线-->
                            <Border BorderBrush="Transparent" BorderThickness="0,0,0,1" Height="30" Margin="5,0" Name="bor" Width="20" HorizontalAlignment="Left"/>
                            <!--绘制曲线-->
                            <Path Data="M3 0 45 0 48 3 28 20 3 20 0 17 0 3z" Stroke="#DDD" StrokeThickness="1" Fill="{TemplateBinding Background}" Margin="0,0,2,0" VerticalAlignment="Center"/>
                            <TextBlock Text="开" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="10" Margin="0,0,15,0"/>
                        </Grid>
                        <!--触发器-->
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="bor" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="bor" Property="BorderBrush" Value="#FF3BBAFF"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="#FF3BBAFF"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <!--水泵关样式-->
        <Style TargetType="RadioButton" x:Key="RightSwitchButtonStyle">
            <Setter Property="Background" Value="#FFF0F4F8"/>
            <Setter Property="Foreground" Value="Gray"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="RadioButton">
                        <Grid>
                            <Border BorderBrush="Transparent" BorderThickness="0,0,0,1" Height="30" Margin="8,0" Name="bor" Width="20" HorizontalAlignment="Right"/>
                            <Path Data="M0 17 20 0 45 0 48 3 48 17 45 20 3 20z" Stroke="#DDD" StrokeThickness="1" Fill="{TemplateBinding Background}" Margin="0,0,2,0" VerticalAlignment="Center"/>
                            <TextBlock Text="关" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="10" Margin="10,0,0,0"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="bor" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="bor" Property="BorderBrush" Value="#FF3BBAFF"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="#FF3BBAFF"/>
                    <Setter Property="Foreground" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <!--故障样式-->
        <Style TargetType="CheckBox" x:Key="WarningButtonStyle">
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Opacity" Value="0.2"/>
            <Setter Property="Margin" Value="0,2"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="CheckBox">
                        <Grid>
                            <Border BorderBrush="Red" BorderThickness="1" Padding="1" Name="root">
                                <Border.Background>
                                    <DrawingBrush TileMode="Tile" Viewport="0,0,0.1,0.2" >
                                        <!--画笔填充-->
                                        <DrawingBrush.Drawing>
                                            <GeometryDrawing>
                                                <GeometryDrawing.Pen>
                                                    <Pen Brush="blue" Thickness="10" x:Name="pen"/>
                                                </GeometryDrawing.Pen>
                                                <GeometryDrawing.Geometry>
                                                    <LineGeometry StartPoint="50,0" EndPoint="0,50"/>
                                                </GeometryDrawing.Geometry>
                                            </GeometryDrawing>
                                        </DrawingBrush.Drawing>
                                    </DrawingBrush>
                                </Border.Background>
                                <Border Background="Red" Margin="0,6,0,0" BorderBrush="White" BorderThickness="1" Height="20">
                                    <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="15,0"/>
                                </Border>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Opacity" Value="1"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <DockPanel  Background="DodgerBlue" >
        <!--顶部区域,分5列-->
        <Grid DockPanel.Dock="Top"  >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="48"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="50"/>
            </Grid.ColumnDefinitions>
            <!--图标-->
            <Border Width="45" Grid.Column="0" Background="#3BBAFF" BorderBrush="#EEE"  BorderThickness="1" HorizontalAlignment="Left">
                <Image Source="../Assets/Images/f1.jpg" Margin="8"/>
            </Border>
            <!--背景-->
            <Border Grid.ColumnSpan="4"  Grid.Column="1"   Background="#5E7593"  MouseLeftButtonDown="Border_MouseLeftButtonDown"/>
            <!--文字-->
            <TextBlock Text="搪瓷车间运行环境数字屏" VerticalAlignment="Center" Foreground="White" FontSize="22" Margin="10,0" Grid.Column="1"/>
            <!--时间-->
            <TextBlock  Text="{Binding NowTime, StringFormat={}{0:yyyy年MM月dd日HH时mm分ss秒}}" VerticalAlignment="Center"  Foreground="White" FontSize="19"  Margin="20,0" Grid.Column="3"/>
            <!--退出按钮-->
            <Button   Grid.Column="4" Width="25" Height="25"  Click="Button_Click">
                <Button.Background>
                    <ImageBrush ImageSource="../Assets/Images/closewin.png"/>
                </Button.Background>
            </Button>

        </Grid>

        <!--左侧区域,分5行-->
        <Border DockPanel.Dock="Left" Width="280" Background="White" CornerRadius="3" Margin="30,10,10,10" >
            <Border.Effect>
                <DropShadowEffect BlurRadius="10" ShadowDepth="0" Color="LightGray" Opacity="0.3"/>
            </Border.Effect>
            <Grid Margin="20,10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"/>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition Height="1.2*"/>
                    <RowDefinition Height="1.2*"/>
                </Grid.RowDefinitions>
                <!--第1行,文字栏-->
                <Border Grid.Row="0" Background="#3BBAFF" CornerRadius="3" Margin="0,8">
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <c:RunLoading Width="23" Height="23" Margin="10,0"/>
                        <TextBlock Text="换热站综合信息详情" Foreground="White" FontSize="12" VerticalAlignment="Center"/>
                    </StackPanel>
                </Border>

                <!--第2行,历史曲线-->
                <Grid Grid.Row="1" Background="#FFF0F4F8" Margin="0,5">
                    <Border Height="16" VerticalAlignment="Bottom" Background="#FF0E3A52" CornerRadius="8"/>
                    <!--livechart图表配置,Values绑定序列数据-->
                    <lvc:CartesianChart DisableAnimations="True">
                        <lvc:CartesianChart.Series>
                            <lvc:LineSeries Values="29,225,380,97,440,129,532" LineSmoothness="0"  Fill="Transparent"/>
                        </lvc:CartesianChart.Series>
                        <!--X轴配置-->
                        <lvc:CartesianChart.AxisX>
                            <lvc:Axis Labels="周一,周二,周三,周四,周五,周六,周日" FontSize="8" Foreground="White">
                                <lvc:Axis.Separator>
                                    <lvc:Separator StrokeThickness="0" Step="1"/>
                                </lvc:Axis.Separator>
                            </lvc:Axis>
                        </lvc:CartesianChart.AxisX>
                        <!--Y轴配置-->
                        <lvc:CartesianChart.AxisY>
                            <lvc:Axis MinValue="0" MaxValue="600" Foreground="Transparent"></lvc:Axis>
                        </lvc:CartesianChart.AxisY>
                    </lvc:CartesianChart>
                    <Border Background="#3BBAFF" VerticalAlignment="Top" HorizontalAlignment="Left">
                        <TextBlock Text="历史曲线" Foreground="White" FontSize="10" Margin="5,1"/>
                    </Border>
                </Grid>

                <!--第3行,能耗排名-->
                <Grid Grid.Row="2" Background="#FFF0F4F8" Margin="0,5">
                    <UniformGrid Columns="1">
                        <!--文字部分-->
                        <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
                            <Border Background="#FF3BBAFF">
                                <TextBlock Text="能耗排名" Foreground="White" FontSize="10" Margin="5,1"/>
                            </Border>
                            <TextBlock FontSize="10" VerticalAlignment="Center" Margin="5,0">
                                <Run Text="耗水" Foreground="#666"/>
                                <Run Text="&#xe92e;" FontFamily="../Assets/Fonts/#iconfont"/>
                            </TextBlock>
                            <TextBlock FontSize="10" VerticalAlignment="Center">
                                <Run Text="耗电" Foreground="#666"/>
                               <Run Text="&#xe8b6;" FontFamily="../Assets/Fonts/#iconfont"/>
                            </TextBlock>
                            <TextBlock  FontSize="10" VerticalAlignment="Center" Margin="5,0">
                                <Run Text="耗热" Foreground="#666"/>
                                <Run Text="&#xe60e;" FontFamily="../Assets/Fonts/#iconfont"/>
                            </TextBlock>
                        </StackPanel>
                        <!--耗水-->
                        <Grid Margin="0,0,10,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="&#xe92e;" FontFamily="../Assets/Fonts/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF3BBAFF"/>
                            <ProgressBar Grid.Column="1" Minimum="0" Maximum="200" Value="182" Background="LightGray" Foreground="Orange" Height="4"/>
                        </Grid>
                        <!--耗电-->
                        <Grid Margin="0,0,10,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="&#xe8b6;" FontFamily="../Assets/Fonts/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF3BBAFF"/>
                            <ProgressBar Grid.Column="1" Minimum="0" Maximum="200" Value="156" Background="LightGray" Foreground="Orange" Height="4"/>
                        </Grid>
                        <!--耗热-->
                        <Grid Margin="0,0,10,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="20"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="&#xe60e;" FontFamily="../Assets/Fonts/#iconfont" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF3BBAFF"/>
                            <ProgressBar Grid.Column="1" Minimum="0" Maximum="200" Value="116" Background="LightGray" Foreground="Orange" Height="4"/>
                        </Grid>
                        <!--装饰-->
                        <Border Height="5" Margin="10,0">
                            <Border.Background>
                                <!--线性渐变-->
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
                                    <GradientStop Color="#FF3BBAFF" Offset="0"/>
                                    <GradientStop Color="#113BBAFF" Offset="1"/>
                                </LinearGradientBrush>
                            </Border.Background>
                        </Border>
                    </UniformGrid>
                </Grid>

                <!--第4行,故障统计-->
                <Grid Grid.Row="3" Background="#FFF0F4F8" Margin="0,5">
                    <Border Height="16" VerticalAlignment="Bottom" Background="#FF0E3A52" CornerRadius="8"/>
                    <lvc:CartesianChart DisableAnimations="True">
                        <lvc:CartesianChart.Series>
                            <lvc:ColumnSeries Values="319,223,63,257,335,440,178,223,53,117" MaxColumnWidth="6"/>
                        </lvc:CartesianChart.Series>
                        <lvc:CartesianChart.AxisX>
                            <lvc:Axis Labels="设备1,设备2,设备3,设备4,设备5,设备6,设备7,设备8,设备9,设备10" FontFamily="Microsoft YaHei" FontSize="6" Foreground="White">
                                <lvc:Axis.Separator>
                                    <lvc:Separator Step="1" StrokeThickness="0"/>
                                </lvc:Axis.Separator>
                            </lvc:Axis>
                        </lvc:CartesianChart.AxisX>
                        <lvc:CartesianChart.AxisY>
                            <lvc:Axis MinValue="0" MaxValue="600" Foreground="Transparent">
                                <lvc:Axis.Separator>
                                    <lvc:Separator StrokeThickness="1" Stroke="#DDD" Step="760"/>
                                </lvc:Axis.Separator>
                            </lvc:Axis>
                        </lvc:CartesianChart.AxisY>
                    </lvc:CartesianChart>
                    <Border Background="#FF3BBAFF" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="2">
                        <TextBlock Text="故障统计" Foreground="White" FontSize="10" Margin="5,1"/>
                    </Border>
                </Grid>

                <!--第5行,数据报表-->
                <Grid Grid.Row="4" Background="#FFF0F4F8" Margin="0,5">
                    <lvc:PieChart  InnerRadius="40" Width="110" Height="110"  DisableAnimations="True" StartingRotationAngle="0" HoverPushOut="0"  >
                        <!--以下是静态显示-->
                        <lvc:PieChart.Series>
                            <lvc:PieSeries  Values="23"  Fill="#2BBF6E" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="锻烧车间"></lvc:PieSeries>
                            <lvc:PieSeries  Values="37" Fill="#3DDFEE" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="模具车间"></lvc:PieSeries>
                            <lvc:PieSeries  Values="80" Fill="Red" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="电噴车间"></lvc:PieSeries>
                            <lvc:PieSeries  Values="60" Fill="Blue" StrokeThickness="2" DataLabels="True" Foreground="Black" FontSize="11" LabelPosition="OutsideSlice" Title="组装车间"></lvc:PieSeries>
                        </lvc:PieChart.Series>
                    </lvc:PieChart>
                    <Border Background="#FF3BBAFF" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="2">
                        <TextBlock Text="数据报表" Foreground="White" FontSize="10" Margin="5,1"/>
                    </Border>
                </Grid>

            </Grid>
        </Border>

        <!--右上区域,控制模式-->
        <Border Height="80" DockPanel.Dock="Top" Background="White" Margin="0,10,20,0" CornerRadius="3">
            <Border.Effect>
                <DropShadowEffect BlurRadius="10" ShadowDepth="0" Color="LightGray" Opacity="0.3"/>
            </Border.Effect>
            <UniformGrid Rows="1">
                <!--<Button Content="连接PLC" Width="60" Height="40" Click="Button_Click_1" ></Button>-->
                <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                    <RadioButton Content="手动模式" IsChecked="True" Style="{StaticResource ModeButtonStyle}"/>
                    <RadioButton Content="自动模式" Style="{StaticResource ModeButtonStyle}"/>
                </StackPanel>
                <!--补水泵1-->
                <Grid Background="#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="50 0 0 0">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="No.1 补水泵" Margin="20,3" HorizontalAlignment="Center"/>
                    <Grid Grid.Row="1" Margin="10,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="15"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <RadioButton Grid.Column="0" IsChecked="True"  Command="{Binding StartCommand}" CommandParameter="1" Grid.ColumnSpan="2"  Style="{StaticResource LeftSwitchButtonStyle}" Name="rb"/>
                        <RadioButton Grid.Column="1" Grid.ColumnSpan="2" Command="{Binding StopCommand}"  CommandParameter="1" Style="{StaticResource RightSwitchButtonStyle}"/>
                    </Grid>
                </Grid>
                <!--补水泵2-->
                <Grid Background="#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="50 0 0 0">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <TextBlock Text="No.2 补水泵" Margin="20,3" HorizontalAlignment="Center"/>
                    <Grid Grid.Row="1" Margin="10,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="15"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <RadioButton IsChecked="True" Grid.ColumnSpan="2" Command="{Binding StartCommand}" CommandParameter="2" Style="{StaticResource LeftSwitchButtonStyle}"/>
                        <RadioButton Grid.Column="1" Grid.ColumnSpan="2" Command="{Binding StopCommand}"  CommandParameter="2" Style="{StaticResource RightSwitchButtonStyle}"/>
                    </Grid>
                </Grid>
               
                <!--故障切换--> 
                <Grid Background="#88F0F4F8" VerticalAlignment="Center" HorizontalAlignment="Center">
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <CheckBox Content="循环变频故障" Grid.Row="0" Style="{StaticResource WarningButtonStyle}" IsChecked="True"/>
                    <CheckBox Content="补水变频故障" Grid.Row="1" Style="{StaticResource WarningButtonStyle}"/>
                </Grid>
            </UniformGrid>
        </Border>

        <!--右中区域,系统运行-->
        <Border Background="White" Margin="0,10,20,10" CornerRadius="3" >
            <Border.Effect>
                <DropShadowEffect BlurRadius="10" ShadowDepth="0" Color="LightGray" Opacity="0.3"/>
            </Border.Effect>
            <Canvas>
                <!--管道--> 
                <c:PipeLine Height="7" Width="839" Direction="EW" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="10" Canvas.Top="44"/>
                <c:PipeLine x:Name="middle" Panel.ZIndex="3" Canvas.Left="234" Canvas.Top="49"   Direction="EW"   Height="7" Width="182" CapRadius="3">
                    <c:PipeLine.RenderTransform>
                        <TransformGroup>
                            <RotateTransform Angle="90" ></RotateTransform>
                        </TransformGroup>
                    </c:PipeLine.RenderTransform>
                </c:PipeLine>
                <c:PipeLine Height="7" Width="619" Direction="EW" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="230" Canvas.Top="224"/>
                <c:PipeLine x:Name="middle2" Panel.ZIndex="3" Canvas.Left="164" Canvas.Top="139.5"   Direction="{Binding WaterDirection2}"   Height="7" Width="185" CapRadius="3">
                    <c:PipeLine.RenderTransform>
                        <TransformGroup>
                            <RotateTransform Angle="90" ></RotateTransform>
                        </TransformGroup>
                    </c:PipeLine.RenderTransform>
                </c:PipeLine>
                <c:PipeLine Height="7" Width="399" Direction="{Binding WaterDirection2}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="450" Canvas.Top="296"  />
                <c:PipeLine x:Name="right" Panel.ZIndex="3" Margin="0" Canvas.Left="223" Canvas.Top="302" Width="152" Direction="{Binding WaterDirection32}"  Height="7" CapRadius="1">
                    <c:PipeLine.RenderTransform>
                        <TransformGroup>
                            <TranslateTransform X="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Canvas}, Path=ActualHeight}" Y="0"></TranslateTransform>
                            <RotateTransform Angle="90" CenterX="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Canvas}, Path=ActualHeight}" CenterY="0"></RotateTransform>
                        </TransformGroup>
                    </c:PipeLine.RenderTransform>
                </c:PipeLine>
               
                <c:PipeLine Height="7" Width="179" Direction="{Binding WaterDirection3}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="540" Canvas.Top="447"  />
                <c:PipeLine Height="7.5"  Width="409" Direction="{Binding WaterDirection1}"  HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="10" Canvas.Top="137"/>
                <c:PipeLine Height="7.5"  Width="259" Direction="{Binding WaterDirection2}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="158" Canvas.Top="317"/>
                <c:PipeLine Height="7.5"  Width="459" Direction="{Binding WaterDirection3}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="10" Canvas.Top="447"/>
                <c:PipeLine Height="7.5" Width="399" Direction="{Binding WaterDirection1}" HorizontalAlignment="Left" VerticalAlignment="Center" Canvas.Left="450" Canvas.Top="116"/>
                <!--图片-->
                <Image Source="../Assets/Images/d1.jpg" Canvas.Left="35" Canvas.Top="29" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/d1.jpg" Canvas.Left="755" Canvas.Top="29" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/d1.jpg" Canvas.Left="755" Canvas.Top="279" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/d1.jpg" Panel.ZIndex="3" Canvas.Left="95" Canvas.Top="429" Height="39" Width="61" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/heat_exchange.jpg" Panel.ZIndex="2" Canvas.Left="264" Canvas.Top="20" Height="156" Width="80" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/heat_exchange.jpg" Panel.ZIndex="2" Canvas.Left="264" Canvas.Top="200" Height="156" Width="80" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/i2.jpg" Canvas.Left="164" Canvas.Top="13" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/i2.jpg" Canvas.Left="424" Canvas.Top="13" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/i2.jpg" Canvas.Left="524" Canvas.Top="85" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/i2.jpg" Canvas.Left="684" Canvas.Top="193" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/i2.jpg" Canvas.Left="584" Canvas.Top="265" Height="31" Width="25" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/f2.jpg" Panel.ZIndex="2" Canvas.Left="64" Canvas.Top="115" Height="33" Width="34" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <Image Source="../Assets/Images/f2.jpg" Panel.ZIndex="2" Canvas.Left="224" Canvas.Top="425" Height="33" Width="34" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                  <!--3个水泵-->
                <c:Pump Width="42" Height="38" IsRunning="{Binding Pump1State}" Canvas.Left="411" Canvas.Top="111" HorizontalAlignment="Center" VerticalAlignment="Top"/>
                <c:Pump Width="42" Height="38" IsRunning="{Binding Pump2State}" Canvas.Left="411" Canvas.Top="291" HorizontalAlignment="Center" VerticalAlignment="Top"/>
              
                 <!--数据看板-->
                <c:DataBoard Width="75" Height="40" Canvas.Left="30" Canvas.Top="71" ItemsSource="{Binding MainData.MomentDataList1}"/>
                <c:DataBoard Width="75" Height="40" Canvas.Left="135" Canvas.Top="56" ItemsSource="{Binding MainData.MomentDataList1}"/>
                <c:DataBoard Width="75" Height="40" Canvas.Left="545" Canvas.Top="71" ItemsSource="{Binding MainData.MomentDataList1}"/> 
                <c:DataBoard Width="75" Height="40" Canvas.Left="595" Canvas.Top="178" ItemsSource="{Binding MainData.MomentDataList2}"/>
                <c:DataBoard Width="75" Height="40" Canvas.Left="495" Canvas.Top="252" ItemsSource="{Binding MainData.MomentDataList2}"/> 
                <c:CoolingTower  RunningState="Normal" Width="140" Height="150"  Canvas.Left="415" Canvas.Top="347"></c:CoolingTower>
            </Canvas>
        </Border>
    </DockPanel>

</Window>

 3、ViewModel模型

using LiveCharts.Maps;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using West.HeatExchange.Base;
using West.HeatExchange.Controls;
using West.HeatExchange.Models;

namespace West.HeatExchange.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        CancellationTokenSource cts = new CancellationTokenSource();
        List<Task> tasks = new List<Task>();
        //水泵1地址
        readonly string pumpaddr1 = "DB1.DBX0.0";
        //水泵2地址
        readonly string pumpaddr2 = "DB1.DBX0.1"; 
        //数据看板变量
        public MainModel MainData { get; set; } = new MainModel();
        //Plc对象
        S7.Net.Plc plc = new S7.Net.Plc(S7.Net.CpuType.S71500, "192.168.1.66", 0, 1);
        private DateTime nowTime = DateTime.Now;

        public MainViewModel()
        { 
            ChangeTime();
            ReadmyPLC();
        }

        #region 方法
        private void ReadmyPLC()
        {
            tasks.Add(Task.Run(async () =>
            { 
                plc.Open();
                while (!cts.IsCancellationRequested)
                {
                    await Task.Delay(5000);
                    //读取float,从左到右参数依次是:数据块类型,数据块号,启始地址,变量类型,读取数量
                    var result = plc.Read(S7.Net.DataType.DataBlock, 1, 6, S7.Net.VarType.Real, 4);
                    float[] values = (float[])result; 
                    //水泵1数据
                    MainData.MomentDataList1[0].Value = values[0]; 
                    MainData.MomentDataList1[1].Value = values[1];
                    //水泵2数据
                    MainData.MomentDataList2[0].Value = values[2];
                    MainData.MomentDataList2[1].Value = values[3]; 

                    //读取bool,DB1.DBX0.0是变量的具体地址
                    var result2 = plc.Read(pumpaddr1);   //水泵1状态 
                    Pump1State = bool.Parse(result2.ToString());
                    if (!Pump1State)
                    {
                        WaterDirection1 = "NONE";//停止流动
                    }
                    else
                    {
                        WaterDirection1 = "EW";//开始流动
                    }
                    result2 = plc.Read(pumpaddr2);//水泵2状态 
                    Pump2State = bool.Parse(result2.ToString()); 
                }
            }, cts.Token)
            );
        } 
        private void ChangeTime()
        {
            //每隔1秒更新时间
            Task.Run(() => {
                while (true)
                {
                    Thread.Sleep(1000);
                    string txtDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    NowTime = Convert.ToDateTime(txtDate);
                }
            });
        }

        /// <summary>
        /// 启动或停止水泵
        /// </summary>
        /// <param name="addr"></param>
        private void Changepumpstate(string addr)
        {
            try
            {
                var obj = plc.Read(addr);
                bool flag = (bool)obj;
                if (flag)
                {
                    flag = false;
                }
                else
                {
                    flag = true;
                }
                plc.Write(addr, flag);
            }
            catch
            {
                MessageBox.Show("写入失败");
            }
        }
        #endregion

        #region 属性
        /// <summary>
        /// 系统时间
        /// </summary>
        public DateTime NowTime
        {
            get { return nowTime; }
            set
            {
                nowTime = value;
                OnPropertyChanged();
            }
        }
        private bool pump1State = true;
        /// <summary>
        ///  水泵1状态 
        /// </summary>
        public bool Pump1State
        {
            get
            {
                return pump1State;
            }
            set
            {
                pump1State = value;
                OnPropertyChanged();
            }
        }
        private bool pump2State = true;
        /// <summary>
        ///  水泵2状态 
        /// </summary>
        public bool Pump2State
        {
            get
            {
                return pump2State;
            }
            set
            {
                pump2State = value;
                OnPropertyChanged();
            }
        }
       
        private string waterDirection1 = "EW";
        /// <summary>
        ///  水泵1水流状态 
        /// </summary>
        public string WaterDirection1
        {
            get
            {
                return waterDirection1;
            }
            set
            {
                waterDirection1 = value;
                OnPropertyChanged();
            }
        }
        private string waterDirection2 = "EW";
        /// <summary>
        ///  水泵2水流状态 
        /// </summary>
        public string WaterDirection2
        {
            get
            {
                return waterDirection2;
            }
            set
            {
                waterDirection2 = value;
                OnPropertyChanged();
            }
        }
       
         
        #endregion  

        #region 命令
        /// </summary>
        /// <summary>
        /// 关按钮的命令处理
        /// </summary>
        public ICommand StopCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    //获取传递过来的参数值
                    string s = o.ToString();
                    //根据不同的值区分是哪个关的按钮
                    switch (s)
                    {
                        case "1":
                            //Pump1State = false;
                            //WaterDirection1 = "NONE"; 
                            Changepumpstate(pumpaddr1);
                            break;
                        case "2":
                            //Pump2State = false;
                            //WaterDirection2 = "NONE";
                            Changepumpstate(pumpaddr2);
                            break; 
                    } 
                });
            }
        }

        /// <summary>
        /// 开按钮命令的处理
        /// </summary>
        public ICommand StartCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    string s = o.ToString();
                    switch (s)
                    {
                        case "1":
                            //Pump1State = true;
                            //WaterDirection1 = "EW";
                            Changepumpstate(pumpaddr1);
                            break;
                        case "2":
                            //Pump2State = true;
                            //WaterDirection2 = "EW";
                            Changepumpstate(pumpaddr2);
                            break; 
                    }
                });
            }
        }
        #endregion

    }
}

4、运行程序 

 4、小结

人无我有,人有我精,人精我强,强国工程,从我做起,为中国式的现代化添砖加瓦

原创不易,打字截图不易,走过路过,不要错过,欢迎点赞,收藏,转载,复制,抄袭,留言,动动你的金手指,早日实现财务自由

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1968490.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

计算机基础(Windows 10+Office 2016)教程 —— 第5章 文档编辑软件Word 2016(下)

文档编辑软件Word 2016 5.4 Word 2016的表格应用5.4.1 创建表格5.4.2 编辑表格5.4.3 设置表格 5.5 Word 2016的图文混排5.5.1 文本框操作5.5.2 图片操作5.5.3 形状操作5.5.4 艺术字操作 5.6 Word 2016的页面格式设置5.6.1 设置纸张大小、页面方向和页边距5.6.2 设置页眉、页脚和…

【通俗理解】贝叶斯定理——证据如何更新信念

【通俗理解】贝叶斯定理——证据如何更新信念 信念更新的类比 你可以把贝叶斯定理比作一个“信念调节器”&#xff0c;它根据新的证据来调节我们对某一事件发生的信念强度。 贝叶斯定理的核心作用 组件/步骤描述先验概率在获得新证据之前&#xff0c;对某一事件发生的概率的估…

Mac电脑数据恢复软件 Disk Drill 企业版安装

Mac分享吧 文章目录 效果一、下载软件二、开始安装1、双击运行软件&#xff0c;将拖入文件夹中&#xff0c;等待安装完毕2、应用程序显示软件图标&#xff0c;运行软件&#xff0c;点击安装&#xff0c;软件页面打开表示安装成功 三、运行测试1、打开软件&#xff0c;恢复一个…

Jenkins自动化构建运行Springboot项目

通过在Jenkins中创建流水线任务&#xff0c;编写流水线脚本以实现自动化构建和部署SpringBoot项目 好处 自动化: 自动化整个部署流程&#xff0c;减少人工干预&#xff0c;降低出错率。 一致性: 确保每次部署都遵循相同的步骤和配置&#xff0c;提高部署的一致性。 快速反馈…

临沂厚朴里升腾的文旅“烟火气”为城市“夜”经济贡献新活力

“一街兴一城、一街促百业”的案例不胜枚举&#xff0c;一如北京王府井大街古今交融的馥郁人文、上海南京路万国风貌的时尚繁华、成都春熙路美食飘香的热辣滚烫…无不成为其所在城市的一张靓丽名片&#xff0c;吸引着大量的当地居民和外来游客&#xff0c;为城市的经济发展和人…

Matlab|考虑大规模电动汽车接入电网的双层优化调度策略

目录 1 主要内容 2 部分代码 3 程序结果 4 下载链接 1 主要内容 该程序复现文章《A bi-layer optimization based temporal and spatial scheduling for large-scale electric vehicles》&#xff0c;中文文献可对照《考虑大规模电动汽车接入电网的双层优化调度策略》&…

【C++】学习笔记——智能指针

文章目录 二十一、智能指针1. 内存泄漏2. 智能指针的使用及原理RAII智能指针的原理auto_ptrunique_ptrshared_ptrshared_ptr的循环引用weak_ptr删除器 未完待续 二十一、智能指针 1. 内存泄漏 在上一章的异常中&#xff0c;我们了解到如果出现了异常&#xff0c;会中断执行流…

4.5、作业管理

几乎不太会考 作业的状态 作业&#xff1a;系统为完成一个用户的计算任务&#xff08;或一次事务处理&#xff09;所做的工作总和。例如&#xff0c;对用户编写的源程序&#xff0c;需要经过编译、连接、装入以及执行等步骤得到结果&#xff0c;这其中的每一个步骤称为作业步…

【附安装包】CentOS7(Linux)详细安装教程(手把手图文详解版)

目前流行的虚拟机软件有VMware、Virtual Box和Virtual PC等等&#xff0c;其中最常用的就是VMware。 而centos是Linux使用最广泛的版本之一。 教程开始教程有许多不完备之处&#xff0c;大佬请忽略。。。 1.安装VMware 首先需要准备VMware的安装包以及Ubuntu的ISO镜像&#…

Shell编程——基础语法(2)和 Shell流程控制

文章目录 基础语法&#xff08;2&#xff09;echo命令read命令printf命令test命令 Shell流程控制if-else语句for 循环while 语句until 循环case ... esac跳出循环 基础语法&#xff08;2&#xff09; echo命令 Shell 的 echo 指令与 PHP 的 echo 指令类似&#xff0c;都是用于…

文档管理系统哪个好?优质8款系统深度比较

本文将分享8款文档管理系统&#xff1a;PingCode、Worktile、金山文档、腾讯文档、飞书文档、石墨文档、Confluence、Google Drive。 在寻找合适的文档管理系统时&#xff0c;你是否感到困惑和不安&#xff1f;市场上众多选项让人难以抉择&#xff0c;尤其是当你希望找到既能提…

springCloud组件专题(五) --- seata

一.Seata介绍 1. seata是什么 是一款开源的分布式事务解决方案&#xff0c;供了 AT、TCC、SAGA 和 XA 事务模式。 2.分布式事务中的概念 2.1. 二阶段提交 二阶段提交的含义就是将事务的提交分成两个步骤&#xff0c;分别为&#xff1a; 准备阶段&#xff1a;事务协调者询问所…

Django分页组件封装

目录 1. 前言 2. 代码 3. 使用 3.1 view.py 3.2 list.html 1. 前言 在日常开发中&#xff0c;我们也许会遇到一页内容太多不够展示的问题&#xff0c;过于冗余。 此时&#xff0c;我们就需要进行分页&#xff0c;分页的方式有两种&#xff1a;1. ajax异步分页 2. 普通选…

记一些零碎的只是点和一些安全工具的使用(这里建议将漏洞原理搞清楚,然后可以尝试手动和使用工具)

目录 信息收集 扫描端口 工具 nmap TxPortMap tideFinger fscan 漏洞扫描 目录扫描 利群使用 不同系统、不同框架的漏洞 OA weblogic Struts2 thinkphp漏洞 shiro 蚁剑使用 更高级的连接工具 免杀类型 主机端的免杀 流量层的免杀 安全设备 主机端安全设备…

Docker容器数据库启动,如何用别名JAR jdbc:postgresql://别名:5432/postgres

如果想了解为啥这样做得同学&#xff0c;请去看这个文章 Docker容器网络&#xff08;七&#xff09;_host.docker.internal-CSDN博客 因为docker0网络&#xff0c;需要用别名的话&#xff0c;还得在host文件加 dockerIp(172.0.0.2) 别名 怎么查&#xff0c; docker network …

每日一题 ~ LCR 015. 找到字符串中所有字母异位词

. - 力扣&#xff08;LeetCode&#xff09; 题目解析 题目要求找出字符串中所有的字母异位词。所谓字母异位词指的是两个字符串中字符出现的次数相同&#xff0c;但顺序可以不同的情况。 思路分析 固定窗口&#xff1a;使用滑动窗口技巧&#xff0c;窗口大小固定为待匹配字…

Latex基本数学公式

LaTeX数学公式入门 LaTeX作为一种广泛使用的排版系统&#xff0c;尤其在学术界和科技领域&#xff0c;以其强大的排版能力和灵活性著称。而它的公式编辑能力更是让人叹为观止&#xff0c;经常与Markdown结合使用&#xff0c;以简化文档编写和公式展示的过程。 LaTeX 公式 L…

数字的位操作——326、504、263、190、191、476、461、477、693

326. 3 的幂&#xff08;简单&#xff09; 给定一个整数&#xff0c;写一个函数来判断它是否是 3 的幂次方。如果是&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 整数 n 是 3 的幂次方需满足&#xff1a;存在整数 x 使得 n 3x 示例 1&#xff1a; 输入&a…

本地部署持续集成工具Jenkins并配置公网地址实现远程自动化构建

文章目录 前言1. 安装Jenkins2. 局域网访问Jenkins3. 安装 cpolar内网穿透软件4. 配置Jenkins公网访问地址5. 公网远程访问Jenkins6. 固定公网地址 前言 本文主要介绍如何在Linux CentOS 7中安装Jenkins并结合cpolar内网穿透工具实现远程访问管理本地部署的Jenkins服务. Jenk…

DDR等长,到底长度差多少叫等长?

DDR4看这一篇就够了 - 知乎 (zhihu.com) 【全网首发】DDR4 PCB设计规范&设计要点PCB资源PCB联盟网 - Powered by Discuz! (pcbbar.com) 终于看到较为权威的DDR4等长要求了: !!!! 依据这个要求&#xff0c;H616项目的等长线不合格&#xff1a;