OPC UA C# client connecting to OPC UA CODESYS server

dogulas
2024-12-17
2024-12-30
  • dogulas - 2024-12-17

    Ok, I have a simple test environment. From C#, using OPC UA, I can read DINTs, write DINTs, read Structs, but when I try to write a struct I get BadNotWritable.

    I am using Communication Manager, OPC UA Server, and IEC Symbol Publishing. I have a struct defined with one single DINT in it. The instance of the struct is in GVL. The struct is marked read/write in IEC Symbol Publishing.

    Any ideas? Pointers are welcome.

        public static async Task WriteStructA(byte[] structAData)
        {
            if (_session == null)
                throw new Exception("session is null");
    
            // create a RequestHeader
            RequestHeader requestHeader = new RequestHeader
            {
                Timestamp = DateTime.UtcNow,
                TimeoutHint = 30000 // timeout in milliseconds
            };
    
            // create an ExtensionObject to wrap the value to be written
            ExtensionObject extensionObject = new ExtensionObject(
                new ExpandedNodeId("GVL_StructA", 5),
                structAData
                );
    
            // create a WriteValue object to hold the value to be written
            WriteValue writeValue = new WriteValue
            {
                NodeId = _structANodeId,
                AttributeId = Attributes.Value,
                Value = new DataValue(extensionObject)
            };
    
            // put the WriteValue object into a WriteValueCollection object
            WriteValueCollection writeValueCollection = new WriteValueCollection() { writeValue };
    
            // perform the write operation asynchronously
            WriteResponse writeResponse = await _session.WriteAsync(
                requestHeader,
                writeValueCollection,
                CancellationToken.None
                );
    
            // check the result of the write operation
            if (writeResponse.Results[0] != StatusCodes.Good)
                throw new Exception("Write failed: " + writeResponse.Results[0]);
        }
    
     
  • dogulas - 2024-12-30

    Ok, for the sake of completeness in case anyone else has this problem. I can tell you that what finally worked for me was to change the line:

        // create an ExtensionObject to wrap the value to be written
        ExtensionObject extensionObject = new ExtensionObject(
            new ExpandedNodeId("GVL_StructA", 5),
            structAData
            );
    

    to:

        // create an ExtensionObject to wrap the value to be written
        ExtensionObject extensionObject = new ExtensionObject(
            new ExpandedNodeId("GVL_StructA|defaultbinary", 5),
            structAData
            );
    

    I'm sure there are many other problems that could cause this very generic error, but this was my problem.

     
    πŸ‘
    1

Log in to post a comment.