FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.36k stars 658 forks source link

How to create a variable as array #729

Open frheuinghhn opened 5 years ago

frheuinghhn commented 5 years ago

Hi guys, I am new to opc ua and I have a problem. I have created a type, which I would like to assign a few variables. My Object should be a radar and should have 3 variables:

setup our own namespace, not really necessary but should as spec

uri = "http://examples.freeopcua.github.io"
idx = server.register_namespace(uri)

# get Objects node, this is where we should put our nodes
objects = server.get_objects_node()
types = server.get_node(ua.ObjectIds.BaseObjectType)
object_type_to_derive_from = server.get_root_node().get_child(["0:Types", 
                                                               "0:ObjectTypes", 
                                                               "0:BaseObjectType"])

#create object type Messbereich for measurement range
messbereich_type = types.add_object_type(idx, "Messbereich_ObjectType")
messbereich_type.add_variable(0, "Max_Wert", ua.Variant(0.0, ua.VariantType.Float))
messbereich_type.add_variable(0, "Min_Wert", ua.Variant(0.0, ua.VariantType.Float))

#create new object type Radar and define its variables
radar_type = types.add_object(idx, "Radar_ObjectType")
radar_type.add_property(0, "Radarmodell", "TI")
radar_type.add_property(0, "Aktualisierungsrate", 0.1) #for refresh Time
radar_type.add_object(0, "Messbereich", messbereich_type.nodeid)
radar_type.add_property(0, "Radardaten", ua.VariantArrayType.Array)

#create object Radar 
radar = objects.add_object(idx, "Radar", radar_type.nodeid)
max_wert = objects.add_object(idx, "Max_Wert", messbereich_type.nodeid)
min_wert = objects.add_object(idx, "Min_Wert", messbereich_type.nodeid)

# starting!
server.start()
print("Server started at: {}".format(uri))

try:
    while True:
        time.sleep(1)
finally:
    # close connection, remove subcsriptions, etc
    server.stop()

Thanks in advance

zerox1212 commented 5 years ago

I haven't done this in a while, but I think you need to pass in an array. radar_type.add_property([0,1,2,3], "Radardaten", ua.VariantArrayType.Array)

It's like this in the server examples (not defining a type though): myarrayvar = myobj.add_variable(idx, "myarrayvar", [6.7, 7.9])

frheuinghhn commented 5 years ago

@zerox1212 Thank you for the quick response, it was really helfpful. Could you please send me the link to the code in the examples? Thank you

zerox1212 commented 5 years ago

https://github.com/FreeOpcUa/python-opcua/blob/master/examples/server-example.py#L125