⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 groupunit.pas

📁 delphi2007 opcserver
💻 PAS
📖 第 1 页 / 共 3 页
字号:
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 aList:=nil;
 result:=S_OK;
 if (clItems = nil) or (clItems.count = 0) then
  begin
   result:=S_FALSE;
   Exit;
  end;

 try
  aList:=TList.Create;
  if (aList = nil) then
   begin
    result:=E_OUTOFMEMORY;
    Exit;
   end;

  for i:= 0 to clItems.count - 1 do
   begin
    aAttr:=TOPCItemAttributes.Create;
    if (aAttr = nil) then
     begin
      result:=E_OUTOFMEMORY;
      Exit;
     end;
    TOPCItem(clItems[i]).FillInOPCItemObject(aAttr);
    aList.Add(aAttr);
   end;

  ppUnk:=TOPCItemAttEnumerator.Create(aList);

 finally
  if (aList <> nil) and (aList.count > 0) then
   begin
    for i:= 0 to aList.count - 1 do
     TOPCItemAttributes(aList[i]).Free;
    aList.Free;
   end;
 end;
end;
//IOPCItemMgt end

//IOPCGroupStateMgt begin
function TOPCGroup.GetState(out pUpdateRate:DWORD; out pActive:BOOL; out ppName:POleStr;
                  out pTimeBias:Longint; out pPercentDeadband:Single; out pLCID:TLCID;
                  out phClientGroup:OPCHANDLE; out phServerGroup:OPCHANDLE):HResult;stdcall;
begin
 pUpdateRate:=requestedUpdateRate;
 pActive:=groupActive;
 ppName:=StringToLPOLESTR(tagName);
 pTimeBias:=timeBias;
 pPercentDeadband:=percentDeadband;
 pLCID:=lang;
 phClientGroup:=clientHandle;
 phServerGroup:=serverHandle;
 result:=S_OK;
end;

function TOPCGroup.SetState(pRequestedUpdateRate:PDWORD;
                           out pRevisedUpdateRate:DWORD;
                           pActive:PBOOL;
                           pTimeBias:PLongint;
                           pPercentDeadband:PSingle;
                           pLCID:PLCID;
                           phClientGroup:POPCHANDLE):HResult; stdcall;
begin
//the group might have been marked as removed
 if groupRemovedRequest then
  begin
   result:=E_FAIL;
   Exit;
  end;

 result:=S_OK;
 if Assigned(pRequestedUpdateRate) then
  if (ValidateRequestedUpDateRate(pRequestedUpdateRate^) <> pRequestedUpdateRate^) then
   result:=OPC_S_UNSUPPORTEDRATE;

 if Assigned(pTimeBias) then
  ValidateTimeBias(pTimeBias);

 if Assigned(pPercentDeadband) then
  percentDeadband:=pPercentDeadband^
 else
  percentDeadband:=0;

 if Assigned(pLCID) then
  lang:=pLCID^;

 if Assigned(phClientGroup) then
  clientHandle:=phClientGroup^;

 if (addr(pRevisedUpdateRate) <> nil) then
  pRevisedUpdateRate:=requestedUpdateRate;

 if Assigned(pActive) then
  begin
   if pActive^ and not groupActive then               //from not active to active
    GroupActiveFromInactive;
   groupActive:=pActive^;
  end;

end;

function TOPCGroup.SetName(szName:POleStr):HResult;stdcall;
begin
 result:=S_OK;

 if groupRemovedRequest then
  begin
   result:=E_FAIL;
   Exit;
  end;

 if (length(szName) = 0) then
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 if (servObj.IsGroupNamePresent(servObj.grps,szName) <> -1) then
  begin
   result:=OPC_E_DUPLICATENAME;
   Exit;
  end;
 tagName:=szName;
end;

function TOPCGroup.CloneGroup(szName:POleStr; const riid: TIID; out ppUnk:IUnknown):HResult;stdcall;
var
 s1:string;
 i:integer;
