C# WPF S7协议读取西门子PLC(S7200_SMART)

WPF S7协议读取西门子PLC(S7200_SMART)

S7200_SMART

添加包 S7netplus

在这里插入图片描述

MainWindow.xaml

<Window x:Class="WPF_S7_Demo.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525" >
    <Grid>
        <ContentControl prism:RegionManager.RegionName="ContentRegion" />
        <StackPanel>
            <TextBlock Text="{Binding TestValue}"></TextBlock>
            <Button Content="连接PLC" Width="100" Command="{Binding ConnectPLCCommand}"></Button>
            <Button Content="读取数据" Width="100" Margin="0 10 0 0" Command="{Binding ReadDataCommand}"></Button>
        </StackPanel>
    </Grid>
</Window>

MainWindowViewModel.cs

using Prism.Commands;
using Prism.Mvvm;
using S7.Net;
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WPF_S7_Demo.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        public Plc SiemensPlc;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _TestValue = "";
        public string TestValue
        {
            get { return _TestValue; }
            set { SetProperty(ref _TestValue, value); }
        }

        public MainWindowViewModel()
        {

        }

        private DelegateCommand _ConnectPLCCommand;
        public DelegateCommand ConnectPLCCommand =>
            _ConnectPLCCommand ?? (_ConnectPLCCommand = new DelegateCommand(ExecuteConnectPLCCommand));

        void ExecuteConnectPLCCommand()
        {
            // CpuType.S7200:   PLC类型
            // 10.168.100.2         IP地址
            // 0                         表示PLC所在的机架号,一般选0即可
            // 1                         表示PLC所在的插槽号,一般选1即可
            SiemensPlc = new Plc(CpuType.S7200Smart, "192.178.1.2", 0, 1);
            SiemensPlc.Open();
        }

        private DelegateCommand _ReadDataCommand;
        public DelegateCommand ReadDataCommand =>
            _ReadDataCommand ?? (_ReadDataCommand = new DelegateCommand(ExecuteReadDataCommand));

        void ExecuteReadDataCommand()
        {
            // 单个读取
            // 读取浮点数
            var realVariabl = ((uint)SiemensPlc.Read("DB1.DBD1300")).ConvertToFloat();
            TestValue = realVariabl.ToString();

            // 读取单字节bool DB1,X1102.3
            bool db1Bool2 = (bool)SiemensPlc.Read("DB1.DBX1000.3");
            TestValue = db1Bool2.ToString();

            // 多个读取
            // DataType: 数据类型,DB或Memory等
            // DB : DataBlock=1,Memory=0
            // count : 偏移量(offset),设置多少就获取到具体位置的数据
            var bytes = SiemensPlc.ReadBytes(DataType.DataBlock, 1, 1000, 2);
            // 读取Bool
            var bool1 = bytes[0].SelectBit(3);
            TestValue = bool1.ToString();

            // 读取浮点数
            var bytes = SiemensPlc.ReadBytes(DataType.DataBlock, 1, 1300, 4);
            // Skip 第几位开始
            // Take 读几个
            Double realVariable = S7.Net.Types.Double.FromByteArray(bytes.Skip(0).Take(4).ToArray());
            TestValue = realVariable.ToString();

        }
    }
}

执行效果
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
MVCMVPMVVM是常用的软件设计模式,用于分离应用程序的不同组件,提高代码的可维护性和可重用性。以下是它们的概念和区别: 1. MVC模式(模型-视图-控制器):MVC模式是最古老也是最常用的设计模式之一。它将应用程序分为三个组件:模型、视图和控制器。模型负责处理数据和业务规则,视图负责展示数据给用户,控制器负责处理用户输入并更新模型和视图。MVC模式通过分离关注点,使得修改一个组件对其他组件没有依赖,增强了代码的可维护性。 2. MVP模式(模型-视图-展示器):MVP模式是基于MVC模式的演化,主要用于桌面和移动应用程序的开发。它与MVC的不同之处在于,视图和控制器被合并成一个展示器,展示器负责处理用户输入、更新模型并更新视图。MVP模式通过与视图分离,使得视图的变化不会影响展示器的逻辑。这样,在测试时可以更轻松地独立对展示器进行单元测试。 3. MVVM模式(模型-视图-视图模型):MVVM模式是一种用于构建用户界面的设计模式。它将视图的状态和行为抽象成一个视图模型,视图模型负责处理用户输入、保存视图状态、与模型进行交互,并通过数据绑定将数据自动更新到视图上。MVVM模式通过数据绑定机制,使得视图和模型之间的通信变得更简单,提高了可维护性和可重用性。它常用于Web前端开发和桌面应用程序的现代界面开发。 总结来说,MVCMVPMVVM是三种常见的软件设计模式。MVC模式是最早的一种,将应用程序分为模型、视图和控制器,用于分离关注点。MVP模式是基于MVC模式的演化,通过将视图和控制器合并成一个展示器,便于测试和维护。MVVM模式是用于构建用户界面的设计模式,通过视图模型和数据绑定机制,实现了视图与模型之间的解耦。每种模式都有自己的特点和适用场景,根据具体需求选择合适的模式可以提高开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值