Page 1 of 1

How to Resize an Array Variable

Posted: 02 Feb 2014, 15:04
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);

Re: How to Resize an Array Variable

Posted: 05 Feb 2014, 09:24
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

Re: How to Resize an Array Variable

Posted: 07 Feb 2014, 01:33
by loum
Thanks!