.NET Based OPC UA Client/Server SDK  3.3.3.537
Write – Basic Write

Prerequisites

A session with the server must be established.

Description

The following dialog shows an example of a basic implementation of the Write service. Press the button “Show Code” to display the corresponding code, and the button “Help” to show this documentation page.

clienttutorials_basic_write.png

At “Write Request”, you can specify the NodeIds and the values to be written. Press the button “...” right of the field “Node #1” respectively “Node #2” to open a window for browsing the address space.

clienttutorials_read_attribute2.png

Select the desired variables and press the button “OK”. The NodeId and the current value of the variable are now displayed in the corresponding fields.

On pressing the button “...” right of the field “Value #1” respectively “Value #2” a dialog window opens.

clienttutorials_basic_write2.png

Here you can enter the value to be written. The data type of this value is shown above of the input field. You’ll recieve an error message if the value is entered in the wrong format. Press “OK” and the value will appear in the corresponding field.

On pressing the button “Write”, the values are written and the StatusCode for the operation is displayed at “Write Response”. Check the box in front of “Use Asynchronous Pattern” to call “BeginWrite” instead of “Write”.

clienttutorials_basic_write3.png

Sample Code

We need to fill a list with WriteValues for writing the data. In this example, the information that is needed for writing is stored in the “Tag” field of a GUI element. The class VariableNode is used to hold the data.

// build a list of values to write.
List<WriteValue> nodesToWrite = new List<WriteValue>();
// get the first value to write.
if (!String.IsNullOrEmpty(Node1TB.Text))
{
NodeId nodeId = NodeId.Parse(Node1TB.Text);
VariableNode variable = Value1TB.Tag as VariableNode;
if (variable != null)
{
nodesToWrite.Add(new WriteValue()
{
NodeId = nodeId,
AttributeId = Attributes.Value,
Value = new DataValue() { WrappedValue = variable.Value },
UserData = Result1TB // the UserData can be used to store application state information.
});
}
}
// get the second value to write.
if (!String.IsNullOrEmpty(Node2TB.Text))
{
NodeId nodeId = NodeId.Parse(Node2TB.Text);
VariableNode variable = Value2TB.Tag as VariableNode;
if (variable != null)
{
nodesToWrite.Add(new WriteValue()
{
NodeId = nodeId,
AttributeId = Attributes.Value,
Value = new DataValue() { WrappedValue = variable.Value },
UserData = Result2TB // the UserData can be used to store application state information.
});
}
}
// this is a blocking call so show the wait cursor.
Cursor = Cursors.WaitCursor;
// read the value (setting a 10 second timeout).
List<StatusCode> results = session.Write(
nodesToWrite,
new RequestSettings() { OperationTimeout = 10000 });