Python Script with asyncua

i want to control the B&R PLC with an asyncua python script.

Everthing works. Readout of values, subscription a.s.o.

But i have problem to wite values

Here my code

    # Get the node by its NodeId
    path = "ns=6;s=::AsGlobalPV:ShortetStat2ndStack"

    node = self.__client.get_node(path)
    value = await node.get_value()
    if value:
        await node.write_value(False)      
    else:    
        await node.write_value(True)    

or

    dv = ua.DataValue(not val, ua.VariantType.Boolean)
    # dv.ServerTimestamp = datetime.now()
    # timestamp = datetime.now()  
     await node.set_value(dv)

I get always “BadWriteNotSupported” for the PLC Server
writing with UAExpert is possible and i have ua.AccessLevel.CurrentWrite for this value

Some ideas?

Is Write access allowed for the current logged in user?

See settings in OPC UA Default View file.

I’ve done something with asyncua in python.
It was for write structure to the PLC but it should work for bool too.

from asyncua import Client, ua, Node
from asyncua.common.subscription import DataChangeNotif, Subscription
import asyncio
import time


class PLCClient(Client):
  async def writeNode(self, node:Node, nodeVal):
          attr = ua.WriteValue()
          attr.NodeId = node.nodeid
          attr.AttributeId = ua.AttributeIds.Value
          attr.Value = ua.DataValue(nodeVal)
          params = ua.WriteParameters()
          params.NodesToWrite = [attr]
          return await node.write_params(params)

Maybe it could help.

It works
thanks a lot

1 Like

Glad it works in your app :slight_smile: