How to Resize an Array Variable

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

Moderator: uasdkc

Post Reply
loum
Jr. Member
Jr. Member
Posts: 3
Joined: 07 Aug 2013, 18:54

How to Resize an Array Variable

Post by loum »

Hi UA folks,

In an ANSI C UA server I have created a float array variable (see the code below.

How should I resize the array variable during server operation with the ANSI C SDK?

Thanks
Lou McNamee

Code: Select all

	g_p3521CBInterface->CreateStringNodeIdEx(&parentNodeId, System3521_Operation_Cmd, g_UaProvider3521_uNamespaceIndex1);
	g_p3521CBInterface->GetNode(pAddressSpace,
			&parentNodeId,
			&pParent);
	g_p3521CBInterface->CreateNumericNodeIdEx(&nodeInfo.TypeDefinition, OpcUaId_XYArrayItemType, 0);
	g_p3521CBInterface->CreateStringNodeIdEx(&nodeInfo.NodeId, System3521_Operation_Cmd_Parm, g_UaProvider3521_uNamespaceIndex1);
	nodeInfo.NodeClass = OpcUa_NodeClass_Variable;
	g_p3521CBInterface->CreateLocalizedText(&nodeInfo.sDisplayName, m_LanguageEnUSLabel.c_str(), "Out");
	g_p3521CBInterface->CreateQualifiedNameEx(&nodeInfo.sBrowseName, "Out", g_UaProvider3521_uNamespaceIndex1);
	g_p3521CBInterface->CreateLocalizedText(&nodeInfo.sDescription, m_LanguageEnUSLabel.c_str(), OpcUa_Null);
	uStatus = CreateNode(pAddressSpace,
			&pNode,
			pParent,
			&referenceTypeId,
			&nodeInfo);

	if (uStatus != OpcUa_Good)
		syslog(LOG_ERR,"Error creating OPC UA variable %s", System3521_Operation_Wave_Out);

	OpcUa_Variable_SetAccessLevel(pNode, 3);
	OpcUa_Variable_SetDataType_Numeric(pNode, OpcUaId_Float, 0);
	OpcUa_Variable_SetHistorizing(pNode, OpcUa_False);
	OpcUa_Variable_SetMinimumSamplingInterval(pNode, m_minSamplingInterval);
	OpcUa_Variable_SetValueRank(pNode, OpcUa_ValueRanks_OneDimension);
	OpcUa_Variant_Initialize(&value);
	pArrayDimensions = (OpcUa_UInt32 *)g_p3521CBInterface->Alloc(sizeof(OpcUa_UInt32) * 1);
	pArrayDimensions[ 0 ] = m_numCmdParams;
	OpcUa_Variable_AttachArrayDimensions(pNode, pArrayDimensions, 1);
	value.Datatype = OpcUaType_Float;
	value.ArrayType = OpcUa_VariantArrayType_Array;
	value.Value.Array.Length = m_numCmdParams;
	value.Value.Array.Value.FloatArray = (OpcUa_Float *)g_p3521CBInterface->Alloc(sizeof(OpcUa_Float) * m_numSamples);
	for(OpcUa_UInt32 i=0; i<m_numSamples; i++)
	{
		value.Value.Array.Value.FloatArray[i] = 0;
	}

	OpcUa_Variable_AttachValue(pNode, &value);

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

Re: How to Resize an Array Variable

Post by Support Team »

Hello Lou,

this can be done like this:

Code: Select all

 /* get OpcUa_Variable and store in pNode */

        OpcUa_Variant *pValue = OpcUa_Null;
        OpcUa_Int32 newLength = 3;
        OpcUa_Float *pNewArray = OpcUa_Null;

        pValue = OpcUa_Variable_GetValue(pNode);
        OpcUa_GotoErrorIfNull(pValue, OpcUa_BadInternalError);

        pNewArray = OpcUa_ReAlloc(pValue->Value.Array.Value.FloatArray, newLength * sizeof(OpcUa_Float));
        OpcUa_GotoErrorIfAllocFailed(pNewArray);

        pValue->Value.Array.Length = newLength;
        pValue->Value.Array.Value.FloatArray = pNewArray;

        /* now the new array is set and the elements can be modified / set */
        /* if the ArrayDimensions of the variable were set to != 0, they have to get updated, too */
Best regards
Support Team

loum
Jr. Member
Jr. Member
Posts: 3
Joined: 07 Aug 2013, 18:54

Re: How to Resize an Array Variable

Post by loum »

Thanks!

Post Reply