如何使用OPCAutomation.dll从kepware连接和读取值(how to connect and read values from kepware using OPCAutomation.

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("", ""......);
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.

  • 译文

我正在创建一个小型的c#程序,使用OPCAutomation.dll从kepware服务器连接和读取值,但无法获取其语法。 OPCAutomation.OPCServer _OPCServer = new OPCAutomation.OPCServer(); _OPCServer.connect(,......);

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


 

第二个参数是OPC服务器节点,可以保留String.Empty。


 

从Reflector:


 

  public virtual extern void Connect([In,MarshalAs(UnmanagedType。 BStr)] string ProgID,[In,可选,MarshalAs(UnmanagedType.Struct)]对象节点); 
  


 

我正在添加一个解释读取和写入值:


 

  //设置一些变量
 OPCServer ConnectedOpc = new OPCServer(); 
数组OPCItemIDs = Array.CreateInstance(typeof(string),10); 
数组ItemServerHandles = Array.CreateInstance(typeof(Int32),10); 
数组ItemServerErrors = Array.CreateInstance(typeof(Int32),10); 
 Array ClientHandles = Array.CreateInstance(typeof(Int32),10); 
 Array RequestedDataTypes = Array.CreateInstance(typeof(Int16),10); 
数组AccessPaths = Array.CreateInstance(typeof(string),10); 
 OPCGroup OpcGroupNames; 
 
 //连接到KepServerEX 
 ConnectedOpc.Connect(Kepware.KEPServerEX.V5,); 
 
 //添加标签和OPC组。 
 //设置标签
 OPCItemIDs.SetValue(Counting.PL​​C.Station1.LoggedON,1); 
 OPCItemIDs.SetValue(Counting.PL​​C.Station2.LoggedON,2); 
 OPCItemIDs.SetValue(Counting.PL​​C.Station3.LoggedON,3); 
 OPCItemIDs.SetValue(Counting.PL​​C.Station1.Operator,4); 
 OPCItemIDs.SetValue(Counting.PL​​C.Station2.Operator,5); 
 OPCItemIDs.SetValue(Counting.PL​​C.Station3.Operator,6); 
 
 //设置opc组
 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); 
 
 //从服务器读取这些标签的值。 
 //读
数组ItemServerReadValues = Array.CreateInstance(typeof(string),10); 
对象a; 
对象b; 
 OpcGroupNames.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice,6,ref ItemServerHandles,OutItemServerReadValues,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 
数组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); 
  

 

你可能感兴趣的:(如何使用OPCAutomation.dll从kepware连接和读取值(how to connect and read values from kepware using OPCAutomation.)