Writing custom structures/extension objects to a server

Questions regarding the use of the C++ SDK for Server or Client development or integration into customer products ...

Moderator: uasdkcpp

Post Reply
saw
Jr. Member
Jr. Member
Posts: 1
Joined: 16 Mar 2023, 10:56

Writing custom structures/extension objects to a server

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


User avatar
Support Team
Hero Member
Hero Member
Posts: 3064
Joined: 18 Mar 2011, 15:09

Re: Writing custom structures/extension objects to a server

Post 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/").
Best regards
Unified Automation Support Team

Sandie Flis
Jr. Member
Jr. Member
Posts: 1
Joined: 09 Jan 2020, 13:56

Re: Writing custom structures/extension objects to a server

Post by Sandie Flis »

Thanks for this tid

Post Reply