按照自下而上的顺序
一.PLC的DB块设置
PLC 为西门子的S7 1200
PLC CUP 地址设置为 192.168.1.3
DB块号为22。
PLC内容的设置我不会,同事帮忙设置,此处内容省略,设置如下图
二、KEPServerEX 设置
KEPServerEX 为6.4 简体中文版本
- 新建
点击新建弹出,如下图,点击是,更新
2 添加通道
名称为设置为TEST,网络设备器选择自己设置的本机IP地址:192.168.1.50
以后步骤全为默认。
3.添加设备
名称Device
PLC选择S1200
Ip设置为PLC的CPU IP
以后设置都为默认下一步到最后。
4.添加静态标记
名称为 Tag_01,地址 为DB22.X0.0(之前PLC设置里面的第一个值,该值在PLC里面设置为间隔1S变化一次方便检测)
5.点击 Quick Client
测试KEPServerEX通讯是否成功
如果TEST.Device.Tag_01 对应的Quality值为良好则为成功
三、C#编写简单客户端程,类似Quick Client的检测功能
- 界面 (WPF)
- 前台代码
<Window x:Class="KEPClien.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:KEPClien"
mc:Ignorable="d"
Title="MainWindow" Height="550" Width="825">
<Grid>
<TextBox HorizontalAlignment="Left" Name="txt_Kep" Height="23" Margin="139,59,0,0" TextWrapping="Wrap" Text="Kepware.KEPServerEX.V6" VerticalAlignment="Top" Width="158"/>
<Label Content="服务器名" HorizontalAlignment="Left" Margin="47,58,0,0" VerticalAlignment="Top"/>
<TextBox Name ="txt_IP" HorizontalAlignment="Left" Height="23" Margin="433,59,0,0" TextWrapping="Wrap" Text="192.168.1.50" VerticalAlignment="Top" Width="120"/>
<Label Content="服务器地址" HorizontalAlignment="Left" Margin="326,58,0,0" VerticalAlignment="Top"/>
<Button Name="btn_Connect" Content="连接" HorizontalAlignment="Left" Margin="616,59,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBox HorizontalAlignment="Left" x:Name="txt_Group" Height="22" Margin="139,125,0,0" TextWrapping="Wrap" Text="TEST.Device" VerticalAlignment="Top" Width="194"/>
<Label Content="组名" HorizontalAlignment="Left" Margin="47,122,0,0" VerticalAlignment="Top"/>
<Button x:Name="btn_Group_Add" Content="添加组" HorizontalAlignment="Left" Margin="616,122,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBox HorizontalAlignment="Left" x:Name="txt_Item" Height="23" Margin="139,194,0,0" TextWrapping="Wrap" Text="TEST.Device.Tag_01" VerticalAlignment="Top" Width="194"/>
<Label Content="项目名" HorizontalAlignment="Left" Margin="47,191,0,0" VerticalAlignment="Top"/>
<Button x:Name="btn_Item_Add" Content="添加项" HorizontalAlignment="Left" Margin="616,191,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<TextBox HorizontalAlignment="Left" x:Name="txt_Item_Value" Height="23" Margin="139,334,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="194"/>
<Label Content="tag值实时显示" HorizontalAlignment="Left" Margin="47,331,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
- 后台代码
using OPCAutomation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
namespace KEPClien
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
#region 变量定义
OPCServer KepServer;
OPCGroups KepGroups;
OPCGroup KepGroup;
OPCItems KepItems;
OPCItem KepItem;
OPCItem[] KepItemArr = new OPCItem[20];
#endregion
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
switch (((Button)sender).Name)
{
case "btn_Connect":
{
try
{
KepServer = new OPCServer();
KepServer.Connect(this.txt_Kep.Text, this.txt_IP.Text);
MessageBox.Show("连接成功");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
break;
}
case "btn_Group_Add":
{
try
{
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add(this.txt_Group.Text);
SetGroupProperty();
KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
MessageBox.Show("添加成功");
break;
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
break;
}
case "btn_Item_Add":
{
try
{
KepItems = KepGroup.OPCItems;
KepItemArr[0] = KepItems.AddItem(this.txt_Item.Text, 1);
MessageBox.Show("添加成功");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
break;
}
}
}
void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{ //为了测试,所以加了控制台的输出,来查看事物ID号
//Console.WriteLine("********"+TransactionID.ToString()+"*********");
for (int i = 1; i <= NumItems; i++)
{
this.txt_Item_Value.Text = ItemValues.GetValue(i).ToString();
//b = Qualities.GetValue(i).ToString();
//c = TimeStamps.GetValue(i).ToString();
}
}
/// <summary>
/// 设置组属性
/// </summary>
private void SetGroupProperty()
{
KepServer.OPCGroups.DefaultGroupIsActive = true;// Convert.ToBoolean(txtGroupIsActive.Text);
KepServer.OPCGroups.DefaultGroupDeadband = 0;// Convert.ToInt32(txtGroupDeadband.Text);
KepGroup.UpdateRate = 250;// Convert.ToInt32(txtUpdateRate.Text);
KepGroup.IsActive = true;// Convert.ToBoolean(txtIsActive.Text);
KepGroup.IsSubscribed = true;// Convert.ToBoolean(txtIsSubscribed.Text);
}
}
}
注意点:添加的组和项 必须和Quick Client 里面看到 组和项的名称一致
需引用OPCAutomation.dll,如果DLL是32位的项目平台目标要选择X86否则报错
来源:oschina
链接:https://my.oschina.net/u/4381003/blog/4029645