Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OPC UA Client - Write variable using BrowseName

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

like image 582
AlexMacabu Avatar asked Jan 27 '26 04:01

AlexMacabu


1 Answers

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

like image 65
SFriedl Avatar answered Jan 28 '26 16:01

SFriedl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!