一,安裝KEPware.Enhanced.OPC.DDE.KEPServer。(PLC數據傳送給KEPServer,開發的程序用OPCServer讀KEPServer)工具
設置ip地址後面是指的plc站號,此處必須和plc上站號對應。不然沒法接收數據。ui
打開quick client 查看傳值狀況spa
二.C#程序代碼線程
引用opcdaauto.dllcode
在win7 64位下KepServer = new OPCServer()會出錯的問題,orm
檢索COM 類工廠中 CLSID 爲 <28e68f9a-8d75-11d1-8dc3-3c302a000000> 的組件時失敗,緣由是出現如下錯誤: 80040154blog
1.必定要到C:\Windows\SysWOW64路徑下打開cmd.exe運行命令文件(而不是在運行打開),註冊「OPCDAAuto.dll」 輸入命令: REGSVR32 OPCDAAuto.dll;便可事件
2.編譯工具目標要選擇x86ip
KepServer.Connect("KEPware.KEPServerEx.V4", "127.0.0.1"); 此處ip地址是安裝KEPware.Enhanced.OPC.DDE.KEPServer電腦的ip地址.開發
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using OPCAutomation; using System.Threading; namespace SPCTest { public partial class Form2 : Form { private OPCServer KepServer; private OPCGroups KepGroups; private OPCGroup KepGroup; private OPCItems KepItems; //軸承淨重,注脂量,防塵蓋壓入深度,防塵蓋平整度 壓入OK/NG private OPCItem item1, item2, item3; public Form2() { InitializeComponent(); } #region OPC Server private void KepServerLoad() { try { KepServer = new OPCServer(); KepServer.Connect("KEPware.KEPServerEx.V4", "127.0.0.1"); if (KepServer.ServerState == (int)OPCServerState.OPCRunning) { // statusLabelConnectInfo.Text = "OPC Server鏈接成功"; } else { // statusLabelConnectInfo.Text = "OPC Server鏈接失敗"; return; } } catch (Exception ex) { // statusLabelConnectInfo.Text = "OPC Server鏈接失敗," + ex.Message; return; } KepGroups = KepServer.OPCGroups; Thread t1; // 開1個線程用於讀取數據 t1 = new Thread(new ThreadStart(KepProcess)); t1.Start(); } public void KepProcess() { //KepGroup = KepGroups.Add("Channel.Device.Group"); KepGroup = KepGroups.Add("Channel1.Device1.Group"); KepGroup.UpdateRate = 250; KepGroup.IsActive = true; KepGroup.IsSubscribed = true; //當KepGroup中數據發生改變的觸發事件 KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); KepItems = KepGroup.OPCItems; item1 = KepItems.AddItem("Channel1.Device1.A01", 1); item2 = KepItems.AddItem("Channel1.Device1.A02", 2); item3 = KepItems.AddItem("Channel1.Device1.A03", 3); } //當數據改變時觸發的事件 public delegate void DelegateShowMessage(string str); public void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { string str = ""; DelegateShowMessage show1 = new DelegateShowMessage(ShowMessage); for (int i = 1; i <= NumItems; i++) { if (ClientHandles.GetValue(i).Equals(1)) { str = "A01:" + ItemValues.GetValue(i).ToString(); } if (ClientHandles.GetValue(i).Equals(2)) { str = "A02:" + ItemValues.GetValue(i).ToString(); } if (ClientHandles.GetValue(i).Equals(3)) { str = "A03:" + ItemValues.GetValue(i).ToString(); } BeginInvoke(show1, new string[] { str }); } } public void ShowMessage(string str) { richTextBox1.AppendText(str+","); } #endregion private void Form2_Load(object sender, EventArgs e) { KepServerLoad(); } private void Form2_FormClosing(object sender, FormClosingEventArgs e) { KepServer.Disconnect(); } } }