【发布时间】:2014-10-01 08:10:45
【问题描述】:
我有一个使用 OPC 的线程应用程序。此线程与 OPC 工厂服务器建立连接并接收自动化通知。
OPC : v 3.40.2808.0
语言:Pascal,IDE:Delphi XE 2
与服务器的连接良好,通知即将到来。但有时,服务并不完美,我的应用程序没有收到所有通知。
我不确定问题出在我的代码中,但请稍微解释一下:
GestOPC = class(TThread)
contructor Create(suspendu:boolean);
destructor Destroy;override;
procedure C******; // Called by connexion
private
procedure Execute;override;
procedure CallGestOPCMainLabelAtt(const s : string);// IU Calling
procedure Call****;
procedure Call****;
procedure Call****;
public
sCallIHM :string;
***: boolean;
***: LongInt;
ThreadListNotif: TThreadList,
ServeurIF : OPCServer;
OPCDataCallBack:IOPCDataCallBack;
Buffer_notif : ^TNotif;
procedure connexion;
end;
TOPCDataCallback = class ( TInterfacedObject, IOPCDataCallback)
public
function OnDataChange(.....)HResult;stdcall;
function OnReadComplete(.....)HResult;stdcall;
function OnWriteComplete(.....)HResult;stdcall;
function OnCancelComplete(.....)HResult;stdcall;
end;
OPC's Thread的OnDataChange:接收通知的事件
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,iCountBadItem:integer;
Begin
if not bClosing then ///It's to not do anything if the app is closing.
begin
//Initialisation
iCountBadItem := 0; //this is for count during debbugging all notification unreadable
Result := s_OK;
ClientItems := POPCHANDLEARRAY (phClientItems);
Values := POleVariantArray(pvValues);
for i := 0 to dwCount -1 do
begin
if Qualities[i] = OPC_QUALITY_GOOD then
begin
new(ClientOPC.Buffer_notif);
ClientOPC.Buffer_notif^.groupe_handle.indice_type1 := hGroup;
ClientOPC.Buffer_notif^.item_client := ClientItems[i];
ClientOPC.Buffer_notif^.valeur_item := Value[i];
ClientOPC.ThreadListNotif.Add(ClientOPC.Buffer_Notif);
//This list is used in another Thread with LockList for use the notification.
end
else
iCountBadItem := iCountBadItem+1;
end;
end;
我的 OPC 线程执行程序是空的,以确保我有一个良好的接收:
procedure GestOPC.Execute;
begin
NameThreadForDebugging('GestOPC');
while (not Terminated) do
begin
sleep(100);
end;
end;
我的线程是由我的主窗体的 FormCreate 事件创建的:
ClientOPC := GestOPC.Create(false);
ClientOPC.FreeOnTerminate := false;
连接是由 ButtonClick' 事件启动的:
ClientOPC.Connexion;
函数连接从创建接收器TOPCDataCallBack开始,与Ofs建立连接,对数据库进行SQL调用,最后动态修改我的接口(OPC在线程上的原因是为了不冻结我的连接期间的接口)。
对于我的 IU Calling,我使用这个结构:
sCallIHM := "Ceci est un exemple de mon travail ;) ";
Queue(
procedure
begin
CallGestOPCMainLabelAtt( sCallIHM );
end ) ;
我如何升级它才能接收我的所有消息?
感谢阅读 =)
【问题讨论】:
标签: multithreading delphi opc