How to write an array tag in a VARIANT structure on an OpenOPC server
Asked Answered
P

3

8

I'm trying to communicate with an OPC DA server and need to write in a tag which is in an array format. We can connect with a simulation server, read tags (int, real, array) and write tags (int, real, str). The problem comes when we need to write in an array tag. The developper of the OpenOPC library (Barry Barnreiter) recommand to use a VARIANT variable because OPC "expect to see a Windows VARIANT structure when writing complex objects such as arrays".

  • I did install Pywin32 (build 217) as suggested here.
  • I tried to send a simple integer instead of an array in a VARIANT structure.

Here's the code:

from win32com.client import VARIANT
import pythoncom
import OpenOPC
opc_local = OpenOPC.open_client()
opc_local.connect('Matrikon.OPC.Simulation','localhost')
values = VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
w = opc_local.write(('Bucket Brigade.ArrayOfReal8', values))
print(w)

Here's the error that we get when the line with opc_local.write gets executed:

AttributeError: 'module' object has no attribute 'VARIANT'

Here's the entire traceback:

runfile('C:/Users/nadmin/Downloads/sanstitre0.py', wdir='C:/Users/nadmin/Downloads')
Traceback (most recent call last):

  File "<ipython-input-5-6799f41ab928>", line 1, in <module>
    runfile('C:/Users/nadmin/Downloads/sanstitre0.py', wdir='C:/Users/nadmin/Downloads')

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 95, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)

  File "C:/Users/nadmin/Downloads/sanstitre0.py", line 14, in <module>
    w = opc_local.write(('Bucket Brigade.ArrayOfReal8', values))

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\Pyro\core.py", line 381, in __call__
    return self.__send(self.__name, args, kwargs)

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\Pyro\core.py", line 456, in _invokePYRO
    return self.adapter.remoteInvocation(name, Pyro.constants.RIF_VarargsAndKeywords, vargs, kargs)

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\Pyro\protocol.py", line 497, in remoteInvocation
    return self._remoteInvocation(method, flags, *args)

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\Pyro\protocol.py", line 572, in _remoteInvocation
    answer.raiseEx()

  File "C:\Users\nadmin\AppData\Local\Continuum\anaconda2\lib\site-packages\Pyro\errors.py", line 72, in raiseEx
    raise self.excObj

And here's the configuration of the computer:

  • Windows 10
  • Python 2.7
  • Pyro 3.16
  • Pywin32 Build 223
  • OpenOPC 1.3.1 win32-py27
Pentheas answered 25/11, 2021 at 23:1 Comment(2)
Show the entire traceback. The code you've given would not produce that error, and the OpenOPC source code never uses the word "VARIANT".Gaea
I can't see that you're doing anything wrong here. The error is happening as Pyro tries to convert the response it got back to Python objects. I have TWO hacky suggestions for you. First, it shouldn't need to be remote. I suggest removing the 'localhost' parameter to opc_local.connect. Second, try adding import win32com.client so it has access to the module that contains VARIANT.Gaea
P
3

You have to change your line opc_local = OpenOPC.open_client() for opc_local = OpenOPC.client(). This will make you connect directly to the OPC server, as opposed to using the OpenOPC Gateway Service.

The VARIANT structure is not included inside the Gateway Service exe. Note that the Gateway Service exe is it's own frozen Python distribution. Thus it only includes the Python modules inside it that it needs to run and nothing else. So by avoiding using the Gateway Service you should not have this problem since you'll be executing your code entirely using the Python distribution that you installed yourself on your PC.

Pentheas answered 7/12, 2021 at 16:42 Comment(0)
C
1

According to Python COM server throws 'module' object has no attribute 'VARIANT', the VARIANT class was introduced in Pywin32 build 217.

As you have included in your post that you have Pywin32 Build 223, this should not be a problem. But to be sure, from this list of available downloads: Home / pywin32 / Build 217, I would specifically select pywin32-217.win-amd64-py2.7.exe.

If that doesn't work, I would suggest checking the source of the configuration you listed; Do you only have one version of python installed? Perhaps you have multiple Python IDEs that could get mixed up? These are some common cases that can cause confusion in fixing bugs.

Carducci answered 7/12, 2021 at 13:17 Comment(0)
C
0

You need to upgrade the python to 3.9 and Pywin32 to Build 302. In addition, you need to install the OpenOPC-Python3x 1.3.1.

Contortionist answered 5/12, 2021 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.