模拟plc s7实现数据读取
plc设置
照着视频敲的 https://www.bilibili.com/video/BV1gE411j7VZ?spm_id_from=333.999.0.0
简单的添加数据 以便于读取,plc的ip地址设置为本地IP同一网段。设置完成后点击开始仿真
!!!仿真后要点击转至在线
!!!数据块要取消优化的块访问
NetToPLC连接PLC
plcsim的地址就是前面设置的plc地址
NETwork address和plcsim的地址必须在同一个网段
读取数据
ip地址为网关的ip
代码 页面
<Window x:Class="S7test.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:S7test"
mc:Ignorable="d" WindowStyle="None" AllowDrop="True"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="6*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1" >
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height="3*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<Border>
<TextBlock Grid.Row="0" Text="基于S7协议的西门子通讯项目" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>
</Border>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="1.5*"></RowDefinition>
<RowDefinition Height="2*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label BorderThickness="1" BorderBrush="Black" Content="地址" FontFamily="25" Padding="40 20"/>
<Label BorderThickness="1" BorderBrush="Black" Content="DBW0" FontFamily="25" Padding="40 20" Grid.Column="1"/>
<Label BorderThickness="1" BorderBrush="Black" Content="DBW2" FontFamily="25" Padding="40 20" Grid.Column="2"/>
<Label BorderThickness="1" BorderBrush="Black" Content="DBW4" FontFamily="25" Padding="40 20" Grid.Column="3"/>
<Label BorderThickness="1" BorderBrush="Black" Content="字符串" FontFamily="25" Padding="40 20" Grid.Column="4"/>
<Label Grid.Row="1" Name="VALUE" Grid.Column="0" BorderThickness="1" BorderBrush="Black" Content="值" FontFamily="25" Padding="45 20"/>
<Label Grid.Row="1" Name="VALUE1" Grid.Column="1" BorderThickness="1" BorderBrush="Black" Content="0" FontFamily="25" Padding="45 20"/>
<Label Grid.Row="1" Name="VALUE2" Grid.Column="2" BorderThickness="1" BorderBrush="Black" Content="0" FontFamily="25" Padding="45 20"/>
<Label Grid.Row="1" Name="VALUE3" Grid.Column="3" BorderThickness="1" BorderBrush="Black" Content="0" FontFamily="25" Padding="45 20"/>
<Label Grid.Row="1" Name="VALUE4" Grid.Column="4" BorderThickness="1" BorderBrush="Black" Content="0" FontFamily="25" Padding="45 20"/>
<TextBlock Grid.Row="1" Name="VALUE5" Grid.Column="5" Text="" FontFamily="25" Padding="45 20"/>
</Grid>
<StackPanel Orientation="Horizontal" Margin="20">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<Label Content="Q0.0"></Label>
<Border Height="25" Width="25" CornerRadius="50" Background="Black"/>
</StackPanel>
</StackPanel>
<Button Height="40" Width="150" HorizontalAlignment="Right" Content="单次读取" FontSize="20" Background="MediumPurple" Name="btn_SingleRead" Click="btn_SingleRead_Click"></Button>
</Grid>
<Grid Grid.Row="2" Name="DBW">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="118"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label BorderThickness="1" BorderBrush="Black" Content="地址" FontFamily="25" Padding="40 15"/>
<Label BorderThickness="1" BorderBrush="Black" Content="DBW0" FontFamily="25" Padding="40 15" Grid.Column="1"/>
<Label BorderThickness="1" BorderBrush="Black" Content="DBW2" FontFamily="25" Padding="40 15" Grid.Column="2"/>
<Label BorderThickness="1" BorderBrush="Black" Content="DBW4" FontFamily="25" Padding="40 15" Grid.Column="3"/>
<Label BorderThickness="1" BorderBrush="Black" Content="字符串" FontFamily="25" Padding="40 15" Grid.Column="4"/>
<Label BorderThickness="1" Grid.Row="1" BorderBrush="Black" Content="数值" FontFamily="25" Padding="40 15"/>
<TextBox BorderThickness="1" Name="DBW0" Padding="40 15" BorderBrush="Black" Background="LightGray" Grid.Column="1" Grid.Row="1"/>
<TextBox BorderThickness="1" Name="DBW2" Padding="40 15" BorderBrush="Black" Background="LightGray" Grid.Column="2" Grid.Row="1"/>
<TextBox BorderThickness="1" Name="DBW4" Padding="40 15" BorderBrush="Black" Background="LightGray" Grid.Column="3" Grid.Row="1"/>
<TextBox BorderThickness="1" Name="DBW8" Padding="40 15" BorderBrush="Black" Background="LightGray" Grid.Column="4" Grid.Row="1"/>
<Label BorderThickness="1" Grid.Row="2" BorderBrush="Black" Content="写入" FontFamily="25" Padding="40 15"/>
<Button Background="black" Grid.Row="2" Grid.Column="1" Foreground="White" Content="数值写入"></Button>
<Button Background="black" Grid.Row="2" Grid.Column="2" Foreground="White" Content="数值写入"></Button>
<Button Background="black" Grid.Row="2" Grid.Column="3" Foreground="White" Content="数值写入"></Button>
<Button Background="black" Grid.Row="2" Grid.Column="4" Foreground="White" Content="数值写入"></Button>
</Grid>
</Grid>
<Rectangle Margin="10 20" Stroke="Black"></Rectangle>
<Label Content="PLC IP" HorizontalAlignment="Left" Margin="26,59,0,0" VerticalAlignment="Top" Width="92" Height="36"/>
<TextBox Padding="5 5" HorizontalAlignment="Left" Name="IP" FontSize="18" Text="192.168.211.1" Height="33" Margin="26,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="142"/>
<Label Content="端口号" HorizontalAlignment="Left" Margin="26,186,0,0" VerticalAlignment="Top" Width="92" Height="36"/>
<TextBox Text="102" Name="Port" Padding="5 5" FontSize="20" HorizontalAlignment="Left" Height="32" Margin="26,228,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="142"/>
<Button Content="连接" Name="btn_Start" HorizontalAlignment="Left" Margin="46,333,0,0" VerticalAlignment="Top" Width="74" Click="btn_Start_Click" />
<Button Content="断开" Name="btn_Close" HorizontalAlignment="Left" Margin="46,368,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.04,0.658" Click="btn_Close_Click"/>
</Grid>
</Window>
代码 后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using S7.Net;
using thinger.DataConvertLib;
namespace S7test
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//s7通讯对象
private Plc plc = null;
//连接标志位
private bool connectState=false;
//用于停止线程
private CancellationTokenSource cts = null;
public MainWindow()
{
InitializeComponent();
this.DBW0.Tag = new Variable() { Type = Type.Ushort, Offset = 0, Start = 0 };
this.DBW2.Tag = new Variable() { Start = 2, Offset = 0, Type = Type.Short };
this.DBW4.Tag = new Variable() { Start = 4, Offset = 0, Type = Type.Float };
this.DBW8.Tag = new Variable() { Start = 8, Offset = 0, Type = Type.String };
}
//连接PLC
private void btn_Start_Click(object sender, RoutedEventArgs e)
{
if (connectState == false)
{
try
{
this.plc = new Plc(CpuType.S71200, this.IP.Text, Convert.ToInt32(this.Port.Text), 0, 0);
this.plc.Open();
}
catch (Exception ex)
{
MessageBox.Show("建立连接失败"+ex.Message);
return;
}
this.connectState = true;
//开启线程
cts = new CancellationTokenSource();
Task.Run(new Action(() => {Read(); }), cts.Token);
MessageBox.Show("建立连接成功");
}
else
MessageBox.Show("请勿重复连接");
}
private void btn_Close_Click(object sender, RoutedEventArgs e)
{
if (this.connectState == true)
{
this.plc?.Close();
this.connectState = false;
//停止线程
this.cts?.Cancel();
MessageBox.Show("断开连接成功");
}
else
{
MessageBox.Show("已断开");
}
}
//数据解析
private void MsgShow(byte[] value)
{
if (value == null)
{
return;
}
foreach(var item in this.DBW.Children)
{
if(item is TextBox textbox)
{
if (textbox.Tag != null)
{
Variable variable = textbox.Tag as Variable;
switch (variable.Type)
{
case Type.Bool:
break;
case Type.Byte:
break;
case Type.Short:
textbox.Text= ShortLib.GetShortFromByteArray(value, variable.Start).ToString();
break;
case Type.Ushort:
textbox.Text = UShortLib.GetUShortFromByteArray(value, variable.Start).ToString();
break;
case Type.Int:
break;
case Type.UInt:
break;
case Type.Long:
break;
case Type.Float:
textbox.Text = FloatLib.GetFloatFromByteArray(value, variable.Start).ToString();
break;
case Type.String:
textbox.Text = StringLib.GetSiemensStringFromByteArray(value, variable.Start, 10);
break;
}
}
}
}
}
//单个读取
private void btn_SingleRead_Click(object sender, RoutedEventArgs e)
{
//this.VALUE1.Content = ((ushort)plc.Read("DB1.DBW0")).ConvertToShort().ToString();
this.VALUE2.Content =((ushort)plc.Read("DB1.DBW2")).ConvertToShort().ToString();
this.VALUE3.Content = ((uint)plc.Read("DB1.DBD4")).ConvertToFloat().ToString();
plc.Write(S7.Net.DataType.DataBlock, 0, 0, 10,2);
int maxlength = (byte)plc.Read(S7.Net.DataType.DataBlock, 1, 8, VarType.Byte, 1);
int actuLegth = (byte)plc.Read(S7.Net.DataType.DataBlock, 1, 9, VarType.Byte, 1);
this.VALUE4.Content = (string)plc.Read(S7.Net.DataType.DataBlock, 1, 8, VarType.S7String, actuLegth);
}
private void Read()
{
//数据读取
//线程建立IsCancellationRequested为false 释放为true
while (!cts.IsCancellationRequested)
{
Thread.Sleep(100);
try
{
Dispatcher.Invoke(new Action(() =>
{
MsgShow(ReadDB());
}));
}
catch(Exception)
{
return;
}
}
}
//批量读取
private byte[] ReadDB()
{
try
{
byte[] result = this.plc.ReadBytes(S7.Net.DataType.DataBlock, 1, 0, 20);
return result;
}
catch(Exception ex)
{
return null;
}
}
}
}
代码 数据解析类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace S7test
{
/// <summary>
/// 解析数据
/// </summary>
public enum Type
{
Bool,
Byte,
Short,
Ushort,
Int,
UInt,
Long,
Float,
String
}
public class Variable
{
//使用属性描述变量
public int Start { get; set; }//起始位置
public int Offset { get; set; }//偏移量
public Type Type { get; set; }//变量的类型
}
}