VB, VC development OPC client must read!
Author: szTaoyanbin, 2004-2-8 10:12:00 published in: "OPC Forum" reply to a total of 40 people, 7055 views
Develop OPC clients must read !!!!!!!! STEP.1 If the program wants to call a function of the COM library must first log in to the COM function Coinitialize () You can complete this feature Cogetmalloc () You can get a point to COM Memory Management Interface Pointer HRESULT RL; RL = Coinitialize (NULL); RL = COGETMALLOC (MemctX_task, & g_pimalloc); Step.2 Each COM server has a progID to get a global unique CLSID () function can be implemented This transformation ProgID uses variable szname to pass the parameter passed in the sample program its value is L "opc.simaticnet" m_popc rl = clsidFromProgid (szname, & clsid); step.3 cocreateInstance () function creates a class instance its CLSID value settings as follows : R2 = COCREATEINSTANCE (CLSID, NULL, CLSCTX_LOCAL_SERVER, IID_IUNKOWN, (VOID **); The result of this program is a pointer variable pointing to the server object IUNKNOWN interface Punk Step.4 From the iUnkown interface through the queryinterface () method to get The pointer of other interfaces HRESULT R3; R3 = punk-> queryinterface (IID_IOPCSERVER, (Void **) & m_popc); the result of this program is to get a pointer (variable m_popc) Step.5 Iopcserver interface pointing to the server object IOPCSERVER interface. AddGroup's AddgROUP () method to create OPC group m_pItemMgt) HRESULT r1; r1 = m_Popc-> AddGroup (szName, TRUE, 500,1, & TimeBias, & PercDeadband, dwLCID, & m_GrpServerHandle, & RevUpRate, IID_IOPCItemMgt, (LPUNKNOWN *) & m_pItemMgt); this program The execution result is that a group with a specified name and attribute is to have a process group object that is required in the returned parameter. The pointer of the mouth is here Iopcitemmgt (Variable STEP.6 IOPCITEMMGT interface has an AddItem () method can create OPC item HRESULT R1; R1 = m_pitemgt-> additems (NumItems, Pitems, & M_PITRESULT, & PERRORS); the result of this program is created Specific number of items of special properties In addition, the event structure variable m_pitResult Server handle target system is also assigned to execute the pointer to execute the required operation requires a pointer to the IopciteMGT interface, if To perform asynchronous communication, you need to point to the Iopcasyncio interface. HRESULT RL; R1 = M_PITEMMGT-> QueryFace (IID_IOPCASYNCIO, (Void * *) & phasencio); two ways to read () and Write () can read and write Numerical R2 = Pasyncio-> Read (m_dwconnection, opc_ds_cache, dwnumitems, phserver, & m_transactionid, "); the execution result of this program is that the OPC item is sent to the IadviseSink interface Step.7 must be before the program stops running. Delete the created OPC objects and releases memory to the various interfaces used in the currently 5.4 r1 = m_pitemgt->
RemoveItems (dwnumitems, phser, & perrors); r1 = m_popc-> removegroup (m_grpserverhandle, true); m_pitemmgt-> release (); m_popc-> release (); the following is "VB, VC development OPC client must read!" Reply:
Sztaoyanbin:
2003-12-30 13:47:00
Alas !!!!!!! VB DEMO !!!!! STEP_1: Init Server !! Set Svr = CreateObject (ProgID $) STEP_2: AddGroup !! Set Group = Svr.AddGroup "szTaoYanBinGRP" false RateRequested, GroupClientHdl, PercentDeadband , dwLangId_ENGLISH, GroupServerHdl, RateRevised Set PtrItemMgt = Group STEP_3: AddItems !!! ItemsActive (0) = True: ItemsActive (1) = True PtrItemMgt.AddItems NbrItems, ItemsIDs, ItemsActivity, ItemsClientHdls, ItemsSvrHdls, ItemsErrors, ItemsObjects, accessPath STEP_4: Read & Write Items !!!! Set ptrSyncIO = group WHILE (true) PtrSyncIO.OPCRead OPC_DS_DEVICE, NbrItems, ItemsSvrHdls, pValues, pQualities, pTimeStamps, ItemsErrors WEND STEP_5: Free Group & Items !!!!!! Svr.RemoveGroup GroupServerHdl, False Set Group = Nothing Set SVR = Nothings OK !!!!!! If you think this is worthy of value, please recommend it as the essence !!!!!!!!!!!!!!!!!!!!!!!!!!! Sztaoyanbin:
2003-12-30 15:02:00