Home > Back-end >  The return value is NULL why OPC OnDataChange
The return value is NULL why OPC OnDataChange

Time:10-06

Recently made an opc client program, using Delphi7.0, using OPCDAauto. DLL, the import of Delphi, put controls on, then connect to the add, but when OnDataChange NumItems: returns the number of changes in the value is correct, but ClientHandles ItemValues, Qualities,
TimeStamps of the return value is NULL, don't know is why?

O master,


The unit s;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, OPCAutomation_TLB OleServer, ActiveX.

Type
TForm1=class (TForm)
Btn1: TBitBtn;
OPCServer1: TOPCServer;
OPCGroup1: TOPCGroup;
Opcgrps1: TOPCGroups;
Procedure btn1Click (Sender: TObject);
Procedure OPCGroup1DataChange (ASender: TObject; TransactionID,
NumItems: Integer; Var ClientHandles ItemValues, Qualities,
TimeStamps: OleVariant);
Private
{Private declarations}
Public
{Public declarations}
FOPCItems: OPCItems;//interface
FOPCItem: OPCItem;//interface
end;

Var
Form1: TForm1;

Implementation

{$R *. DFM}

Procedure TForm1. Btn1Click (Sender: TObject);

Var
NumItems: Integer;
OVItemID: OleVariant;
OVClientHandles: OleVariant;
OVServerHandles: OleVariant;
OVErrors: OleVariant;
OleErr: string;
The begin
OPCServer1. Connect1 (' MMTSET OPC Server ');
OPCGroup1. ConnectTo (OPCServer1. OPCGroups. Add (" test "));
OPCGroup1. UpdateRate:=1000;
OPCGroup1. IsActive:=True;
OPCGroup1. IsSubscribed:=True;//events support


FOPCItems:=OPCGroup1. OPCItems;

NumItems:=1;

OVItemID:=VarArrayCreate ([1, 1], varOleStr);
OVItemID [1] :='a';

OVClientHandles:=VarArrayCreate ([1, 1], varInteger);
OVClientHandles [1] :=1;

OVServerHandles:=VarArrayCreate ([1, 1], varInteger);
OVServerHandles [1] :=0;//initialize

OVErrors:=VarArrayCreate ([1, 1], varOleStr);
OVErrors:=EmptyParam;//initialize

FOPCItems. AddItems (
NumItems,
PSafeArray (TVarData (OVItemID). VOleStr),
PSafeArray (TVarData (OVClientHandles). VInteger),
PSafeArray (TVarData (OVServerHandles). VInteger),
PSafeArray (TVarData (OVErrors). VInteger),
EmptyParam,
EmptyParam
);

If OVServerHandles [1] & gt; 0 then
FOPCItem:=FOPCItems GetOPCItem (OVServerHandles [1])
The else
The begin
//raise EOleError. Create (' failure: connection + cnItemName + # 13 # 10 + OleErr);
Caption:='error'.
End
end;

Procedure TForm1. OPCGroup1DataChange (ASender: TObject; TransactionID,
NumItems: Integer; Var ClientHandles ItemValues, Qualities,
TimeStamps: OleVariant);
Var nSize: Integer;
P: Pointer;
S: a string;
The begin
If ClientHandles=null then
The Exit;

NSize:=VarArrayHighBound (ClientHandles, 1) - VarArrayLowBound (ClientHandles, 1) + 1;
SetLength (s, nSize);
P:=VarArrayLock (ClientHandles);
Try
Move (p ^, PChar (s) ^, nSize);
The finally
VarArrayUnlock (ClientHandles);
end;

Caption:=s;

end;

end.

CodePudding user response:

No one reply

CodePudding user response:

You check the TLB Delphi compiled class files, you the same question, and I found that is a compile error, procedure TOPCGroup. InvokeEvent (DispID: TDispID; Var Params: TVariantArray); Here, you check

CodePudding user response:

If Assigned (FOnDataChange) then
FOnDataChange (Self,
Params [0],
Params [1],
OleVariant (TVarData (Params [2]),
OleVariant (TVarData (Params [3]),
OleVariant (TVarData (Params [4]),
OleVariant (TVarData (Params [5])));

CodePudding user response:

OPC, or find a stable client is better, do more troublesome

CodePudding user response:

Meet the same problem, is solved

CodePudding user response:

Write an example before, can consult
http://bbs.csdn.net/topics/330170980
  • Related