先下載opcdaauto.dll,作為Project的Reference,再寫入下面經過改良的Class,當中不難看出Class包含連接OPC Server、創建Client Group及寫入Tag Value。
using OPCAutomation;
public class OPC
{
OPCServer KepServer;
OPCGroups KepGroups;
OPCGroup KepGroup;
OPCItems KepItems;
OPCItem KepItem;
string strHostIP = "";
string strHostName = "";
int itmHandleClient = 0;
public bool GetLocalServer()
{
IPHostEntry IPHost = Dns.GetHostEntry(Environment.MachineName);
if (IPHost.AddressList.Length > 0)
{
strHostIP = IPHost.AddressList[0].ToString();
}
else
{
MessageBox.Show("GetLocalServer Error 1");
return false;
}
IPHostEntry ipHostEntry = Dns.GetHostEntry(strHostIP);
strHostName = ipHostEntry.HostName.ToString();
try
{
KepServer = new OPCServer();
object serverList = KepServer.GetOPCServers();
KepServer.Connect(((Array)serverList).GetValue(1).ToString(),"localhost");
}
catch(Exception ex)
{
MessageBox.Show("GetLocalServer Error 2 " + ex.Message);
return false;
}
return true;
}
public bool CreateGroup()
{
try
{
KepGroups = KepServer.OPCGroups;
KepGroup = KepGroups.Add("MaxsonProgram");
SetGroupProperty();
KepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
KepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete);
KepItems = KepGroup.OPCItems;
}
catch (Exception err)
{
MessageBox.Show(err.Message, "CreateGroup提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
public void SetGroupProperty()
{
KepServer.OPCGroups.DefaultGroupIsActive = true;
KepServer.OPCGroups.DefaultGroupDeadband = 0;
KepGroup.UpdateRate = 1000;
KepGroup.IsActive = true;
KepGroup.IsSubscribed = true;
}
void KepGroup_AsyncWriteComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array Errors)
{
MessageBox.Show("Success Write");
}
void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
}
public void opcWrite(string tagname, string value)
{
try
{
Array Errors;
int cancelID;
KepItem = KepItems.AddItem(tagname, itmHandleClient);
int[] temp2 = new int[2] { 0, KepItem.ServerHandle };
Array serverHandles = (Array)temp2;
object[] valueTemp = new object[2] { "", value };
Array values = (Array)valueTemp;
KepGroup.AsyncWrite(1, ref serverHandles, ref values, out Errors, 2009, out cancelID);
KepItems.Remove(KepItems.Count, ref serverHandles, out Errors);
GC.Collect();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
如果想調用OPC Class,可依下面作為參考。
OPC opc = new OPC();
if (opc.GetLocalServer())
{
if (opc.CreateGroup())
{
opc.opcWrite("SuiteLink.Topic.Test02", "30");
opc.opcWrite("SuiteLink.Topic.Rainfall_Value", "10.33");
opc.opcWrite("SuiteLink.Topic.Rainfall_X", "123.456");
opc.opcWrite("SuiteLink.Topic.Rainfall_Y", "654.321");
opc.opcWrite("SuiteLink.Topic.WinterMode", "1");
opc.opcWrite("SuiteLink.Topic.NormalMode", "0");
opc.opcWrite("SuiteLink.Topic.TriggerLevel", "1.6");
opc.opcWrite("SuiteLink.Topic.RainingState", "0");
}
}
Intouch Setting如下:
Visual Studio Setting如下(.net 4.5 + 32 bit CPU):(必須,我試了很多次,這是必須,否則會連接不上,這可是的花了很多時間得出來的結論)
如果設定成功及運行C# Application,你會在Intouch Viewer中看到Tag Value會改變,亦即代表你成功了。
要先使用 PN06-2521 Wonderware Kepware Drive 200909.iso 安裝driver 才可以 !!!
回覆刪除