.NET Based OPC UA Client/Server SDK  3.1.0.500
Write Implementation for DiscreteItemTypes

The DemoServer Address Space contains examples of the following Subtypes of the DiscreteItemType in Demo → 010_ComplianceTest → DA Profile → DataItemDiscreteType:

  1. TwoStateDiscreteType
  2. MultiStateDiscreteType
  3. MultiStateValueDiscreteType

TwoStateDiscreteType

The DataType of an instance of TwoStateDiscreteType is Boolean. The text representing the value is stored in the properties TrueState and FalseState. So it is not necessary to prevent clients from writing values that are not allowed.

MultiStateDiscreteType

Instances of a MultiStateDiscreteType have a property called EnumStrings. The value of this property is an array of LocalizedTexts. The corresponding text for a value can be found at the array index of the value of the property. E.g. the corresponding textual representation of the value 2 can be found at the array index 2 of the value of the property.

So the allowed values for an instance of MultiStateDiscreteType are:
0 ≤ allowed value < array length.

If a client tries to write a value that is not allowed, the server has to return BadOutOfRange (see example code below). The code snippet is taken from the class EnumStringDataSource which can be found in the file Demo → ValueDataStore.cs in the Visual Studio Solution for the Demo Server.

public override StatusCode Write(int componentIndex, Variant value, StatusCode status, DateTime timestamp)
{
if (timestamp == DateTime.MinValue)
{
timestamp = DateTime.UtcNow;
}
if (componentIndex == 0)
{
UnifiedAutomation.UaBase.TypeInfo typeInfo = TypeUtils.IsInstanceOfDataType(value, TypeInfo);
if (typeInfo == null)
{
return StatusCodes.BadTypeMismatch;
}
try
{
int newValue = value.ToInt32();
if (newValue >= 0 && newValue < EnumStrings.Length)
{
SelectedIndex = newValue;
Status = status;
Timestamp = timestamp;
return StatusCodes.Good;
}
}
catch (Exception)
{
// assume out of range error.
}
return StatusCodes.BadOutOfRange;
}

MultiStateValueDiscreteType

The behavior for an instance of MultiStateValueDiscretetType is quite similar. The instances have a property called EnumValues. The value of this property contains an array of EnumValueType. I.e. the value contains an array of value-text pairs. So the server has to check wheter the value of the property contains the value to write (see example below). The code snippet is taken from the class EnumValueDataSource which can be found in the file Demo → ValueDataStore.cs in the Visual Studio Solution for the Demo Server.

public override StatusCode Write(int componentIndex, Variant value, StatusCode status, DateTime timestamp)
{
if (timestamp == DateTime.MinValue)
{
timestamp = DateTime.UtcNow;
}
if (componentIndex == 0)
{
UnifiedAutomation.UaBase.TypeInfo typeInfo = TypeUtils.IsInstanceOfDataType(value, TypeInfo);
if (typeInfo == null)
{
return StatusCodes.BadTypeMismatch;
}
try
{
long newValue = value.ToInt64();
for (int ii = 0; ii < EnumValues.Length; ii++)
{
if (EnumValues[ii].Value == newValue)
{
SelectedIndex = ii;
Status = status;
Timestamp = timestamp;
return StatusCodes.Good;
}
}
}
catch (Exception)
{
// assume out of range error.
}
return StatusCodes.BadOutOfRange;
}