plcopcsiemensopen62541

Open62541 - Writing Array in Siemens Data Block


In Open62541 in C++, I could not find in the documentation how to write data in a Data Block of a Siemens PLC, especially in the case of an array. With the UA_Client_writeValueAttribute(client, UA_NODEID_STRING(4,"DATABLOCK_NAME"."VARIABLE_NAME"), &value); I do not get any error message neither the variable change.

The PLC is the server and the code is running on a Raspberry Pi 4 as client.

Thanks for helping!


Solution

  • I was able to crack the answer! Here it is:

    UA_Variant opcValue;
    UA_Variant_init(&opcValue);
    // Create an array of Float values
    UA_Float dataArray[] = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
    size_t dataArraySize = sizeof(dataArray) / sizeof(UA_Float);
    
    // Create a variant containing the array
    UA_Variant_setArrayCopy(&opcValue, dataArray, dataArraySize, &UA_TYPES[UA_TYPES_FLOAT]);
    
    // Write the array to the OPC UA variable
    UA_NodeId nodeId = UA_NODEID("ns=NAMESPACE_INDEX;s=\"DATABLOCK_NAME\".\"VARIABLE_NAME\""); 
    
    UA_StatusCode writeStatus = UA_Client_writeValueAttribute(client, nodeId, &opcValue);
    if(writeStatus != UA_STATUSCODE_GOOD) {
        // Handle error
    }
    

    So, for my case was: UA_NodeId nodeId = UA_NODEID("ns=4;s=\"opcData\".\"Readings\"");