2、 创建服务器,编写Open()方法
/// 创建一个OPC Server接口
///
/// 返回错误信息
/// 若为true,创建成功,否则创建失败
public bool Open(out string error)
{
error="";bool success=true;
Type svrComponenttyp ;
//获取 OPC Server COM 接口
iidRequiredInterface = typeof(IOPCItemMgt).GUID;
svrComponenttyp = System.Type.GetTypeFromProgID(serverType);
try
{
//创建接口
pIOPCServer =(IOPCServer)System.Activator.CreateInstance(svrComponenttyp);
error="";
}
catch (System.Exception err) //捕捉失败信息
{
error="错误信息:"+err.Message;success=false;
}
Return true;
}
3、 在服务器上添加用于添加Group的函数
/// 添加组
/// 组名
/// /创建时,组是否被激活
/// //组的刷新频率,以ms为单位
/// 返回错误信息
/// 若为true,添加成功,否则添加失败
public bool AddGroup(string groupName,int bActive,int updateRate,out string error)
{
error="";
int dwLCID = 0x407; //本地语言为英语
int pRevUpdateRate;
float deadband = 0;
// 处理非托管COM内存
GCHandle hDeadband;
IntPtr pTimeBias = IntPtr.Zero;
hDeadband = GCHandle.Alloc(deadband,GCHandleType.Pinned);
try
{
pIOPCServer.AddGroup(groupName, //组名
bActive, //创建时,组是否被激活
updateRate, //组的刷新频率,以ms为单位
hClientGroup, //客户号
pTimeBias, //这里不使用
(IntPtr)hDeadband,
dwLCID, //本地语言
out nSvrGroupID, //移去组时,用到的组ID号
out pRevUpdateRate, //返回组中的变量改变时的最短通知时间间隔
ref iidRequiredInterface,
out pobjGroup1); //指向要求的接口
hClientGroup=hClientGroup+1;
int groupID=nSvrGroupID;
groupsID.Add(groupName,groupID);
}
catch (System.Exception err) //捕捉失败信息
{
error="错误信息:"+err.Message;
}
finally
{
if (hDeadband.IsAllocated) hDeadband.Free();
}
if(error=="")
return true;
else
return false;
}
4、 向指定的组中添加变量的函数
/// 添加多个项到组
///
/// 指定组名
/// 指定项名
/// 由函数返回的服务器确定的项ID号
/// 无错误,返回true,否则返回false
public bool AddItems(string groupName,string[] itemsName,int[] itemsID)
{
bool success=true;
OPCITEMDEF[] ItemDefArray=new OPCITEMDEF[itemsName.Length];
for(int i=0;i<ITEMSNAME.LENGTH;I++)
{
hClientItem=hClientItem+1;
ItemDefArray[i].szAccessPath = ""; // 可选的通道路径,对于Simatiic Net不需要。
ItemDefArray[i].szItemID = itemsName[i]; // ItemID, see above
ItemDefArray[i].bActive = 1; // item is active
ItemDefArray[i].hClient = hClientItem; // client handle
ItemDefArray[i].dwBlobSize = 0; // blob size
ItemDefArray[i].pBlob = IntPtr.Zero; // pointer to blob
ItemDefArray[i].vtRequestedDataType = 2; //Word数据类型
}
//初始化输出参数
IntPtr pResults = IntPtr.Zero;
IntPtr pErrors = IntPtr.Zero;
try
{
// 添加项到组
((IOPCItemMgt)GetGroupByName(groupName)).AddItems(itemsName.Length,ItemDefArray,out pResults,out pErrors);
// Unmarshal to get the server handles out fom the m_pItemResult
// after checking the errors
int[] errors = new int[itemsName.Length];
Marshal.Copy(pErrors, errors, 0,itemsName.Length);
IntPtr pos = pResults;
for(int i=0;i
{
if (errors[i] == 0)
{
OPCITEMRESULT result = (OPCITEMRESULT)Marshal.PtrToStructure(pos, typeof(OPCITEMRESULT));
itemsID[i] = result.hServer;
this.hitemsID.Add(itemsName[i],result.hServer);
pos = new IntPtr(pos.ToInt32() + Marshal.SizeOf(typeof(OPCITEMRESULT)));
}
else
{
success=false;
break;
}
}
}
catch (System.Exception err) // catch for error in adding items.
{
success=false;
}
finally
{
// 释放非托管内存
if(pResults != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pResults);
pResults = IntPtr.Zero;
}
if(pErrors != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(pErrors);
pErrors = IntPtr.Zero;
}
}
return success;
}
说明:使用该函数时,在类的开头,应该先声明整数数据,以用于保存由本函数返回的服务器对每一项分配的Item ID号: