注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

OPC client -C#.NET sample  

2014-05-06 22:55:24|  分类: OPC |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

FROM :https://www.faweb.net/us/opc/sample_csnet.html

If you access to OPC Server from .NET, the wrapper which exchanges COM and .NET is needed.
It explains the implementation of C#.NET accessed the OPC server by using RCW(Runtime Callble Wrapper) here.

Connect to OPC Server by COM

The instance of the COM connection is created specifying CLSID of the OPC server, and you can get each interface-pointers of OPC such as the OPC server objects and the browse objects etc.
IOPCServerList svrList = (IOPCServerList)CreateInstance(CLSID_SERVERLIST, null); Guid clsidList; svrList.CLSIDFromProgID(sSvrName, out clsidList); m_OPCServer = (IOPCServer)CreateInstance(clsidList, sNodeName); AddGroup(sGrpName, iUpdateRate); IOPCCommon m_com = (IOPCCommon)m_OPCServer;

Create OPC Group and Add tags

It is necessary to create the group object in the OPC server to acquire real data from the outside.The data update cycle can be set to the group object, and usually register the device accessed by the same synchronization to the group. Because the threads of each group are created in the OPC server, please note CPU load increase by subdivision too much.
// Add OPCGroup guidGroupStateMgt = Marshal.GenerateGuidForType(typeof(IOPCGroupStateMgt2)); m_OPCServer.AddGroup(sGrpName, (bActive) ? 1 : 0, iUpdateRate, iClientGroup, ptrTimeBias, ptrDeadBand, iLCID, out m_iServerGroup, out iRevisedUpdateRate, ref guidGroupStateMgt, out group); m_OPCGroup2 = (IOPCGroupStateMgt2)group; m_OPCGroup2.SetKeepAlive(iKeepAliveTime, out iKeepAliveTime); m_OPCConnPointCntnr = (IConnectionPointContainer)m_OPCGroup2; guidDataCallback = Marshal.GenerateGuidForType(typeof(IOPCDataCallback)); m_OPCConnPointCntnr.FindConnectionPoint(ref guidDataCallback, out m_OPCConnPoint); // Add OPCItems for (i = 0; i < iItemCount; i++) { itemDef[i].szItemID = ItemName[i]; itemDef[i].bActive = 1; itemDef[i].hClient = ClientHd[i]; } m_OPCItem = (IOPCItemMgt)m_OPCGroup2; m_OPCItem.AddItems(iItemCount, itemDef, out ppResult, out ppErrors);

Sync Read/Write and ASync Read/Write

The OPC has two ways of data access, Synchronize and Asynchronous. In Synchronize, the client application must be wait for completing server work. But the client application can confirm server transaction is normally completed because the OPC Server returns the result on completing the communication work. This is a normally style of procedure.
In Asynchronous mode, the OPC Server turns the handle to client as soon as receiving the request and proceed the transaction on background. After completing this work, calls handler of client application. Asynchronous mode cut down wait time for communication and be able to run client application.
Caution: Asynchronous access is enabled under advice mode.
// Read by IOPCSyncIO OPCSyncIO2 = (IOPCSyncIO2)m_OPCGroup2; OPCSyncIO2.Read(OPC_DS_DEVICE /*OPC_DS_CACHE*/, iItemCount, ServerHd, out ppItemVal, out ppErrors); Marshal.Copy(ppErrors, Errors, 0, iItemCount); posItem = ppItemVal; for (i=0; i<iItemCount; i++) { ItemState = (OPCITEMSTATE)Marshal.PtrToStructure(posItem, typeof(OPCITEMSTATE)); if (Errors[i] == 0) { Values[i] = ItemState.vDataValue; TimeStamps[i] = ItemState.ftTimeStamp; Qualities[i] = ItemState.wQuality; } Marshal.DestroyStructure(posItem, typeof(OPCITEMSTATE)); posItem = new IntPtr(posItem.ToInt32() + Marshal.SizeOf(typeof(OPCITEMSTATE))); } // Write by IOPCSyncIO. OPCSyncIO2.Write(iItemCount, ServerHd, Value, out ppErrors); Marshal.Copy(ppErrors, errors, 0, iItemCount); Marshal.FreeCoTaskMem(ppErrors);
Cache read (OPC_DS_CACHE) immediately returns the latest data value on the server's memory without a communication. The OPC server automatically updates data according to the registered update cycle of each group. It becomes possible to high-speed access when it may not be necessarily the latest data.


// Read by IOPCASyncIO OPCAsyncIO3 = (IOPCAsyncIO3)m_OPCGroup2; OPCAsyncIO3.Read(iItemCount, ServerHd, wTransID, out wCancelID, out ppErrors); // Write by IOPCASyncIO OPCAsyncIO3 = (IOPCAsyncIO3)m_OPCGroup2; OPCAsyncIO3.Write(iItemCount, ServerHd, Value, wTransID, out wCancelID, out ppErrors); Marshal.FreeCoTaskMem(ppErrors);

  评论这张
 
阅读(785)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017