UA Bundle SDK .NET  2.2.1.258
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Groups Pages
Read – Basic Read

Prerequisites

A session with the server must be established.

Description

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

On pressing the button “Read”, the two variables Server/ServerStatus/State and Server/ServerStatus/CurrentTime are read. The response, i.e. the value of the variables is displayed in the corresponding fields at “Read Response”. Check the box in front of “Use Asynchronous Pattern” to call “BeginRead” instead of “Read”.

See Read – Read Attribute for an example which allows the user to select the Node and the attribute to read.

Sample Code

The following code reads the values of two OPC UA defined variables from the server and displays the values in the user interface.

NodeIds of OPC UA defined nodes are contained as constants for different node classes like

  • UnifiedAutomation.UaBase.VariableIds
  • UnifiedAutomation.UaBase.ObjectIds
  • UnifiedAutomation.UaBase.MethodIds
try
{
// build a list of values to read.
List<ReadValueId> nodesToRead = new List<ReadValueId>();
// read the current time and state from the server using the predefined node ids.
nodesToRead.Add(new ReadValueId() { NodeId = VariableIds.Server_ServerStatus_State, AttributeId = Attributes.Value });
nodesToRead.Add(new ReadValueId() { NodeId = VariableIds.Server_ServerStatus_CurrentTime, AttributeId = Attributes.Value });
// read the value (setting a 10 second timeout).
List<DataValue> results = session.Read(
nodesToRead,
0,
new RequestSettings() { OperationTimeout = 10000 });
// the GetEnumerationText call reads the EnumStrings or EnumValues property and returns the text that matches the value.
ServerStateTB.Text = session.Cache.GetEnumerationText(VariableIds.Server_ServerStatus_State, results[0]);
// the ToString() method on a DataValue will return the StatusCode if it is Bad instead of the Value.
CurrentTimeTB.Text = results[1].ToString();
}
catch (Exception exception)
{
ExceptionDlg.Show(this.Text, exception);
}