?? opcdatacallback.~pas
字號:
unit OPCDataCallback;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ComObj,OPCtypes,OPCDA, OPCutils,ActiveX;
type
// 定義接收 IConnectionPointContainer 的數(shù)據(jù)返回類
TOPCDataCallback = class(TInterfacedObject, IOPCDataCallback)
public
//異步通訊連接后,當連接項的數(shù)據(jù)發(fā)生變化時,服務器自動調(diào)用此函數(shù)
function OnDataChange(dwTransid: DWORD; hGroup: OPCHANDLE;
hrMasterquality: HResult; hrMastererror: HResult; dwCount: DWORD;
phClientItems: POPCHANDLEARRAY; pvValues: POleVariantArray;
pwQualities: PWordArray; pftTimeStamps: PFileTimeArray;
pErrors: PResultList): HResult; stdcall;
//異步通訊連接并執(zhí)行異步讀操作后,當服務器接收到讀信息時,自動調(diào)用此函數(shù)返回數(shù)據(jù)
function OnReadComplete(dwTransid: DWORD; hGroup: OPCHANDLE;
hrMasterquality: HResult; hrMastererror: HResult; dwCount: DWORD;
phClientItems: POPCHANDLEARRAY; pvValues: POleVariantArray;
pwQualities: PWordArray; pftTimeStamps: PFileTimeArray;
pErrors: PResultList): HResult; stdcall;
//異步通訊連接并執(zhí)行異步寫操作后,當服務器執(zhí)行完數(shù)據(jù)寫如后,自動調(diào)用此函數(shù)返回寫操作的結(jié)果
function OnWriteComplete(dwTransid: DWORD; hGroup: OPCHANDLE;
hrMastererr: HResult; dwCount: DWORD; pClienthandles: POPCHANDLEARRAY;
pErrors: PResultList): HResult; stdcall;
//暫時沒有測試此功能
function OnCancelComplete(dwTransid: DWORD; hGroup: OPCHANDLE):
HResult; stdcall;
end;
implementation
// TOPCDataCallback 方法
function TOPCDataCallback.OnDataChange(dwTransid: DWORD; hGroup: OPCHANDLE;
hrMasterquality: HResult; hrMastererror: HResult; dwCount: DWORD;
phClientItems: POPCHANDLEARRAY; pvValues: POleVariantArray;
pwQualities: PWordArray; pftTimeStamps: PFileTimeArray;
pErrors: PResultList): HResult;
var
ClientItems: POPCHANDLEARRAY;
Values: POleVariantArray;
Qualities: PWORDARRAY;
I: Integer;
NewValue: string;
begin
Result := S_OK;
ClientItems := POPCHANDLEARRAY(phClientItems);
Values := POleVariantArray(pvValues);
Qualities := PWORDARRAY(pwQualities);
for I := 0 to dwCount - 1 do
begin
if Qualities[I] = OPC_QUALITY_GOOD then
begin
NewValue := VarToStr(Values[I]);
showmessage('新的返回數(shù)據(jù)從 item '+ inttostr(ClientItems[I])+'接收到,其值為: '+NewValue);
end
else begin
showmessage('新的返回數(shù)據(jù)從 item '+ inttostr(ClientItems[I])+'接收到,但數(shù)據(jù)質(zhì)量有問題');
end;
end;
end;
//此項中具體內(nèi)容為本人添加,可實現(xiàn)異步讀函數(shù)的回調(diào),功能考慮不完善(錯誤判斷等)
function TOPCDataCallback.OnReadComplete(dwTransid: DWORD; hGroup: OPCHANDLE;
hrMasterquality: HResult; hrMastererror: HResult; dwCount: DWORD;
phClientItems: POPCHANDLEARRAY; pvValues: POleVariantArray;
pwQualities: PWordArray; pftTimeStamps: PFileTimeArray;
pErrors: PResultList): HResult;
var
Values: POleVariantArray;
NewValue: string; //保存返回的數(shù)據(jù)
Qualities: PWORDARRAY;
I: Integer;
begin
Result := S_OK;
Values:= POleVariantArray(pvValues); //異步讀返回數(shù)據(jù)
Qualities:= PWORDARRAY(pwQualities); //異步讀返回數(shù)據(jù)質(zhì)量
for i:=0 to dwCount - 1 do
begin
NewValue := VarToStr(Values[i]);
if Qualities[i] = OPC_QUALITY_GOOD then
begin
ShowMessage('回調(diào)讀函數(shù)響應,其值為:'+NewValue);
// form1.ASyncreadEdit.Text:=NewValue;
end
else
ShowMessage('回調(diào)讀函數(shù)響應,但沒有讀到正確數(shù)據(jù)');
end;
end;
//此項中具體內(nèi)容為本人添加,可實現(xiàn)異步寫函數(shù)的回調(diào),功能考慮不完善(錯誤判斷等)
function TOPCDataCallback.OnWriteComplete(dwTransid: DWORD; hGroup: OPCHANDLE;
hrMastererr: HResult; dwCount: DWORD; pClienthandles: POPCHANDLEARRAY;
pErrors: PResultList): HResult;
var
WErrors:PResultList;
begin
WErrors:=PResultList(pErrors);
if WErrors[0] = S_OK then
ShowMessage('寫回調(diào)函數(shù)響應,異步寫完成')
else
ShowMessage('寫回調(diào)函數(shù)響應,但異步寫數(shù)據(jù)沒有正確寫入');
end;
//此函數(shù)暫時沒有測試
function TOPCDataCallback.OnCancelComplete(dwTransid: DWORD;
hGroup: OPCHANDLE): HResult;
begin
Result := S_OK;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -