making a opc client in Python and use opc server Events

Discussion in 'Python' started by getelectronic@gmail.com, Jun 4, 2007.

  1. Guest

    Hi all
    I have a sample code to implement opc client in Python. i use a
    file .py making by makepy with pythonwin for Com Interface.
    i can get all server in machine, connect to server opc, disconnect,
    add group, add item, read, write item in server opc.

    import win32com.client # librairie pour utiliser l'interface COM/DCOM
    from win32com.client import gencache
    gencache.EnsureModule('{DFB83232-A952-11D2-A46B-00C04F796375}', 0, 1,
    0)

    for svr in opcserver.GetOPCServers():
    print svr

    #connect to server OPC Demo Simulation from Matrikon
    opcserver.Connect('Matrikon.OPC.Simulation.1')

    # Instance object Groups
    groups=opcserver.OPCGroups
    #add group
    group=groups.Add('Group1')

    #instance onject Items
    items=group.OPCItems
    # add item in server opc
    tem=items.AddItem('File1.item1',1)

    #read item value
    item.Read(win32com.client.constants.OPCDevice)

    # write a new value
    item.Write(100)

    #read item value
    item.Read(win32com.client.constants.OPCDevice)
    #if no pb you have 100 :)

    #Disconnect
    #opcserver.Disconnect()

    BUT, and BUT, i want to use a event from opc server for uodating item
    value with this below class. And i don't konw how make it!!!!!!!!
    help me plz

    opcserver=win32com.client.Dispatch('OPC.Automation.1')
    and now i want to use events from opc server. in a class:
    class DIOPCGroupEvent:
    class DIOPCGroupsEvent:
    class DIOPCServerEvent:
     
    , Jun 4, 2007
    #1
    1. Advertising

  2. Guest

    On 4 juin, 11:32, wrote:
    > Hi all
    > I have a sample code to implement opc client in Python. i use a
    > file .py making by makepy with pythonwin for Com Interface.
    > i can get all server in machine, connect to server opc, disconnect,
    > add group, add item, read, write item in server opc.
    >
    > import win32com.client # librairie pour utiliser l'interface COM/DCOM
    > from win32com.client import gencache
    > gencache.EnsureModule('{DFB83232-A952-11D2-A46B-00C04F796375}', 0, 1,
    > 0)
    >
    > for svr in opcserver.GetOPCServers():
    > print svr
    >
    > #connect to server OPC Demo Simulation from Matrikon
    > opcserver.Connect('Matrikon.OPC.Simulation.1')
    >
    > # Instance object Groups
    > groups=opcserver.OPCGroups
    > #add group
    > group=groups.Add('Group1')
    >
    > #instance onject Items
    > items=group.OPCItems
    > # add item in server opc
    > tem=items.AddItem('File1.item1',1)
    >
    > #read item value
    > item.Read(win32com.client.constants.OPCDevice)
    >
    > # write a new value
    > item.Write(100)
    >
    > #read item value
    > item.Read(win32com.client.constants.OPCDevice)
    > #if no pb you have 100 :)
    >
    > #Disconnect
    > #opcserver.Disconnect()
    >
    > BUT, and BUT, i want to use a event from opc server for uodating item
    > value with this below class. And i don't konw how make it!!!!!!!!
    > help me plz
    >
    > opcserver=win32com.client.Dispatch('OPC.Automation.1')
    > and now i want to use events from opc server. in a class:
    > class DIOPCGroupEvent:
    > class DIOPCGroupsEvent:
    > class DIOPCServerEvent:


    Try this:

    # Event Handlers
    class ServerEvent:
    def __init__(self):
    print 'Init ServerEvent'

    def OnServerShutDown(self, Reason):
    print 'OnServerShutDown', Reason

    class GroupEvent:
    def __init__(self):
    print 'Init GroupEvent'

    def OnAsyncCancelComplete(self, CancelID):
    print 'OnAsyncCancelComplete', CancelID

    def OnDataChange(self, TransactionID, NumItems, ClientHandles,
    ItemValues, Qualities, TimeStamps):
    print 'OnDataChange', zip(ClientHandles, ItemValues, Qualities)

    def OnAsyncReadComplete(self, TransactionID, NumItems, ClientHandles,
    ItemValues, Qualities,
    TimeStamps, Errors):
    print 'OnAsyncReadComplete', zip(ClientHandles, ItemValues,
    Qualities)

    def OnAsyncWriteComplete(self, TransactionID, NumItems,
    ClientHandles, Errors):
    print 'OnAsyncWriteComplete', zip(ClientHandles, Errors)

    class GroupsEvent:
    def __init__(self):
    print 'Init GroupsEvent'

    def OnGlobalDataChange(self, TransactionID, GroupHandle, NumItems,
    ClientHandles, ItemValues,
    Qualities, TimeStamps):
    print 'OnGlobalDataChange', zip(ClientHandles, ItemValues,
    Qualities)

    opc = DispatchWithEvents('Matrikon.OPC.Automation.1', ServerEvent)

    groups = DispatchWithEvents(opc.OPCGroups, GroupsEvent)
    groups.DefaultGroupIsActive = True
    groups.DefaultGroupUpdateRate = 2000

    group1 = DispatchWithEvents(groups.Add('G1'), GroupEvent)
    group2 = DispatchWithEvents(groups.Add('G2'), GroupEvent)
    #etc ...

    It works for the GroupsEvents but I don't get the GroupEvent for each
    group, I may still do someting wrong..
     
    , Jun 28, 2007
    #2
    1. Advertising

  3. Guest

    On 28 juin, 16:55, wrote:
    > On 4 juin, 11:32, wrote:
    >
    >
    >
    >
    >
    > > Hi all
    > > I have a sample code to implement opc client in Python. i use a
    > > file .py making by makepy with pythonwin for Com Interface.
    > > i can get all server in machine, connect to server opc, disconnect,
    > > add group, add item, read, write item in server opc.

    >
    > > import win32com.client # librairie pour utiliser l'interface COM/DCOM
    > > from win32com.client import gencache
    > > gencache.EnsureModule('{DFB83232-A952-11D2-A46B-00C04F796375}', 0, 1,
    > > 0)

    >
    > > for svr in opcserver.GetOPCServers():
    > > print svr

    >
    > > #connect to server OPC Demo Simulation from Matrikon
    > > opcserver.Connect('Matrikon.OPC.Simulation.1')

    >
    > > # Instance object Groups
    > > groups=opcserver.OPCGroups
    > > #add group
    > > group=groups.Add('Group1')

    >
    > > #instance onject Items
    > > items=group.OPCItems
    > > # add item in server opc
    > > tem=items.AddItem('File1.item1',1)

    >
    > > #read item value
    > > item.Read(win32com.client.constants.OPCDevice)

    >
    > > # write a new value
    > > item.Write(100)

    >
    > > #read item value
    > > item.Read(win32com.client.constants.OPCDevice)
    > > #if no pb you have 100 :)

    >
    > > #Disconnect
    > > #opcserver.Disconnect()

    >
    > > BUT, and BUT, i want to use a event from opc server for uodating item
    > > value with this below class. And i don't konw how make it!!!!!!!!
    > > help me plz

    >
    > > opcserver=win32com.client.Dispatch('OPC.Automation.1')
    > > and now i want to use events from opc server. in a class:
    > > class DIOPCGroupEvent:
    > > class DIOPCGroupsEvent:
    > > class DIOPCServerEvent:

    >
    > Try this:
    >
    > # Event Handlers
    > class ServerEvent:
    > def __init__(self):
    > print 'Init ServerEvent'
    >
    > def OnServerShutDown(self, Reason):
    > print 'OnServerShutDown', Reason
    >
    > class GroupEvent:
    > def __init__(self):
    > print 'Init GroupEvent'
    >
    > def OnAsyncCancelComplete(self, CancelID):
    > print 'OnAsyncCancelComplete', CancelID
    >
    > def OnDataChange(self, TransactionID, NumItems, ClientHandles,
    > ItemValues, Qualities, TimeStamps):
    > print 'OnDataChange', zip(ClientHandles, ItemValues, Qualities)
    >
    > def OnAsyncReadComplete(self, TransactionID, NumItems, ClientHandles,
    > ItemValues, Qualities,
    > TimeStamps, Errors):
    > print 'OnAsyncReadComplete', zip(ClientHandles, ItemValues,
    > Qualities)
    >
    > def OnAsyncWriteComplete(self, TransactionID, NumItems,
    > ClientHandles, Errors):
    > print 'OnAsyncWriteComplete', zip(ClientHandles, Errors)
    >
    > class GroupsEvent:
    > def __init__(self):
    > print 'Init GroupsEvent'
    >
    > def OnGlobalDataChange(self, TransactionID, GroupHandle, NumItems,
    > ClientHandles, ItemValues,
    > Qualities, TimeStamps):
    > print 'OnGlobalDataChange', zip(ClientHandles, ItemValues,
    > Qualities)
    >
    > opc = DispatchWithEvents('Matrikon.OPC.Automation.1', ServerEvent)
    >
    > groups = DispatchWithEvents(opc.OPCGroups, GroupsEvent)
    > groups.DefaultGroupIsActive = True
    > groups.DefaultGroupUpdateRate = 2000
    >
    > group1 = DispatchWithEvents(groups.Add('G1'), GroupEvent)
    > group2 = DispatchWithEvents(groups.Add('G2'), GroupEvent)
    > #etc ...
    >
    > It works for the GroupsEvents but I don't get the GroupEvent for each
    > group, I may still do someting wrong..- Masquer le texte des messages précédents -
    >
    > - Afficher le texte des messages précédents -


    In order to get the GroupEvent, you have to keep a reference to the
    group you add
    eg:
    g1 = groups.Add('G1')
    group1 = DispatchWithEvents(g1, GroupEvent)
    etc...
     
    , Aug 7, 2007
    #3
  4. getgroup Guest

    On 7 août, 07:17, wrote:
    > On 28 juin, 16:55, wrote:
    >
    >
    >
    >
    >
    > > On 4 juin, 11:32, wrote:

    >
    > > > Hi all
    > > > I have a sample code to implementopcclientinPython. i use a
    > > > file .py making by makepy with pythonwin for Com Interface.
    > > > i can get all server in machine, connect to serveropc, disconnect,
    > > > add group, add item, read, write item in serveropc.

    >
    > > > import win32com.client # librairie pour utiliser l'interface COM/DCOM
    > > > from win32com.clientimport gencache
    > > > gencache.EnsureModule('{DFB83232-A952-11D2-A46B-00C04F796375}', 0, 1,
    > > > 0)

    >
    > > > for svr in opcserver.GetOPCServers():
    > > > print svr

    >
    > > > #connect to serverOPCDemo Simulation from Matrikon
    > > > opcserver.Connect('Matrikon.OPC.Simulation.1')

    >
    > > > # Instance object Groups
    > > > groups=opcserver.OPCGroups
    > > > #add group
    > > > group=groups.Add('Group1')

    >
    > > > #instance onject Items
    > > > items=group.OPCItems
    > > > # add item in serveropc
    > > > tem=items.AddItem('File1.item1',1)

    >
    > > > #read item value
    > > > item.Read(win32com.client.constants.OPCDevice)

    >
    > > > # write a new value
    > > > item.Write(100)

    >
    > > > #read item value
    > > > item.Read(win32com.client.constants.OPCDevice)
    > > > #if no pb you have 100 :)

    >
    > > > #Disconnect
    > > > #opcserver.Disconnect()

    >
    > > > BUT, and BUT, i want to use a event fromopcserver for uodating item
    > > > value with this below class. And i don't konw how make it!!!!!!!!
    > > > help me plz

    >
    > > > opcserver=win32com.client.Dispatch('OPC.Automation.1')
    > > > and now i want to use events fromopcserver. in a class:
    > > > class DIOPCGroupEvent:
    > > > class DIOPCGroupsEvent:
    > > > class DIOPCServerEvent:

    >
    > > Try this:

    >
    > > # Event Handlers
    > > class ServerEvent:
    > > def __init__(self):
    > > print 'Init ServerEvent'

    >
    > > def OnServerShutDown(self, Reason):
    > > print 'OnServerShutDown', Reason

    >
    > > class GroupEvent:
    > > def __init__(self):
    > > print 'Init GroupEvent'

    >
    > > def OnAsyncCancelComplete(self, CancelID):
    > > print 'OnAsyncCancelComplete', CancelID

    >
    > > def OnDataChange(self, TransactionID, NumItems, ClientHandles,
    > > ItemValues, Qualities, TimeStamps):
    > > print 'OnDataChange', zip(ClientHandles, ItemValues, Qualities)

    >
    > > def OnAsyncReadComplete(self, TransactionID, NumItems, ClientHandles,
    > > ItemValues, Qualities,
    > > TimeStamps, Errors):
    > > print 'OnAsyncReadComplete', zip(ClientHandles, ItemValues,
    > > Qualities)

    >
    > > def OnAsyncWriteComplete(self, TransactionID, NumItems,
    > > ClientHandles, Errors):
    > > print 'OnAsyncWriteComplete', zip(ClientHandles, Errors)

    >
    > > class GroupsEvent:
    > > def __init__(self):
    > > print 'Init GroupsEvent'

    >
    > > def OnGlobalDataChange(self, TransactionID, GroupHandle, NumItems,
    > > ClientHandles, ItemValues,
    > > Qualities, TimeStamps):
    > > print 'OnGlobalDataChange', zip(ClientHandles, ItemValues,
    > > Qualities)

    >
    > >opc= DispatchWithEvents('Matrikon.OPC.Automation.1', ServerEvent)

    >
    > > groups = DispatchWithEvents(opc.OPCGroups, GroupsEvent)
    > > groups.DefaultGroupIsActive = True
    > > groups.DefaultGroupUpdateRate = 2000

    >
    > > group1 = DispatchWithEvents(groups.Add('G1'), GroupEvent)
    > > group2 = DispatchWithEvents(groups.Add('G2'), GroupEvent)
    > > #etc ...

    >
    > > It works for the GroupsEvents but I don't get the GroupEvent for each
    > > group, I may still do someting wrong..- Masquer le texte des messages précédents -

    >
    > > - Afficher le texte des messages précédents -

    >
    > In order to get the GroupEvent, you have to keep a reference to the
    > group you add
    > eg:
    > g1 = groups.Add('G1')
    > group1 = DispatchWithEvents(g1, GroupEvent)
    > etc...- Masquer le texte des messages précédents -
    >
    > - Afficher le texte des messages précédents -


    Hi Yes i try it.
    But when a client get a datachange the script is bloked
    you must kill a main thread, this solution is not good.

    i have experimented this programme:

    http://groups.google.fr/group/cn.bb...ent opc python&rnum=25&hl=fr#1ecb94a0aca9e8d6

    it run when a item in Group is changed But you can't do anything in a
    loop WHEN.
     
    getgroup, Aug 8, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. F. GEIGER

    OPC Server in Python

    F. GEIGER, May 17, 2004, in forum: Python
    Replies:
    4
    Views:
    2,849
    F. GEIGER
    May 18, 2004
  2. pierlau
    Replies:
    6
    Views:
    3,996
  3. Walter S. Leipold

    RE: Writing an OPC client with Python ?

    Walter S. Leipold, Mar 3, 2006, in forum: Python
    Replies:
    0
    Views:
    444
    Walter S. Leipold
    Mar 3, 2006
  4. baner

    Python OPC Client for RSLINX

    baner, Oct 2, 2011, in forum: Python
    Replies:
    3
    Views:
    1,495
    acardoso
    Oct 27, 2011
Loading...

Share This Page