OPCUA DataTypes #9
-
|
Hi @GianfriAur , I have a question, this time about the dataTypes. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Not sure if this is the most efficient way, but it seems to work: // If it's a variable, we can also read its value's data type (if available)
$valueDataType = $client->read($node->reference->nodeId)->getVariant()->type->name ?? null; |
Beta Was this translation helpful? Give feedback.
-
|
Hi @H4rw3y5ag3 great question, and the confusion is totally understandable: two unrelated concepts share the word "type". What you foundThe four constants public const TYPE_NUMERIC = 'numeric';
public const TYPE_STRING = 'string';
public const TYPE_GUID = 'guid';
public const TYPE_OPAQUE = 'opaque';live in The 25 built-in OPC UA data typesWhat you're actually looking for lives in case Boolean = 1;
case SByte = 2; case Byte = 3;
case Int16 = 4; case UInt16 = 5;
case Int32 = 6; case UInt32 = 7;
case Int64 = 8; case UInt64 = 9;
case Float = 10; case Double = 11;
case String = 12; case DateTime = 13;
case Guid = 14; case ByteString = 15;
case XmlElement = 16;
case NodeId = 17; case ExpandedNodeId = 18;
case StatusCode = 19;
case QualifiedName = 20; case LocalizedText = 21;
case ExtensionObject = 22;
case DataValue = 23; case Variant = 24;
case DiagnosticInfo = 25;Your answer is exactly right$type = $client->read($node->reference->nodeId)->getVariant()->type;
// returns a BuiltinType ? e.g. BuiltinType::Int32, BuiltinType::UInt16, That gives you the actual data type the server populated the variant with. The 4 NodeId selectors and the 25 BuiltinType cases never overlap, they answer two different questions:
Bonus read the DataType attribute without fetching the valueIf you only need the type metadata (e.g. for variables you can't read, or to avoid the round-trip on a hot path), read the use PhpOpcua\Client\Types\AttributeId;
$dataTypeNodeId = $client->read($nodeId, AttributeId::DataType)->getValue();
// ? a NodeId of the DataType definition (e.g. i=6 = Int32, i=4 = Int16, ?)The numeric IDs of the standard types match the A possible API improvement worth considering: rename in addition I will prepare in the v4.4.0 release also Does this seem like a possible improvement to the API? |
Beta Was this translation helpful? Give feedback.
-
|
Hi @H4rw3y5ag3 I finally released 4.4.0, if you want to upgrade just do the backwards compatibility is 100%, $dv = $client->read('ns=2;s=Devices/PLC/Speed');
$dv->getType(); // BuiltinType::Double
//or
$dv->type; // BuiltinType::DoubleIf you have any other questions, just ask |
Beta Was this translation helpful? Give feedback.
Hi @H4rw3y5ag3
I finally released 4.4.0, if you want to upgrade just do the backwards compatibility is 100%,
as anticipated the new API to easily access the type is
If you have any other questions, just ask