gopcua / opcua

Native Go OPC-UA library
MIT License
829 stars 253 forks source link

[Question] Need some help registering custom UDTs #734

Closed JessevGool closed 4 weeks ago

JessevGool commented 1 month ago

So I'm currently testing out the library and I'm having some trouble registering UDTs. I have succesfully registered a UDT which is on the public OPC Milo server: uaexpert_GU8lUksKlP Using the following code

type customStruct struct {
    Foo string
    Bar uint32
    Baz bool
}
func init() {
    ua.RegisterExtensionObject(ua.NewStringNodeID(2, "ComplexTypes/CustomStructTypeVariable"), new([]byte))
    ua.RegisterExtensionObject(ua.NewStringNodeID(2, "DataType.CustomStructType.BinaryEncoding"), new(customStruct))
}

and read it as follows

var nodeID = "ns=2;s=ComplexTypes/CustomStructTypeVariable"

    id, err := ua.ParseNodeID(nodeID)
    if err != nil {
        log.Println(err.Error())
    }

    log.Println(id)

    req := &ua.ReadRequest{
        MaxAge:             2000,
        NodesToRead:        []*ua.ReadValueID{{NodeID: id}},
        TimestampsToReturn: ua.TimestampsToReturnBoth,
    }

    resp, err := client.connection.Read(client.Context, req)
    if err != nil {
        log.Println(err.Error())
    }

    log.Println(len(resp.Results))
    log.Println(resp.Results[0].Value)
    log.Println(resp.Results[0].Value.Value().(customStruct))

This all worked fine and I got the values that are in the foo, bar and baz fields. Now I'm trying to do this for one of my servers, which has the following struct: afbeelding For which I'm using the following code to register:

type UDT_SemVer struct {
    major uint8
    minor uint8
    patch uint8
}

func init() {
    ua.RegisterExtensionObject(ua.NewStringNodeID(3, "\"MasterController\".\"Modules\".\"Compressor\".\"ID\".\"ElectricalVersion\""), new([]byte))
    ua.RegisterExtensionObject(ua.NewStringNodeID(3, "DT_\"UDT_SemVer\".BinaryEncoding"), new(UDT_SemVer))
}

The reading is almost identical to the previous one, the only differences being the nodeId and the type conversion at the end. The response is as follows: afbeelding Any idea why this isn't working?