nauful / LibUA

Open-source OPC UA client and server library
Apache License 2.0
262 stars 94 forks source link

Read Datatype return NodeId intead of Datatype #122

Closed name024 closed 1 year ago

name024 commented 1 year ago

First of all thanks for this great lib! I have try to read the Datatype from a OPC UA-Variable using the Nuget Package and the OPC UA Client. My expectation is a datatype string like "Bool", "Int32", "String", etc. but instead of a datatype string i´ve get the nodeId. I could not find my mistake. Can someone help?

` public (bool Sucess, string Datatype) ReadDatatype(string nodeId) { if (client is null) return (false, "Not Connected");

    try
    {
        string Error = GetIdFromString(nodeId, out NodeId readNodeId);
        if (Error != "")
            return (false, Error);

        var readRes = client.Read(new ReadValueId[]
            {
                new ReadValueId(readNodeId, NodeAttribute.DataType, null, new QualifiedName(0, null))
            }, out DataValue[] dvs);

        if (readRes != StatusCode.Good)
            return (false, readRes.ToString());

        if (dvs[0].Value is null && dvs[0].ServerTimestamp is null && dvs[0].SourceTimestamp is null && dvs[0].StatusCode is null)
            return (false, "Read Error - exist NodeId?");

        return (true, dvs[0].Value.ToString() ?? "");
    }
    catch (Exception ex)
    {
        return (false, $"Error. {ex}");
    }
}`

image

name024 commented 1 year ago

Found my bug. If you read the Datatype you get the OPC UA "NodeId" which hold the Datatype information - not directly the Datatype.