OPC DA / OpenOPC timeout errror when reading value twice

51 Views Asked by At

I am trying to execute the following python code:

import OpenOPC
import pywintypes

pywintypes.datetime = pywintypes.TimeType
opc = OpenOPC.client()

opc.connect('server_name')

value = opc['tag_name']
print(value)

opc['tag_name'] = value + 1

value = opc['tag_name'] # Timeout for the following line
print(value)

opc.close()

The code does work without the commented line of code reading the tag for the second time. However, the current code gives the timeout error:

Exception                                 Traceback (most recent call last)
Cell In[40], line 15
     11 print(value)
     13 opc['tag_name'] = value + 1
---> 15 value = opc['tag_name']
     16 print(value)
     18 opc.close()

File c:\Users\username\.pyenv\pyenv-win\versions\3.9.0-win32\lib\site-packages\OpenOPC\opcda.py:262, in client.__getitem__(self, key)
    260 def __getitem__(self, key):
    261     if self.win32os:
--> 262         return self.clientIO.__getitem__(self._opc, self.clientTools, key)
    263     else:
    264         return None

File c:\Users\username\.pyenv\pyenv-win\versions\3.9.0-win32\lib\site-packages\OpenOPC\opcdaio.py:660, in ClientIO.__getitem__(self, _opc, clientTools, key)
    658 def __getitem__(self, _opc, clientTools, key):
    659     """Read single item (tag as dictionary key)"""
--> 660     value, quality, time_str = self.read(_opc, clientTools, key)
    661     return value

File c:\Users\username\.pyenv\pyenv-win\versions\3.9.0-win32\lib\site-packages\OpenOPC\opcdaio.py:387, in ClientIO.read(self, _opc, clientTools, tags, group, size, pause, source, update, timeout, sync, include_error, rebuild)
    384     results = self.iread(_opc, clientTools, tags, group, size, pause, source, update, timeout, sync, include_error, rebuild)
    386 if single:
--> 387     return list(results)[0]
    388 else:
    389     return list(results)

File c:\Users\username\.pyenv\pyenv-win\versions\3.9.0-win32\lib\site-packages\OpenOPC\opcdaio.py:307, in ClientIO.iread(self, _opc, clientTools, tags, group, size, pause, source, update, timeout, sync, include_error, rebuild)
    305 now = time.time() * 1000
    306 if now - start > timeout:
--> 307     raise TimeoutError('Callback: Timeout waiting for data')
    309 if self.callback_queue.empty():
    310     pythoncom.PumpWaitingMessages()

Exception: ('TimeoutError', 'Callback: Timeout waiting for data')

Why am I unable to read from the server multiple times?

Some notes:

  1. I am on windows, using 32 bit Python v 3.9.0
  2. pip install pywin32 --upgrade, gives Requirement already satsisfied
  3. I am able to write to server multiple times just fine
  4. I am using OpenOPC: https://pypi.org/project/OpenOPC-DA/
  5. I am able to read multiple time by using this very hacky solution:

Hacky solution:

import OpenOPC
import pywintypes

pywintypes.datetime = pywintypes.TimeType
opc = OpenOPC.client()

opc.connect('server_name')

value = opc['tag_name']
print(value)

opc['tag_name'] = value + 1

opc.close()
opc = OpenOPC.client()
opc.connect('server_name')
value = opc['tag_name']
print(value)

opc.close()

Any help is greatly appreciated.

1

There are 1 best solutions below

0
On

The solution was to use OpenOPC-WF instead of OpenOPC-DA.

To solve, run in terminal:

  1. pip uninstall OpenOPC-DA
  2. pip install OpenOPC-WF