牛
牛头12236
Unregistered / Unconfirmed
GUEST, unregistred user!
先看看以下程序
procedure TABCommObj.AsyncWrite(TransactionID: Integer);
var
vItemIDs, vServerHandles, vClientHandles, vErrors: Variant;
numItemsInPacket: Longint;
__numGroups, __numItemInLast, __numItemsInPacket: Longint;
vValues, vValue: Variant;
i, j, Offset: Integer;
begin
//
// 每一万个分组
//
if TransactionID = FNumGroups then
numItemsInPacket := FNumItemsInLast
else
numItemsInPacket := GROUP_OFFSET;
//
// 按一百个偏移
//
__numGroups := numItemsInPacket div MAX_ITEM_PAR_ELEMENT;
__numItemInLast := numItemsInPacket mod MAX_ITEM_PAR_ELEMENT;
if __numItemInLast = 0 then
__numItemInLast := MAX_ITEM_PAR_ELEMENT
else
Inc(__numGroups);
vValues := VarArrayCreate([1, __numGroups], varVariant);
vItemIDs := VarArrayCreate([1, __numGroups], varOleStr);
vServerHandles := VarArrayCreate([1, __numGroups], varInteger);
vClientHandles := VarArrayCreate([1, __numGroups], varInteger);
vErrors := VarArrayCreate([1, __numGroups], varInteger);
for i := 1 to __numGroups do
begin
if i = __numGroups then
__numItemsInPacket := __numItemInLast
else
__numItemsInPacket := MAX_ITEM_PAR_ELEMENT;
Offset := (TransactionID - 1) * GROUP_OFFSET + (i - 1) * MAX_ITEM_PAR_ELEMENT;
vItemIDs := Format('[%s]%s[%d],L%d', [FTopic, FTag, Offset, __numItemsInPacket]);
vClientHandles := i;
vValue := VarArrayCreate([1, __numItemsInPacket], varVariant);
for j := 1 to __numItemsInPacket do
begin
vValue[j] := 1;//FPCurrValue^; // 该指针有些危险
//Inc(FPCurrValue);
end; // for
vValues := vValue;
end; // for
FOPCGroup.OPCItems.AddItems(__numGroups,
PSafeArray(TVarData(vItemIDs).VOleStr),
PSafeArray(TVarData(vClientHandles).VInteger),
PSafeArray(TVarData(vServerHandles).VInteger),
PSafeArray(TVarData(vErrors).VInteger),
EmptyParam,
EmptyParam);
try
FOPCGroup.AsyncWrite(__numGroups,
PSafeArray(TVarData(vServerHandles).VInteger),
PSafeArray(TVarData(vValues).VArray),
PSafeArray(TVarData(vErrors).VInteger),
TransactionID,
FCancelID);
except
on E: Exception do MessageBox(0, PChar(E.Message), '', MB_OK);
end;
end;
procedure TABCommObj.AsyncWrite(TransactionID: Integer);
var
vItemIDs, vServerHandles, vClientHandles, vErrors: Variant;
numItemsInPacket: Longint;
__numGroups, __numItemInLast, __numItemsInPacket: Longint;
vValues, vValue: Variant;
i, j, Offset: Integer;
begin
//
// 每一万个分组
//
if TransactionID = FNumGroups then
numItemsInPacket := FNumItemsInLast
else
numItemsInPacket := GROUP_OFFSET;
//
// 按一百个偏移
//
__numGroups := numItemsInPacket div MAX_ITEM_PAR_ELEMENT;
__numItemInLast := numItemsInPacket mod MAX_ITEM_PAR_ELEMENT;
if __numItemInLast = 0 then
__numItemInLast := MAX_ITEM_PAR_ELEMENT
else
Inc(__numGroups);
vValues := VarArrayCreate([1, __numGroups], varVariant);
vItemIDs := VarArrayCreate([1, __numGroups], varOleStr);
vServerHandles := VarArrayCreate([1, __numGroups], varInteger);
vClientHandles := VarArrayCreate([1, __numGroups], varInteger);
vErrors := VarArrayCreate([1, __numGroups], varInteger);
for i := 1 to __numGroups do
begin
if i = __numGroups then
__numItemsInPacket := __numItemInLast
else
__numItemsInPacket := MAX_ITEM_PAR_ELEMENT;
Offset := (TransactionID - 1) * GROUP_OFFSET + (i - 1) * MAX_ITEM_PAR_ELEMENT;
vItemIDs := Format('[%s]%s[%d],L%d', [FTopic, FTag, Offset, __numItemsInPacket]);
vClientHandles := i;
vValue := VarArrayCreate([1, __numItemsInPacket], varVariant);
for j := 1 to __numItemsInPacket do
begin
vValue[j] := 1;//FPCurrValue^; // 该指针有些危险
//Inc(FPCurrValue);
end; // for
vValues := vValue;
end; // for
FOPCGroup.OPCItems.AddItems(__numGroups,
PSafeArray(TVarData(vItemIDs).VOleStr),
PSafeArray(TVarData(vClientHandles).VInteger),
PSafeArray(TVarData(vServerHandles).VInteger),
PSafeArray(TVarData(vErrors).VInteger),
EmptyParam,
EmptyParam);
try
FOPCGroup.AsyncWrite(__numGroups,
PSafeArray(TVarData(vServerHandles).VInteger),
PSafeArray(TVarData(vValues).VArray),
PSafeArray(TVarData(vErrors).VInteger),
TransactionID,
FCancelID);
except
on E: Exception do MessageBox(0, PChar(E.Message), '', MB_OK);
end;
end;