Writing custom structures/extension objects to a server
Posted: 21 Mar 2023, 13:53
I am currently using the following code to write a generic structure value to my server and the data does not seem to get through to the server. The server appears to receive the structure definition with the data type of the fields but the values are not in the generic structure value when the variant is converted on the server. I have debugged the variant (nodesToWrite[0].Value.Value) and converted it to a generic structure value and the data is there before writing but has vanished on the server side. I receive the error 0x803C0000 (The value was out of range) in my client.
The values do come through when written with UaExpert when I write the same data to the server.
I use the following code to create a structure definition and a set the fields of a generic structure value.
The values do come through when written with UaExpert when I write the same data to the server.
Code: Select all
UaStatus SampleClient::writeValue(OpcUa_NodeId NodeId, UaGenericStructureValue gsValue)
{
UaDiagnosticInfos diagnosticInfos;
UaWriteValues nodesToWrite;
UaStatus result;
UaStatusCodeArray results;
ServiceSettings serviceSettings;
UaExtensionObject value;
UaVariant variant;
gsValue.toExtensionObject(value);
variant.setExtensionObject(value, OpcUa_False);
nodesToWrite.create(1);
OpcUa_NodeId_CopyTo(&NodeId, &nodesToWrite[0].NodeId);
nodesToWrite[0].AttributeId = OpcUa_Attributes_Value;
variant.copyTo(&nodesToWrite[0].Value.Value);
result = m_pSession->write(serviceSettings, nodesToWrite, results, diagnosticInfos);
if (result.isGood())
{
// Write service succeded - check status of write value
result = UaStatus(results[0]);
}
return result;
}
Code: Select all
status = findPLCGlobal(pMyClient, "TestStruct1", nodeId);
structDefinition.setName("TestStruct1");
structDefinition.setDataTypeId(300000); // SSTRUCT1 data type (Boolean, Int16, Float)
field.setName("FFIELD1");
field.setDataTypeId(OpcUaType_Boolean);
structDefinition.addChild(field);
field.setName("FFIELD2");
field.setDataTypeId(OpcUaType_Int16);
structDefinition.addChild(field);
field.setName("FFIELD3");
field.setDataTypeId(OpcUaType_Float);
structDefinition.addChild(field);
gsValue = UaGenericStructureValue(structDefinition);
value.setBoolean((OpcUa_Boolean)true);
status = gsValue.setField(UaString("FFIELD1"), value);
value.setInt16((OpcUa_Int16)401);
status = gsValue.setField(UaString("FFIELD2"), value);
value.setFloat((OpcUa_Float)52.9f);
status = gsValue.setField(UaString("FFIELD3"), value);
status = pMyClient->writeValue(nodeId.NodeId, gsValue);