making a opc client in Python and use opc server Events

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • getelectronic@gmail.com

    #1

    making a opc client in Python and use opc server Events

    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.Ensure Module('{DFB832 32-A952-11D2-A46B-00C04F796375}', 0, 1,
    0)

    for svr in opcserver.GetOP CServers():
    print svr

    #connect to server OPC Demo Simulation from Matrikon
    opcserver.Conne ct('Matrikon.OP C.Simulation.1' )

    # Instance object Groups
    groups=opcserve r.OPCGroups
    #add group
    group=groups.Ad d('Group1')

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

    #read item value
    item.Read(win32 com.client.cons tants.OPCDevice )

    # write a new value
    item.Write(100)

    #read item value
    item.Read(win32 com.client.cons tants.OPCDevice )
    #if no pb you have 100 :)

    #Disconnect
    #opcserver.Disc onnect()

    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=win32 com.client.Disp atch('OPC.Autom ation.1')
    and now i want to use events from opc server. in a class:
    class DIOPCGroupEvent :
    class DIOPCGroupsEven t:
    class DIOPCServerEven t:

  • jp.eugster@googlemail.com

    #2
    Re: making a opc client in Python and use opc server Events

    On 4 juin, 11:32, getelectro...@g mail.com 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.Ensure Module('{DFB832 32-A952-11D2-A46B-00C04F796375}', 0, 1,
    0)
    >
    for svr in opcserver.GetOP CServers():
    print svr
    >
    #connect to server OPC Demo Simulation from Matrikon
    opcserver.Conne ct('Matrikon.OP C.Simulation.1' )
    >
    # Instance object Groups
    groups=opcserve r.OPCGroups
    #add group
    group=groups.Ad d('Group1')
    >
    #instance onject Items
    items=group.OPC Items
    # add item in server opc
    tem=items.AddIt em('File1.item1 ',1)
    >
    #read item value
    item.Read(win32 com.client.cons tants.OPCDevice )
    >
    # write a new value
    item.Write(100)
    >
    #read item value
    item.Read(win32 com.client.cons tants.OPCDevice )
    #if no pb you have 100 :)
    >
    #Disconnect
    #opcserver.Disc onnect()
    >
    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=win32 com.client.Disp atch('OPC.Autom ation.1')
    and now i want to use events from opc server. in a class:
    class DIOPCGroupEvent :
    class DIOPCGroupsEven t:
    class DIOPCServerEven t:
    Try this:

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

    def OnServerShutDow n(self, Reason):
    print 'OnServerShutDo wn', Reason

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

    def OnAsyncCancelCo mplete(self, CancelID):
    print 'OnAsyncCancelC omplete', CancelID

    def OnDataChange(se lf, TransactionID, NumItems, ClientHandles,
    ItemValues, Qualities, TimeStamps):
    print 'OnDataChange', zip(ClientHandl es, ItemValues, Qualities)

    def OnAsyncReadComp lete(self, TransactionID, NumItems, ClientHandles,
    ItemValues, Qualities,
    TimeStamps, Errors):
    print 'OnAsyncReadCom plete', zip(ClientHandl es, ItemValues,
    Qualities)

    def OnAsyncWriteCom plete(self, TransactionID, NumItems,
    ClientHandles, Errors):
    print 'OnAsyncWriteCo mplete', zip(ClientHandl es, Errors)

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

    def OnGlobalDataCha nge(self, TransactionID, GroupHandle, NumItems,
    ClientHandles, ItemValues,
    Qualities, TimeStamps):
    print 'OnGlobalDataCh ange', zip(ClientHandl es, ItemValues,
    Qualities)

    opc = DispatchWithEve nts('Matrikon.O PC.Automation.1 ', ServerEvent)

    groups = DispatchWithEve nts(opc.OPCGrou ps, GroupsEvent)
    groups.DefaultG roupIsActive = True
    groups.DefaultG roupUpdateRate = 2000

    group1 = DispatchWithEve nts(groups.Add( 'G1'), GroupEvent)
    group2 = DispatchWithEve nts(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..

    Comment

    Working...