按照自下而上的順序
一.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否則報錯
