如何使用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); 
  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值