UA Bundle SDK .NET  2.2.3.276
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Groups Pages
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.

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.

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.

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”.

Sample Code

The following code writes the value for two specified Nodes.

try
{
// get the current session from the parent form.
Session session = m_parent.Session;
// nothing to do if no session.
if (session == null)
{
return;
}
// 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 });
// update the controls.
for (int ii = 0; ii < results.Count; ii++)
{
((TextBox)nodesToWrite[ii].UserData).Text = results[ii].ToString();
}
}
catch (Exception exception)
{
ExceptionDlg.Show(this.Text, exception);
}