begin
 if not (IsEqualIID(riid,IID_IOPCGroupStateMgt) or IsEqualIID(riid,IID_IUnknown)) then
  begin
   result:=E_NOINTERFACE;
   Exit;
  end;

 s1:=szName;
 if length(s1) <> 0 then
  if servObj.IsNameUsedInAnyGroup(s1) then
   begin
    result:=OPC_E_DUPLICATENAME;
    Exit;
   end;

 i:=0;
 if servObj.IsNameUsedInAnyGroup(s1) then
  repeat
   s1:=s1 + IntToStr(GetTickCount);
   Inc(i);
  until (not servObj.IsNameUsedInAnyGroup(s1)) or (i > 9);

 if (i > 9) then
  begin
   result:=E_FAIL;
   Exit;
  end;

 ppUnk:=servObj.CloneAGroup(s1,self,result);
 if (result <> 0) then
  begin
   ppUnk:=nil;
   Exit;
  end;
 result:=S_OK;
end;
//IOPCGroupStateMgt end

//IOPCSyncIO begin
function TOPCGroup.Read(dwSource:OPCDATASOURCE; dwCount:DWORD; phServer:POPCHANDLEARRAY;
                        out ppItemValues:POPCITEMSTATEARRAY; out ppErrors:PResultList):HResult;stdcall;
var
 i,x:integer;
 memErr:boolean;
