Python OPC UA Client - Write variable using BrowseName

944 views Asked by At

I can't find the correct syntax for assigning a value to a variable using its BrowseName. I am testing with the 'flag1' boolean variable because it is easier to debug. But my goal is be able to write in all variables, including the arrays.

If I try to use the index number it works fine.

import pyOPCClient as opc

client = opc.opcConnect('192.168.5.10')

opc.write_value_bool(client, 'ns=4;s="opcData"."flag1"', True)

client.disconnect()

Here is my function to write boolean

##### Function to WRITE a Boolean into Bool Object Variable - Requires Object Name #####
def write_value_bool(client, node_id, value):
    client_node = client.get_node(node_id)  # get node
    client_node_value = value
    client_node_dv = ua.DataValue(ua.Variant(client_node_value, ua.VariantType.Boolean))
    client_node.set_value(client_node_dv)
    print("Value of : " + str(client_node) + ' : ' + str(client_node_value))

I am getting this error:

PS C:\Users\ALEMAC\Documents\Python Scripts> & C:/ProgramData/Anaconda3/python.exe "c:/Users/ALEMAC/Documents/Python Scripts/opctest.py"
Requested session timeout to be 3600000ms, got 30000ms instead
Traceback (most recent call last):
  File "c:\Users\ALEMAC\Documents\Python Scripts\opctest.py", line 5, in <module>
    opc.write_value_bool(client, 'ns=4;s="opcData"."flag1"', True)
  File "c:\Users\ALEMAC\Documents\Python Scripts\pyOPCClient.py", line 49, in write_value_bool
    client_node.set_value(client_node_dv)
  File "c:\Users\ALEMAC\Documents\Python Scripts\opcua\common\node.py", line 217, in set_value
    self.set_attribute(ua.AttributeIds.Value, datavalue)
  File "c:\Users\ALEMAC\Documents\Python Scripts\opcua\common\node.py", line 263, in set_attribute
    result[0].check()
  File "c:\Users\ALEMAC\Documents\Python Scripts\opcua\ua\uatypes.py", line 218, in check
    raise UaStatusCodeError(self.value)
opcua.ua.uaerrors._auto.BadNodeIdUnknown: "The node id refers to a node that does not exist in the server address space."(BadNodeIdUnknown)

TIA Portal V17 OPC UA - S7-1212

UA Expert Client

3

There are 3 answers

7
SFriedl On BEST ANSWER

I see you use the pyOPCClient package. I´m not sure if this is maintaned anymore (Last Update: 2014-01-09 see here).

You can switch to opcua-asyncio which can address nodes with the browse services like this:

myvar = await client.nodes.root.get_child(["0:Objects",..., "4:flag1"])

And here is the complete example

1
Camille G. On

You are using a mix of NodeId and BrowseName. Just Keep the node NodeId

opc.write_value_bool(client, 'ns=4;i=20', True)

To write other datapoint "args" you will also have to find their NodeId.

You can use the OPC UA Service Browse for that.

0
AlexMacabu On

Apparently the S7-1200 by default, when you create a tag in S7-1200 the NodeID is set to NUMERIC. Looks lik you can change it though, using another software called SiOME. https://support.industry.siemens.com/cs/document/109793221/how-do-you-change-the-node-id-identifier-type-of-the-nodes-in-the-s7-1200-opc-ua-server-from-numeric-to-string-?dti=0&dl=en&lc=nl-NL