How to get MaxStringLength, "BadOutOfRange" after writing

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
hartmut_irs
Jr. Member
Jr. Member
Posts: 4
Joined: 26 Oct 2018, 08:31

How to get MaxStringLength, "BadOutOfRange" after writing

Post by hartmut_irs »

Before writing a string value, we want to check the current string length with the defined max string length.

Currently after writing a String value we get the error message "BadOutOfRange". The length of the written value is longer than the defined max string length. This behaviour is correct.

How do we get the information about the max string length for a specific string value? We couldn'd find any code example.

User avatar
Support Team
Hero Member
Hero Member
Posts: 3068
Joined: 18 Mar 2011, 15:09

Re: How to get MaxStringLength, "BadOutOfRange" after writin

Post by Support Team »

Hello,

A server can expose the information about the maximum length of a string with the optional property MaxStringLength.

Description of MaxStringLength in the OPC UA specification:
Only used for DataVariables having a String DataType.
This optional Property indicates the maximum number of bytes supported by the DataVariable.
Best regards
Unified Automation Support Team

hartmut_irs
Jr. Member
Jr. Member
Posts: 4
Joined: 26 Oct 2018, 08:31

Re: How to get MaxStringLength, "BadOutOfRange" after writin

Post by hartmut_irs »

Hello Support-Team,
thank you for the answer.

On server side we have a Siemens OPCUA and the supplier is not able to define a property MaxStringLength.

The Siemens-S7 STRING type is defined like:
Byte0: max string length
Byte1: current string length
Byte2,...: data

An example were we facing the problem:
DataType: STRING (the supplier defined a max. length of 2)

value = 'AB' -> OK
vaule = 'ABC' -> BadOutOfRange

Code: Select all

_gValue.Value = TypeUtils.Cast(gValue, gNode.Type);

WriteValueCollection coNodes = new WriteValueCollection();
coNodes.Add(new WriteValue() { NodeId = gNode.NodeId, AttributeId = Attributes.Value, Value = _gValue });

List<StatusCode> coResults = gSession.Write(coNodes);
if (!StatusCode.IsGood(coResults[0]))
{
	throw new Exception("Failed to write item, result status: " + coResults[0].ToString() + ".");  // BadOutOfRange when Value = 'ABC'
}
When we write the 'AB' to the OPC, the value is correctly written. The 2 length-bytes ahead defined by Siemens-S7 are considered.
We can verfify this with the UAExpert tool.

How we can make a workaround to solve this problem?

User avatar
Support Team
Hero Member
Hero Member
Posts: 3068
Joined: 18 Mar 2011, 15:09

Re: How to get MaxStringLength, "BadOutOfRange" after writin

Post by Support Team »

Hello,

You need to ask the Siemens support for solving the issue. From the OPC UA point of view you can only try writing the value and evaluating the returned StatusCode.
Best regards
Unified Automation Support Team

Post Reply