Page 1 of 1

Writing custom structures/extension objects to a server

Posted: 21 Mar 2023, 13:53
by saw
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.

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;
}
I use the following code to create a structure definition and a set the fields of a generic structure value.

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);


Re: Writing custom structures/extension objects to a server

Posted: 26 Jul 2023, 14:38
by Support Team
Does the issue still persist? If yes can you please check if the DataTypeNodeId is correct?

Code: Select all

structDefinition.setDataTypeId(300000);
Your sample code shows your not specifying a namespace index which means the NodeId is in namespace 0. Namespace 0 is reserved for the OPCF namespace ("http://opcfoundation.org/UA/").

Re: Writing custom structures/extension objects to a server

Posted: 11 Mar 2024, 13:11
by Sandie Flis
Thanks for this tid