convertersystems / opc-ua-client

Visualize and control your enterprise using OPC Unified Architecture (OPC UA) and Visual Studio.
MIT License
405 stars 119 forks source link

Read node value #251

Closed techvoipit closed 1 year ago

techvoipit commented 1 year ago

I would like to read a value variable with this code but I can't isolate it: var readRequest = new ReadRequest { // set the NodesToRead to an array of ReadValueIds. NodesToRead = new[] { // construct a ReadValueId from a NodeId and AttributeId. new ReadValueId { // you can parse the nodeId from a string. NodeId = NodeId.Parse("ns=2;s=Target01.PLC.Memory_Bits.AGITATORE_FERMO"), //NodeId = NodeId.Parse(VariableIds.Server_ServerStatus), // variable class nodes have a Value attribute. AttributeId = AttributeIds.Value } } }; // send the ReadRequest to the server. var readResult = await channel.ReadAsync(readRequest); foreach (var result in readResult.Results) { Console.WriteLine(result); var val = result.GetValueOrDefault(); Console.WriteLine("Value: " + val.Value);

                        // Timestamp del valore letto
                        Console.WriteLine("Timestamp: " + val.SourceTimestamp);

                        // Stato del valore letto
                        Console.WriteLine("Status: " + val.StatusCode);

                }

return True; status: 0x00000000; ts: 28/06/2023 16:43:19

How I can get only true ? attached the node structure ua

dbonfilt commented 1 year ago

Is this not working for you?

var val = result.GetValueOrDefault<bool>();

In fact, in 3.1.1 at least, the GetValueOrDefault requires a type parameter (?).

techvoipit commented 1 year ago

Thank you very much is working!! Can you share me where I can find some documentation about which type I can use for GetValueOrDefault

ismdiego commented 1 year ago

You are welcome, glad to help.

The GetValueOrDefault accepts any NET type. I mean, it will try to cast the read value to the NET type you expect.

I think you are really asking for OPC-UA types, that you can see here: OPC UA Built-in Data Types

Also, you can find a typical mapping from these data types to C# .NET counterparts here (warning: this is NOT official documentation from this library, take this only as an example): Quick OPC Data Types in OPC-UA