begin
 if (dwCount < 1) then
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 if not (dwSource in [OPC_DS_CACHE,OPC_DS_DEVICE]) then
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 ppItemValues:=POPCITEMSTATEARRAY(CoTaskMemAlloc(dwCount * sizeof(OPCITEMSTATE)));
 memErr:=boolean(ppItemValues = nil);

 if not memErr then
  begin
   ppErrors:=PResultList(CoTaskMemAlloc(dwCount*sizeof(HRESULT)));
   memErr:=boolean(ppErrors = nil);
  end;

 if memErr then
  begin
   if (ppItemValues <> nil) then
    CoTaskMemFree(ppItemValues);
   if (ppErrors <> nil) then
    CoTaskMemFree(ppErrors);
   result:=E_OUTOFMEMORY;
   Exit;
  end;

 FillChar(ppItemValues[0], dwCount * sizeOf(OPCITEMSTATE),#0);
 result:=S_OK;

 for i:= 0 to dwCount - 1 do
  if GetItemIndexFromServerHandle(phServer[i],x) then
   begin
//would do access rights check here

    ppErrors[i]:=S_OK;
    TOPCItem(clItems[x]).SyncRead(dwSource,ppItemValues[i],ppErrors[i]);

    if (ppErrors[i] <> S_OK) then
     result:=S_FALSE;

//from the cache the group and item must be active for good quality
    if (dwSource = OPC_DS_CACHE) then
     if not groupActive and TOPCItem(clItems[x]).bActive then
      ppItemValues[i].wQuality:=OPC_QUALITY_OUT_OF_SERVICE;

   end
  else
   begin
    result:=S_FALSE;
    ppErrors[i]:=OPC_E_INVALIDHANDLE;
   end;
end;

function TOPCGroup.Write(dwCount:DWORD; phServer:POPCHANDLEARRAY;
                         pItemValues:POleVariantArray; out ppErrors:PResultList):HResult;stdcall;
var
 i,x,vType:integer;
 ppServer:PDWORDARRAY;
begin
 if (dwCount < 1) then
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 ppErrors:=PResultList(CoTaskMemAlloc(dwCount * sizeof(HRESULT)));
 if ppErrors = nil then
  begin
   result:=E_OUTOFMEMORY;
   Exit;
  end;

 result:=S_OK;
 ppServer:=@phServer^;
 for i:= 0 to dwCount -1 do
  if GetItemIndexFromServerHandle(ppServer[i],x) then
   begin

//check that the type is valid
    vType:=VarType(pItemValues[i]);
    if (vType = varEmpty) or not IsVariantTypeOK(vType) then
     begin
      ppErrors[i]:=OPC_E_BADTYPE;
      result:=S_FALSE;
     end
    else if not TOPCItem(clItems[x]).isWriteAble then
     begin
      ppErrors[i]:=OPC_E_BADRIGHTS;
      result:=S_FALSE;
     end
    else
     begin
      TOPCItem(clItems[x]).WriteItemValue(pItemValues[i],ppErrors[i]);
     end;
   end
  else
   begin
    result:=S_FALSE;
    ppErrors[i]:=OPC_E_INVALIDHANDLE;
   end;
end;
//IOPCSyncIO end

//IOPCAsyncIO2 begin
function TOPCGroup.Read(dwCount:DWORD; phServer: POPCHANDLEARRAY; dwTransactionID:DWORD;
              out pdwCancelID:DWORD; out ppErrors:PResultList):HResult;stdcall;
var
 memErr:boolean;
 i,itemIndex:integer;
 aAsyncObj:TAsyncIO2;
begin
 if (ClientIUnknown = nil) then
  begin
   result:=CONNECT_E_NOCONNECTION;
   Exit;
  end;

 if (dwCount < 1) then
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 ppErrors:=PResultList(CoTaskMemAlloc(dwCount * sizeof(HRESULT)));
 memErr:=boolean(ppErrors = nil);

 if not memErr then
  begin
   aAsyncObj:=TAsyncIO2.Create(self,io2Read,dwTransactionID,
                               dwCount,OPC_DS_DEVICE);
   memErr:=boolean(aAsyncObj = nil);
  end;

 if not memErr then
  try
   aAsyncObj.ppServer:=CoTaskMemAlloc(dwCount * sizeof(longword));
  except
   result:=E_OUTOFMEMORY;
   Exit;
  end;

 if not memErr then
  begin
   pdwCancelID:=aAsyncObj.cancelID;        //was generated in TAsyncIO2.Create
   aAsyncObj.itemCount:=0;
   for i:= 0 to dwCount-1 do
    begin
     if GetItemIndexFromServerHandle(phServer[i],itemIndex) then
      begin
       aAsyncObj.ppServer[aAsyncObj.itemCount]:=itemIndex;
       Inc(aAsyncObj.itemCount);
       ppErrors[i]:=S_OK;
      end
     else
      ppErrors[i]:=OPC_E_INVALIDHANDLE;
    end;
  end;

 if memErr then
  begin
   if (ppErrors <> nil) then
    CoTaskMemFree(ppErrors);
   if Assigned(aAsyncObj) then
    FreeAndNil(aAsyncObj);
   result:=E_OUTOFMEMORY;
   Exit;
  end;

 if (aAsyncObj.itemCount < 1) then
  begin
   if Assigned(aAsyncObj) then
    FreeAndNil(aAsyncObj);
   result:=S_FALSE;
  end
 else
  begin
   asyncList.Add(aAsyncObj);
   result:=S_OK;
  end;
end;

function TOPCGroup.Write(dwCount:DWORD; phServer:POPCHANDLEARRAY;
                         pItemValues:POleVariantArray;
                         dwTransactionID:DWORD;
                         out pdwCancelID:DWORD;
                         out ppErrors:PResultList):HResult;stdcall;
var
 memErr:boolean;
 i,itemIndex:integer;
 aAsyncObj:TAsyncIO2;
begin
 if (ClientIUnknown = nil) then
  begin
   result:=CONNECT_E_NOCONNECTION;
   Exit;
  end;

 if (dwCount < 1) then
  begin
   result:=E_INVALIDARG;
   Exit;
  end;

 aAsyncObj:=TAsyncIO2.Create(self,io2Write,dwTransactionID,dwCount,0);
 memErr:=boolean(aAsyncObj = nil);

 if not memErr then
  begin
   ppErrors:=PResultList(CoTaskMemAlloc(dwCount*sizeof(HRESULT)));
   memErr:=boolean(ppErrors = nil);
  end;

 if not memErr then
  try
   aAsyncObj.ppServer:=CoTaskMemAlloc(dwCount * sizeof(longword));
   aAsyncObj.ppValues:=CoTaskMemAlloc(dwCount * sizeof(OleVariant));
   FillChar(aAsyncObj.ppValues[0], dwCount * sizeOf(OleVariant),#0);
  except
   result:=E_OUTOFMEMORY;
   Exit;
  end;

 if not memErr then
  begin
   pdwCancelID:=aAsyncObj.cancelID;        //was generated in TAsyncIO2.Create
   aAsyncObj.itemCount:=0;
   for i:= 0 to dwCount - 1 do
    begin
     if GetItemIndexFromServerHandle(phServer[i],itemIndex) then
      begin
       ppErrors[i]:=S_OK;
       aAsyncObj.ppServer[aAsyncObj.itemCount]:=itemIndex;
       try
        aAsyncObj.ppValues[aAsyncObj.itemCount]:=pItemValues[i];
       except

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -