how to connect and read values from kepware using OPCAutomation.dll

对着背影说爱祢 提交于 2019-12-08 05:11:10

问题


I am creating a small c# program to connect and read value from kepware server using OPCAutomation.dll, but unable to get its syntax?

OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer();
_OPCServer.connect("", ""......);

what values will come inside these brackets?


回答1:


OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer();
_OPCServer.connect("Kepware.KEPServerEX.V5", "");

The second parameter is the OPC Server node and can be left String.Empty.

From Reflector:

public virtual extern void Connect([In, MarshalAs(UnmanagedType.BStr)] string ProgID, [In, Optional, MarshalAs(UnmanagedType.Struct)] object Node);

I'm adding an explample to read and write values:

// set up some variables
OPCServer ConnectedOpc = new OPCServer();
Array OPCItemIDs = Array.CreateInstance(typeof(string), 10);
Array ItemServerHandles = Array.CreateInstance(typeof(Int32), 10);
Array ItemServerErrors = Array.CreateInstance(typeof(Int32), 10);
Array ClientHandles = Array.CreateInstance(typeof(Int32), 10);
Array RequestedDataTypes = Array.CreateInstance(typeof(Int16), 10);
Array AccessPaths = Array.CreateInstance(typeof(string), 10);
OPCGroup OpcGroupNames;

// connect to KepServerEX
ConnectedOpc.Connect("Kepware.KEPServerEX.V5", "");

// Add tags and OPC group.
// set up the tags
OPCItemIDs.SetValue("Counting.PLC.Station1.LoggedON", 1);
OPCItemIDs.SetValue("Counting.PLC.Station2.LoggedON", 2);
OPCItemIDs.SetValue("Counting.PLC.Station3.LoggedON", 3);
OPCItemIDs.SetValue("Counting.PLC.Station1.Operator", 4);
OPCItemIDs.SetValue("Counting.PLC.Station2.Operator", 5);
OPCItemIDs.SetValue("Counting.PLC.Station3.Operator", 6);

// set up the opc group
OpcGroupNames = ConnectedOpc.OPCGroups.Add("Group01");
OpcGroupNames.DeadBand = 0;
OpcGroupNames.UpdateRate = 100;
OpcGroupNames.IsSubscribed = true;
OpcGroupNames.IsActive = true;
OpcGroupNames.OPCItems.AddItems(6, ref OPCItemIDs, ref ClientHandles, out ItemServerHandles, out ItemServerErrors, RequestedDataTypes, AccessPaths);

// Read the values from the server for those tags.
// read
Array ItemServerReadValues = Array.CreateInstance(typeof(string), 10);
object a;
object b;
OpcGroupNames.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, 6, ref ItemServerHandles, out ItemServerReadValues, out ItemServerErrors, out a, out b);
Console.WriteLine((string)ItemServerReadValues.GetValue(4));
Console.WriteLine((string)ItemServerReadValues.GetValue(5));
Console.WriteLine((string)ItemServerReadValues.GetValue(6));

// Write some values into the server for those tags.
// write
Array ItemServerWriteValues = Array.CreateInstance(typeof(object), 7);
ItemServerWriteValues.SetValue(1, 1);
ItemServerWriteValues.SetValue(1, 2);
ItemServerWriteValues.SetValue(1, 3);
ItemServerWriteValues.SetValue("Test Op 1", 4);
ItemServerWriteValues.SetValue("Test Op 2", 5);
ItemServerWriteValues.SetValue("Test Op 3", 6);
OpcGroupNames.SyncWrite(6, ref ItemServerHandles, ref ItemServerReadValues, out ItemServerErrors);

This example is adapted from: http://lifeisunderconstruction.blogspot.mx/2011/03/opc-client-in-asp-net-c.html, I have added it just in case the link gets broken.



来源:https://stackoverflow.com/questions/30640942/how-to-connect-and-read-values-from-kepware-using-opcautomation-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!