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

36 阅读1分钟

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

S7200_SMART

添加包 S7netplus

1_2.png

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_1.png