我正在尝试管理我的 opcua 服务器中标签的质量状态,但我找不到正确设置 StatusCode 的方法。在这里找到我的代码片段。感谢名为 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()
在这里, 我扩展了您的解决方案示例代码:
# 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(
)