Python COM Issues with OpenOPC when using a Threaded Timer

840 Views Asked by At

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

0

There are 0 best solutions below