2

I'm trying to manage the Quality Status of the tags in my opcua server but I could not find the way to set the StatusCode properly. Find here a snippet of my code. I'm reading the server tag thanks to a standard and free client named Integration Objects.

import sys

sys.path.insert(0, "..")

import time

from opcua import ua, Server

server = Server()

server.set_endpoint("opc.tcp://0.0.0.0:4841/freeopcua/server/")

uri = "http://examples.freeopcua.github.io"

idx = server.register_namespace(uri)

objects = server.get_objects_node()

myobj = objects.add_object(idx, "MyObject")

myvar = myobj.add_variable(idx, "MyVariable", 6.7)

myvar.set_writable()    # Set MyVariable to be writable by clients



server.start()



try:

    count = 0

    while True:

        time.sleep(1)

        count += 0.1

        myvar.set_data_value(count)

        # here I'd like to set programmatically the StatusCode of myvar variable

        print(myvar.get_value())

        print("\n")

except Exception as e:

    print('\nOPC failed:', str(e))

    input("...fine errore...")

finally:

    server.stop()
eglease
  • 2,445
  • 11
  • 18
  • 28
Marcello
  • 51
  • 4

1 Answers1

0

Here, I expanded your example code for solution:

# import sys
# sys.path.insert(0, "..")

import time

from asyncua.sync import Server, SyncNode, ua

server = Server()
server.set_endpoint("opc.tcp://localhost:4841/freeopcua/server/")
uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)
objects:SyncNode = server.nodes.objects
myobj:SyncNode = objects.add_object(idx, "MyObject")
myvar:SyncNode = myobj.add_variable(idx, "MyVariable", 6.7)
myvar.set_writable()    # Set MyVariable to be writable by clients
server.start()

try:
    count = 0
    while True:
        time.sleep(1)
        count += 0.1

        # here I'd like to set programmatically the StatusCode of myvar variable
        res = ua.DataValue(Value=count, StatusCode_= ua.StatusCode(ua.StatusCodes.GoodFaultStateActive))
        if count > 1:
            res = ua.DataValue(Value=count, StatusCode_= ua.StatusCode(ua.StatusCodes.Uncertain))
        myvar.write_value(res)

        data:ua.DataValue = myvar.read_data_value()
        print(data.Value.Value, data.StatusCode.value)
except Exception as e:
    print('\nOPC failed:', str(e))
    input("...fine errore...")
finally:
    server.stop(

)
mggluscevic
  • 431
  • 5
  • 8