OPC:客户端开发——应用WTclient.dll使用手册部分中文版
WinTECH軟件快速客戶端開發DLL(WTclient),提供了一種應用API方便地將定制應用和任何OPC Server相結合的方式。所有COM和OPC的細節均由DLL來處理,這使得一個應用可以輕松的從服務器獲取數據點,而不用關心接口后的實際完成過程。 這些DLL可以方便和已有的或者新的應用相結合。其中提供了面向OPC1.0和OPC2.0數據訪問標準所需的全部OPC接口,同時也提供了瀏覽接口和連 接到簡單警報/事件服務器的支持。
這個用戶指導應該被當作一本OPC數據訪問和OPC警報/事件規范手冊文檔使用,其中的所有資料都可在OPC主網站http://www.opcfoundation.org上找到。
WTclient.lib內有DLL文件中API的出口定義,將這個文件包含到你的定制應用的工程文件中,同時將WTclientAPI.h包 含到會調用DLL的模塊中。如果應用Visual Basic進行編程,需要將VBA范例中(隨demo提供)的module1.bas包含到你的工程中。
在使用WTclient.dll的之前,需要在代碼起始的時候調用一下函數Disable30MinTimer
| 函數 | BOOL Disable30MinTimer (LPCSTR Authorization); |
| 說明 | DLL里面有個計時的函數,如果想正常使用dll的功能需要在開始傳遞一個字符串來調用一個禁用計時的函數,否則30分鐘之后停止服務。該dll文件有的導出函數傳遞也是LPCSTR的,程序調用功能正常,也是采用傳遞byref?的?byte來進行的。 至于說這個字符串是什么,就要和Dll的廠家公司聯系了 |
?
?
?
?
?
?
?
| 函數 | WTclientCoInit()、CoUninitialize() |
| 說明 | 從WTClient.DLL2.0版本開始,初始化DCOM是客戶端應用的責任了。2.0版本之前,WTClient.dll在DLLMain 函數中執行DCOM和安全初始化的過程,并在dll下載時調用CoUninitialize函數。由于不同windows dll文件的加載順序,這會帶來一些問題。 這些初始化調用被移動到了一個新的輸出函數WTclientCoInit()中,用戶的應用可以在主要可執行線程啟動的時候調用這個函數。WTclientCoInit()將DCOM初始化為多線程并使用默認安全設定。你的應用可以選擇自行初始化DCOM替代 這個輸出函數。任何情況下,你的應用都必須在終止時調用CoUninitialize()函數。 |
2、獲取OPC服務器列表
| 函數 | int NumberOfOPCServers (BOOL UseOPCENUM, LPCSTR MachineName); |
| 說明 | OPCENUM是由OPC Foundation提供的一個組件,它允許預期的客戶端應用獲取本地以及遠程可用服務器列表。可惜的是,在寫這個組件的時候,OCENUM的操作并沒有很好的跨平臺一致性。 WTclient.dll允許應用嘗試使用OPCENUM組件來獲取服務器列表,或者越過OPCENUM組件通過掃描Windows 登記的本地拷貝來獲取列表。如果使用了OPCENUM,你可以通過傳遞遠程機器的名稱來獲取遠程服務器列表。 |
一旦WTclietn.dll返回了服務器的數目,應用就可以通過調用如下所示的函數來對服務器名稱列表進行枚舉:
| 函數 | BOOL GetServerName (int index, LPSTR Buffer, int BufSize); |
| 說明 | 一個由用戶提供的字符緩沖區Buffer負責存放傳遞過來的index標識的服務器列表中某個服務器名字。BufSize標識用戶提供的字符緩 沖區的長度,并可以防止dll載入過長的名字。如果Buffer中返回了一個有效的服務器名稱,GetServerName函數返回TRUE。 |
?
還有一個補充函數集允許應用獲取可用的警報/事件服務器列表。
| 函數 | int NumberOfOPC_AEServers (LPCSTR MachineName); BOOL GetOPC_AEServerName (int index, char *pBuf, int BufSize); |
3、建立、斷開OPC連接
| 函數 | HANDLE ConnectOPC(LPCSTR MachineName, LPCSTR ServerName, BOOL EnableDLLBuffering); HANDLE ConnectOPC1(LPCSTR MachineName, LPCSTR ServerName, BOOL EnableDLLBuffering); HANDLE ConnectOPC_AE(LPCSTR MachineName, LPCSTR ServerName); |
| 說明 | 如果一個由計算機名稱和服務器名稱定義的和OPC服務器之間的連接可以被建立,則此函數返回一個有效的HANDLE。可以建立和不同的服務器的多個同步連接,此時返回的HANDLE標識了可用于以后創建組和項目的連接。如果傳遞一個空字符串作為計算機名稱參數,標識的是本地計算機。 WTclient會先嘗試使用OPC2.0連接點接口進行連接,如果不可用,則會轉而采用OPC1.0數據對象接口。 WTclient.dll的基本操作有兩種模式。第一種模式用于支持'C-樣式'回調函數的應用,在這種情況下,dll中沒有緩沖區操作。由于可以從連接的OPC服務器接收到數據更改提示,回調可用于將新的信息傳送給控制應用(此處涉及到下面會提及的EnableOPCNotification 函數)。 對于使用不支持回調函數的工具設計的那些應用(例如Visual Basic 5.0),WTclient會被配置為維護一個由應用創建的全部OPC項目(標簽)列表的模式。當服務器數據改變時,dll中列表里相關的標簽值會被更 新,控制應用可以隨時讀取標簽的值(此處涉及到下面會提及的ReadOPCItem函數)。 ConnectOPC1會總是嘗試使用OPC1.0連接。 ConnentOPC_AE會建立一個和OPC警報/事件服務器的連接,此連接由計算機名稱和服務器名稱指定。 |
斷開連接的函數:
| 函數 | void DisconnectOPC(HANDLE hConnect); void DisconnectOPC_AE(HANDLE hConnect); |
| 說明 | 當一個應用終止的時候,它會負責從依附的服務器斷開連接。 DisconnectOPC(或Disconnect_OPCAE)函數會對hConnect定義的連接執行一個徹底的連接關閉操作。 |
4、獲取服務器標簽名列表
| 函數 | int? NumberOfOPCItems(HANDLE hConnect); BOOL? GetOPCItemName (HANDLE hConnect, int index, char *pBuf, int BufSize); |
| 說明 | 這兩個函數和獲取服務器名稱列表函數的調用方式類似。 NumberOfOPCItems()返回所連接OPC服務器支持的獨有標簽總數。 依次地調用GetOPCItemName()會逐步遍歷所有可用的標簽名,最終允許控制應用選擇一個項目或者提交一個瀏覽列表給用戶。 NumberOfOPCItems()會瀏覽服務器的全部項目名稱列表并提供一個“FLAT”格式的列表。NumberOfOPCItems()函數相當于在根節點處用OPC_FLAT方式調用BrowseItems函數。(BrowseItems函數見下表) |
?
| 函數 | BOOL GetNameSpace (HANDLE hConnect, WORD *pNameSpace); BOOL BrowseTo (HANDLE hConnect, LPCSTR NodeName); char SetWTclientQualifier (char qualifier); int? BrowseItems (HANDLE hConnect, WORD Filter); |
| 說明 | 為了提供較好的應用等級體系命名空間訪問服務器的方式,WTClient dll提供函數以便于應用直接從服務器端瀏覽項目名。 GetNameSpace()函數允許應用決定服務器的命名空間,BrowseTo()函數負責將當前瀏覽位置移動到指定的名稱處,BrowseItems()函數用服務器的節點名稱來填充內部名稱列表。BrowseTo()函數既可以和 OPC_LEAF一起使用來讀取當前瀏覽位置的葉片名稱,也可以和OPC_BRANCH一起使用來當前瀏覽位置的讀取枝條名稱。這些名稱可以被 GetOPCItemName函數獲取。 WtClient動態鏈接庫為NumberOfOPCItems()函數和BrowseItems()函數維護一個單一的名稱列表,因此應用程序在瀏覽服務器命名空間的時候必須很小心,避免覆蓋前一次調用的值。在各個等級的樹的相互作用過程中,調用BrowseItems之后,所有的項目名稱必須立刻拷貝到本地存儲中。 SetWTclientQualifier()函數允許應用程序改變用來解析各個等級命名空間節點的劃界字符。客戶端應用一般認為缺省的劃界符為‘.’,然而也有些服務器將‘.’字符用在了節點名字中。 |
| 函數 | BOOL? SetBrowseFilters (HANDLE hConnect, LPCSTR UserString, VARTYPE DataType, DWORD AccessType); |
| 說明 | 應用程序可以設置瀏覽過濾器來影響服務器返回的OPC項目個數。應用程序可以選擇僅獲取那些符合指定數據類型或者訪問權限的項目。 |
5、創建OPC組和標簽
| 函數 | HANDLE AddOPCGroup (HANDLE hConnect, LPCSTR Name, DWORD *pRate, float *pDeadBand); void? RemoveOPCGroup (HANDLE hConnect, HANDLE hGroup); |
| 說明 | WTclient.dll提供兩個函數,允許你創建、移除一個OPC組。 AddOPCGroup()函數返回一個唯一指定某個組的指針,這個指針對于其他的Wtclient函數是必需的。傳送給做AddOPCGroup()參數的值由控制應用端決定,對于每個組都是明確的。Name不會被服務器采用,可以任意選取。pRate是一個指針,指向對象為所需刷新率(單位為毫秒),服務器端用這個刷新率提供客戶端刷新后的返回值。 |
| 函數 | BOOL? ChangeOPCGroupState (HANDLE hConnect, HANDLE hGroup, BOOL Active)); |
| 說明 | 應用這個函數可以將指定的OPC組設定為激活狀態(Active = TRUE)或者無效狀態(Active = FALSE)。 |
| 函數 | HANDLE? AddOPCItem (HANDLE hConnect, HANDLE hGroup, LPCSTR ItemName); void? RemoveOPCItem (HANDLE hConnect, HANDLE hGroup, HANDLE hItem); |
| 說明 | 如果想要給指定的OPC組添加一個項目,只需使用組的指針以及待添加項目的標簽名調用AddOPCItem函數即可,該函數會返回一個唯一標識指定項目的指針。如果請求的項目不存在,則函數將會返回INVALID_HANDLE_VALUE。當關閉一個組的時候,需要調用 RemoveOPCItem()函數來清理已分配的內存 |
| 函數 | BOOL? SetItemUpdateHandle (HANDLE hConnect,? HANDLE hGroup, HANDLE? hItem, HANDLE hUpdate); |
| 說明 | SetItemUpdateHandle()函數允許客戶端應用定義一個獨有的指針,供服務器在數據更新回調時使用。這個函數的提供是為了給客戶端應用提供便利,以便于進行更有效率的程序設計,并可以對服務器提供的新數據進行操作。 |
| 函數 | BOOL GetOPCItemNameFromHandle (HANDLE hConnect, HANDLE hGroup, HANDLE hItem, char *pBuf, int BufSize); |
| 說明 | 這個函數允許客戶端應用通過傳遞AddOPCItem函數返回的指針來獲取OPC項目名。 |
| 函數 | BOOL GetOPCItemType (HANDLE hConnect, HANDLE hGroup, LPCSTR ItemName, VARTYPE *pType, DWORD *pAccessRights); |
| 說明 | 這個函數返回了指定項目的本地變量類型以及訪問權限。 |
| 函數 | int? NumberOfItemProperties (HANDLE hConnect, LPCSTR ItemName); |
| 說明 | 這個函數返回了和指定標簽相關的OPC項目屬性數量 |
| 函數 | BOOL? GetItemPropertyDescription (HANDLE hConnect, int PropertyIndex, DWORD *pPropertyID, VARTYPE *pVT, BYTE *pDescr, int BufSize); |
| 說明 | 遍歷NumberOfItemProperties()函數創建的列表,就可以獲取每個項目屬性的描述 |
?
| 函數 | BOOL? ReadPropertyValue (HANDLE hConnect, LPCSTR Itemname, DWORD PropertyID, VARIANT *pValue); |
| 說明 | ReadPropertyValue()函數可以返回一個項目屬性的當前值。 |
6、從服務器獲取標簽值更新(Getting updated Tag Values from the Server)
| 函數 | BOOL? EnableOPCNotification (HANDLE hConnect, NOTIFYPROC lpCallback); |
| 說明 | 如果你的應用支持回調,當連接的服務器發出了標簽數據值改變的通知時,WTclient會提供回調操作。回調函數的原型定義如下 |
| 函數 | void CALLBACK EXPORT OPCUpdateCallback (HANDLE hGroup, HANDLE hItem, VARIANT *pVar, ???????????????????????????????????????????????????????????????????????FILETIME timestamp, DWORD quality) |
| 說明 | 由組指針和項目指針確定的標簽的值、時間戳以及OPC品質標簽都會在回調過程中提供給應用。。 |
?
| 函數 | BOOL? ReadOPCItem (HANDLE hConnect, HANDLE hGroup, HANDLE hItem, VARIANT *pVar, ???????????????????????????????????FILETIME *pTimeStamp, DWORD *pQuality); |
| 說明 | 如果WTclient已經配置為維護一個內部標簽緩沖,控制應用可以使用ReadOPCItem函數讀出當前標簽的值。如果請求的項目不可用(或者沒有選定DLL緩沖),函數會返回FALSE。 |
| 函數 | HRESULT? ReadOPCItemFromDevice (HANDLE hConnect, HANDLE hGroup, HANDLE hItem, VARIANT *pVar, FILETIME *pTimeStamp, DWORD *pQuality); |
| 說明 | 這個函數執行了一個直接同步讀取服務器端指定項目的操作。 |
7、將標簽值寫入到服務器(Writing Tag Values to the Server)
| 函數 | DWORD? WriteOPCItem (HANDLE hConnect, HANDLE hGroup, HANDLE hItem, VARIANT *pVar, BOOL DoAsync); |
| 說明 | Writing new data to the connected Server is accomplished via the WriteOPCItem function.? The identifying handles for the group and item must be supplied as well as the data to be written.? The DoAsync parameter instructs the dll to perform an Asynchronous write to the Server.? For Synchronous writes, the return value indicates the hResult returned from the server and a non-zero value indicates an error.? For Asynchronous writes, the return value is a Transaction ID that defines the asynchronous callback handle and a zero value indicates an error. |
8、其他函數和回調函數(Miscellaneous Functions and callbacks)
| 函數 | BOOL? GetSvrStatus (HANDLE hConnect, OPCSERVERSTATUS *pSvrStatus, int VendorInforBufSize); |
| 說明 | The current status of the identified server is returned in the supplied buffer. |
| 函數 | BOOL? SetClientName (HANDLE hConnect, LPCSTR Name); |
| 說明 | This function allows the client to define a name to designate the connection with the server. |
| 函數 | BOOL? EnableErrorNotification (ERRORPROCAPI? lpCallback); |
| 說明 | The WTClient dll can notify the client application of errors that occur during processing of data interface calls to an OPC Server.? Error messages may be generated by the dll if unexpected situations occur during processing of a user request.? The default operation of the dll is to display a user message in a dialog box describing the condition.? In most cases, it is more appropriate for the application itself to handle the error message rather than having the dialog generated by the dll.? If Error Notification is enabled, control will be passed to the application via the following callback: |
| 函數 | void CALLBACK EXPORT ErrorMsgCallback (DWORD hResult, char *pMsg); |
| 說明 | A textual description of the error is contained in pMsg along with the resultant hResult. |
| 函數 | BOOL? EnableClientEventMsgs (EVENTMSGPROC? lpCallback); |
| 說明 | The WTClient dll can also notify the client application of various events as they occur during operation of the connection with the server.? These events are basically used for debugging and represent normal activity associated with processing individual interfaces, (such as entering and exiting a particular function).? Generally, the client application does not need to be made aware of theses events, however, if it wishes to provide a low level description of how the connection is functioning, it can receive these debugging statements as they occur by enabling Event Msgs. |
| 函數 | void CALLBACK EXPORT EventMsgCallback (char *pMsg); |
| 說明 | A textual description of the event is contained in pMsg. |
| 函數 | BOOL? EnableShutdownNotification (HANDLE hConnect, SHUTDOWNPROCAPI? lpCallback); |
| 說明 | If a connected OPC Server wishes to shutdown, it may request that any or all clients disconnect.? The Client application should enable the shutdown notification within the WTClient dll to handle this request and terminate the connection. |
| 函數 | void CALLBACK EXPORT ShutdownCallback (HANDLE hConnect); |
| 說明 | The hConnect parameter identifies the server connection that is requesting the disconnect. |
9、創建警報/事件訂閱機制(Creating an Alarms & Events Subscription)
| 函數 | BOOL Create_AE_Subscription (HANDLE hConnect, HANDLE SubscriptionHandle,DWORD *pBufferTime, DWORD *pMaxSize); BOOL WINAPI EnableAECallback (AE_PROC lpCallback); BOOL WINAPI EnableExtendedAECallback (AE_PROC_EX lpCallback); |
| 說明 | To begin receiving alarm and event messages from the connected server, a subscription must be in initiated and a callback procedure defined.? The application passes a SubscriptionHandle that the server uses to identify the connection to the callback.? pBufferTime and pMaxSize refer to the requested update frequency and the maximum number of events to be exchanged at one time.? (Note:? These values are for efficiency purposes only.? If the standard callback is used, the WTClient DLL will set up the connection and callback to the server.? The DLL will always issue a single callback to the application per event message, regardless of how many event messages are in the server callback to the dll.? If the extended version of the callback is used, these parameters have the effect as described in the OPC A&E Specification with respect to the ONEVENTSTRUCT.) ? |
One of two callback procedures may be defined, depending upon the complexity of the client application.? Teh standard callback procedure is prototyped as follows:
| void CALLBACK EXPORT AECallback (HANDLE SubscriptionHandle, char *pSource, |
?
| 函數 | void CALLBACK EXPORT AECallback (HANDLE SubscriptionHandle, char *pSource, FILETIME timestamp, char *pMsg, DWORD severity); |
| 說明 | The SubscriptionHandle is the same as created by the subscription,? pSource identifies the source of the event and pMsg is the description. |
If additional information regarding the alarm & event notification, the client must enable the extended version of the callback:
| 函數 | void CALLBACK EXPORT ExtendedAECallback (HANDLE SubscriptionHandle, BOOL bRefresh, BOOL bLastRefresh, DWORD dwCount,??????????????????????????????????????????????????????????????????????? ONEVENTSTRUCT *pEvents) |
| 說明 | Notice that the extended version of the A&E Callback contains the unfiltered parameter list as returned by the server OnEvent Interface, and may contain more than one event message. |
?
10、A&E服務器直接調用接口(A&E Server Direct Interface Calls)
The following functions are provided to allow the client application to call directly into the attached A&E Server.? The parameter list is passed directly to the associated Server Interface without change and the resulting returned values originate from the server itself.? The WTClient.dll does not modify these parameters in any way and their definition is described in the OPC Alarms & Events Specification available from the OPC Foundation.? Please refer to this specification for a detailed description of each of the following functions.
| BOOL? GetAESvrStatus (HANDLE hConnect, OPCEVENTSERVERSTATUS *pSvrStatus, |
The current status of the identified server is returned in the supplied buffer.??
| BOOL? Refresh_AE_Subscription (HANDLE hConnect, HANDLE hSubscription); |
This function allows the client to request a refresh of the defined A&E subscription from the server.??
| HRESULT? AckCondition (HANDLE hConnect, DWORD dwCount, LPWSTR szAcknowledgerID, |
The client uses the AckCondition method to acknowledge one or more conditions in the Event Server.? The client receives event notifications from conditions via the IOPCEventSink::OnEvent callback.? This AckCondition method specifically acknowledges the condition becoming active or transitioning into a different sub-condition (and no other state transition of the condition).? One or more conditions belong to a specific event source – the source of the event notification.? For each condition-related event notification, the corresponding Source, Condition Name, Active Time and Cookie? is received by the client as part of the OnEvent callback parameters.? (Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
| HRESULT? EnableConditionByArea (HANDLE? hConnect, DWORD? dwNumAreas, LPWSTR *pszAreas); |
Places all conditions for all sources within the specified process areas into the enabled state.? Therefore, the server will now generate condition-related events for these conditions.?
The effect of this method is global within the scope of the event server.? Therefore, if the server is supporting multiple clients, the conditions are enabled for all clients, and they will begin receiving the associated condition-related events. (Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
| HRESULT EnableConditionBySource (HANDLE? hConnect, DWORD? dwNumSources, LPWSTR *pszSources); |
Places all conditions for the specified event sources into the enabled state.? Therefore, the server will now generate condition-related events for these conditions.?
The effect of this method is global within the scope of the event server.? Therefore, if the server is supporting multiple clients, the conditions are enabled for all clients, and they will begin receiving the associated condition-related events. (Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
| HRESULT DisableConditionByArea (HANDLE? hConnect, DWORD? dwNumAreas, LPWSTR? *pszAreas); |
Places all conditions for all sources within the specified process areas into the disabled state.? Therefore, the server will now cease generating condition-related events for these conditions.?
The effect of this method is global within the scope of the event server.? Therefore, if the server is supporting multiple clients, the conditions are disabled for all clients, and they will stop receiving the associated condition-related events. (Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
| HRESULT DisableConditionBySource (HANDLE? hConnect, DWORD dwNumSources, LPWSTR? *pszSources); |
Places all conditions for the specified event sources into the disabled state.? Therefore, the server will no longer generate condition-related events for these conditions.?
The effect of this method is global within the scope of the event server.? Therefore, if the server is supporting multiple clients, the conditions are disabled for all clients, and they will stop receiving the associated condition-related events. (Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
| HRESULT GetFilter? (HANDLE? hConnect, HANDLE? hSubscription, DWORD? *pdwEventType, |
Returns the filter currently in use for event subscriptions. (Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
| HRESULT SetFilter (HANDLE? hConnect, HANDLE hSubscription, DWORD dwEventType, |
Sets the filtering criteria to be used for the event subscription.
Events may be selected using the following criteria:
? Type of event, i.e. simple, condition, or tracking.
? Event categories
? Lowest severity, i.e. all events with a severity greater than or equal to the specified severity.
? Highest severity, i.e. all events with a severity less than or equal to the specified severity.
? Process areas
? Event Sources
A list of values for a single criterion are logically ORed together (e.g. if two event categories are specified, event notifications for both categories will be received).? If multiple criteria are specified, they will be logically ANDed together, i.e. only those events satisfying all criteria will be selected.? An example is specifying both lowest severity and highest severity will result in the selection of events with severities lying between the two values.
An OPCEventSubscription object has only one filter.
?(Refer to the OPC Alarms & Events Specification for a detailed description of the parametr list and return value.)
?
總結
以上是生活随笔為你收集整理的OPC:客户端开发——应用WTclient.dll使用手册部分中文版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#读取计算机的机器码、网卡、设备码
- 下一篇: 宽带服务器维护查询,网络连接状态查询方法