您的位置:首页 > 运维架构

使用OPCNetAPI连接OPCServer

2015-04-29 12:05 274 查看
OPCServer KepServer;
OPCGroup KepGroup;
bool opc_connected;
string remoteServerName = "KEPware.OPCSampleServer";
string remoteServerIP = "";

public OPCManagerService()
{
try
{
KepServer = new OPCServer();
KepServer.Connect(remoteServerName, remoteServerIP);

if (KepServer.ServerState != (int)OPCServerState.OPCRunning)
{

//这里你可以根据返回的状态来自定义显示信息,请查看自动化接口API文档
//tsslServerState.Text = "状态:" + KepServer.ServerState.ToString() + "   ";
return;
}
}
catch (Exception)
{
return;
}

opc_connected = true;

KepServer.OPCGroups.DefaultGroupIsActive = true;
KepServer.OPCGroups.DefaultGroupDeadband = 0f;  //the percentage change required before a change is reported, used to filter noise
KepServer.OPCGroups.DefaultGroupUpdateRate = 10;  //the rate is ms before item is updated

KepGroup = KepServer.OPCGroups.Add("OPCDOTNETGROUP");
KepGroup.IsSubscribed = false;
KepGroup.OPCItems.DefaultIsActive = false;
}


获取单个OPCItem的方法:

//OPCDataItemValue是自定义的一个类,用来保存OPCItem的实时数据
public OPCDataItemValue GetDataItemValue(string ItemID)
{
if (!opc_connected) return null;

try
{
OPCItem item = KepGroup.OPCItems.AddItem(ItemID, 1);

Object value;
Object quality;
Object timestamp;
item.Read((short)OPCDataSource.OPCDevice, out value, out quality, out timestamp);

OPCDataItemValue itemValue = new OPCDataItemValue();
itemValue.DataValue = value;
itemValue.TimeStamp = (DateTime)timestamp;
itemValue.Quality = Convert.ToInt32(quality);

Array removeServerHandle = (Array)(new int[2] { 0, item.ServerHandle });
Array removeErrors;
KepGroup.OPCItems.Remove(1, ref removeServerHandle, out removeErrors);

return itemValue;
}
catch (Exception)
{
return null;
}
}


获取多个OPCItem的方法:

public List<OPCDataItemValue> GetDataItems(string ItemID)
{
if (!opc_connected) return null;

try
{
List<OPCDataItemValue> dataItems = new List<OPCDataItemValue>();
OPCDataItemValue opcDataItem = null;
int[] itmHandleServer = new int[2]; //index starts at 1
OPCItem KepItem = KepGroup.OPCItems.AddItem(ItemID, 1);
itmHandleServer[1] = KepItem.ServerHandle;
Array handles = (Array)itmHandleServer;

Array values;
Array errors;
object qualities;
object timestamps; //store the timestamp of the read

//read directly from device
KepGroup.SyncRead((short)OPCDataSource.OPCDevice, 1, ref handles, out values, out errors, out qualities, out timestamps);

opcDataItem = new OPCDataItemValue();
opcDataItem.ID = KepItem.ItemID;
opcDataItem.DataValue = values.GetValue(1);
Array t = (Array)timestamps;
opcDataItem.TimeStamp = (DateTime)t.GetValue(1);
Array q = (Array)qualities;
opcDataItem.Quality = Convert.ToInt32(q.GetValue(1));

dataItems.Add(opcDataItem);

//删除OPCItem的方法和上面代码中的一样,这里就不写了。

return dataItems;
}
catch (Exception)
{
return null;
}
}


获取某OPCITem的特定属性:

public OPCDataItemProperties GetDataItemProperties(string ItemID, List<int> PropertyIDs)
{
if (!opc_connected) return null;

try
{
OPCDataItemProperties property = new OPCDataItemProperties();
OPCItem item = KepGroup.OPCItems.AddItem(ItemID, 1);

List<int> idList = new List<int>(PropertyIDs);
idList.Insert(0, 0);
Array ids = (Array)idList.ToArray();

int count = PropertyIDs.Count;
Array values;
Array errors;
KepServer.GetItemProperties(ItemID, count, ref ids, out values, out errors);

property.Count = count;
property.IDs = ids;
property.Values = values;
property.Errors = errors;

Array removeServerHandle = (Array)(new int[2] { 0, item.ServerHandle });
Array removeErrors;
KepGroup.OPCItems.Remove(1, ref removeServerHandle, out removeErrors);

return property;
}
catch (Exception)
{
return null;
}
}


获取某OPCItem包含的所有属性:

public OPCDataItemAvailableProperties GetDataItemAvailableProperties(string ItemID)
{
if (!opc_connected) return null;

try
{
OPCDataItemAvailableProperties properties = new OPCDataItemAvailableProperties();
OPCItem item = KepGroup.OPCItems.AddItem(ItemID, 1);

int propertyCount;
Array propertyIDs;
Array propertyDescriptions;
Array propertyDataTypes;
KepServer.QueryAvailableProperties(ItemID, out propertyCount, out propertyIDs, out propertyDescriptions, out propertyDataTypes);

properties.Count = propertyCount;
properties.IDs = propertyIDs;
properties.Descriptions = propertyDescriptions;
properties.DataTypes = propertyDataTypes;

Array removeServerHandle = (Array)(new int[2] { 0, item.ServerHandle });
Array removeErrors;
KepGroup.OPCItems.Remove(1, ref removeServerHandle, out removeErrors);

return properties;
}
catch (Exception)
{
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: