[python-win32] COM Event Question

Steve Goldman steve_g@ix.netcom.com
Sat, 8 Mar 2003 13:18:07 -0500


This is a multi-part message in MIME format.

------=_NextPart_000_000E_01C2E575.2882F2C0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I'm having trouble seeing events from a python COM client application.  =
The COM=20
server is an "OPC Automation 2.0" object, which is used to communicate =
with industrial
control products.

After running the makepy script, I can see that the OPC Automation =
object has several
different object classes (e.g., OPCServer, OPCGroups,OPCGroup), each =
with their
own events.  DispatchWithEvents allows me to get an OPCServer object and =
see=20
OPCServer events. However, I can't see events associated with an =
OPCGroup.
The OPCGroup object is created using the "Add" method of OPCGroups.  =
OPCGroup
is not a registered class.=20

Using VB, I can see both OPCServer and OPCGroup events.  The following =
two programs
should both print each time a "Data Change" or "Server Shutdown" event =
occurs.  The
VB program does; the python program only sees the "Server Shutdown" =
event.  The
programs are virtually identical, except that VB makes you declare your =
object
variables up front, so I can use "WithEvents" on the OPCGroup.

How do I dispatch the OPCGroup class to see events in Python?  I'd =
appreciate any
advice.

Thanks in advance for your help.

Steve Goldman

******************************
Minimal VB Program
******************************

Dim WithEvents OPC As OPCServer
Dim Groups As OPCGroups
Dim WithEvents Group As OPCGroup
Dim Items As OPCItems
Dim Item As OPCItem

Private Sub Form_Load()
Set OPC =3D New OPCServer
OPC.Connect ("KEPWare.KEPServerEX.V4")
Set Groups =3D OPC.OPCGroups
Set Group =3D Groups.Add("Group 1")
Group.IsSubscribed =3D True
Set Items =3D Group.OPCItems
Set Item =3D Items.AddItem("Channel_0_User_Defined.Sine.Sine1", 1)
End Sub

Private Sub Group_DataChange(ByVal TransactionID As Long, ByVal NumItems =
As Long, ClientHandles() As Long, ItemValues() As Variant, Qualities() =
As Long, TimeStamps() As Date)
Debug.Print "Data Change at", Now()
End Sub

Private Sub OPC_ServerShutDown(ByVal Reason As String)
Debug.Print "Server Shutdown at", Now()
End Sub

#This program sees a Data Change event about once per second
#until the server is shut down.

**************************************
Minimal Python Program
**************************************
import win32com.client, pythoncom,time

class OPCEvents:
    def =
OnDataChange(self,TransactionID,NumItems,ClientHandles,ItemValues,Qualiti=
es,TimeStamps):
        print 'Data Change at', time.ctime()
   =20
    def OnServerShutDown(self, Reason):
        print 'Server Shutdown at',time.ctime()

OPC=3Dwin32com.client.DispatchWithEvents('OPC.Automation.1',OPCEvents)
OPC.Connect('KEPware.KEPServerEx.V4')
groups=3DOPC.OPCGroups
group=3Dgroups.Add('Group 1')
group.IsSubscribed=3D1
items=3Dgroup.OPCItems
item=3Ditems.AddItem('Channel_0_User_Defined.Sine.Sine1',1)
x=3D0
while x<2000000:
    pythoncom.PumpWaitingMessages()
    x=3Dx+1

#This program only shows the server shutting down.
#It can't see Data Change events.

------=_NextPart_000_000E_01C2E575.2882F2C0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4807.2300" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Hi,</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I'm having trouble seeing events from a =
python COM=20
client application.&nbsp; The COM <BR>server is an "OPC Automation 2.0" =
object,=20
which is used to communicate with industrial<BR>control =
products.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>After running the makepy script, I can =
see that the=20
OPC&nbsp;Automation object&nbsp;has several<BR>different object classes =
(e.g.,=20
OPCServer, OPCGroups,OPCGroup), each with their<BR>own events.&nbsp;=20
DispatchWithEvents allows me to get an OPCServer object and see =
<BR>OPCServer=20
events. However, I can't see events associated with an OPCGroup.<BR>The =
OPCGroup=20
object is created using the "Add" method of OPCGroups.&nbsp; =
OPCGroup<BR>is not=20
a registered class. </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Using VB, I can see both OPCServer and =
OPCGroup=20
events.&nbsp; The following two programs<BR>should both print each time =
a "Data=20
Change" or "Server Shutdown" event occurs.&nbsp; The<BR>VB program does; =
the=20
python program only sees the "Server Shutdown" event.&nbsp; =
The<BR>programs are=20
virtually identical, except that VB makes you declare your =
object<BR>variables=20
up front, so I can use "WithEvents" on the OPCGroup.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>How do I dispatch the OPCGroup =
class&nbsp;to=20
see&nbsp;events in Python?&nbsp; I'd appreciate =
any<BR>advice.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Thanks in advance for your =
help.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Steve Goldman</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>******************************<BR>Minimal VB=20
Program<BR>******************************</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Dim WithEvents OPC As OPCServer<BR>Dim =
Groups As=20
OPCGroups<BR>Dim WithEvents Group As OPCGroup<BR>Dim Items As =
OPCItems<BR>Dim=20
Item As OPCItem</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Private Sub Form_Load()<BR>Set OPC =3D =
New=20
OPCServer<BR>OPC.Connect ("KEPWare.KEPServerEX.V4")<BR>Set Groups =3D=20
OPC.OPCGroups<BR>Set Group =3D Groups.Add("Group =
1")<BR>Group.IsSubscribed =3D=20
True<BR>Set Items =3D Group.OPCItems<BR>Set Item =3D=20
Items.AddItem("Channel_0_User_Defined.Sine.Sine1", 1)<BR>End =
Sub</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Private Sub Group_DataChange(ByVal =
TransactionID As=20
Long, ByVal NumItems As Long, ClientHandles() As Long, ItemValues() As =
Variant,=20
Qualities() As Long, TimeStamps() As Date)<BR>Debug.Print "Data Change =
at",=20
Now()<BR>End Sub</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Private Sub OPC_ServerShutDown(ByVal =
Reason As=20
String)<BR>Debug.Print "Server Shutdown at", Now()<BR>End =
Sub</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>#This program sees a Data Change event =
about once=20
per second<BR>#until the server is shut down.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial =
size=3D2>**************************************<BR>Minimal=20
Python Program<BR>**************************************<BR>import=20
win32com.client, pythoncom,time</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>class OPCEvents:<BR>&nbsp;&nbsp;&nbsp; =
def=20
OnDataChange(self,TransactionID,NumItems,ClientHandles,ItemValues,Qualiti=
es,TimeStamps):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
print 'Data Change at', time.ctime()<BR>&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp; def OnServerShutDown(self,=20
Reason):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 'Server =
Shutdown=20
at',time.ctime()</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial=20
size=3D2>OPC=3Dwin32com.client.DispatchWithEvents('OPC.Automation.1',OPCE=
vents)<BR>OPC.Connect('KEPware.KEPServerEx.V4')<BR>groups=3DOPC.OPCGroups=
<BR>group=3Dgroups.Add('Group=20
1')<BR>group.IsSubscribed=3D1<BR>items=3Dgroup.OPCItems<BR>item=3Ditems.A=
ddItem('Channel_0_User_Defined.Sine.Sine1',1)<BR>x=3D0<BR>while=20
x&lt;2000000:<BR>&nbsp;&nbsp;&nbsp;=20
pythoncom.PumpWaitingMessages()<BR>&nbsp;&nbsp;&nbsp; =
x=3Dx+1</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>#This program only shows the server =
shutting=20
down.<BR>#It can't see Data Change events.</FONT></DIV></BODY></HTML>

------=_NextPart_000_000E_01C2E575.2882F2C0--