1

I am having an issue when trying to read OPC tags in a separate thread. Currently using windows and connecting to the Matrikon OPC Simuator using the DCOM mode. The code below works fine

opc = OpenOPC.client()
opc.connect('Matrikon.OPC.Simulation','localhost')
print(opc.read(['Random.Int1','Random.Int2','Random.Int4']))

However, when I attempt to read tags in a loop using a threading.Timer

def start():

    def readTags():
        # Start new threaded timer
        start()          

        # Read OPC tags. 
        print(opc.read(['Random.Int1','Random.Int2','Random.Int4']))

    # Create new threaded timer with the function to call after 1 second
    r = threading.Timer(1, readTags)
    r.start()

opc = OpenOPC.client()
opc.connect('Matrikon.OPC.Simulation','localhost')
start()

I get the following error

OPCError: AddGroup: CoInitialize has not been called.

I have a feeling this has something to do with sharing COM objects between different threads (Using win32com with multithreading) But cannot get it to work properly. Any help would be greatly appreciated, thanks

Community
  • 1
  • 1
rodp82
  • 275
  • 3
  • 12
  • Don't need `pythoncom` if all object is pyobject. Use shared class. – dsgdfg Feb 15 '16 at 07:22
  • Thanks @dsgdfg, please could you elaborate on that – rodp82 Feb 15 '16 at 22:23
  • http://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds check all errors and using the class. Otherwise the application will be garbage. @rodp82 – dsgdfg Feb 16 '16 at 14:37
  • Thanks for your help @dsgdfg, but this does not solve my problem. This is just another way (although, more elegant) of going about using timer threads. I am still getting the same error. My issue comes from the OPC COM object not being shared across threads – rodp82 Feb 16 '16 at 23:59

0 Answers0