Cannot write byte array from .Net to C++

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

Moderator: uasdknet

Post Reply
rhilgartgeip
Full Member
Full Member
Posts: 6
Joined: 06 Jun 2013, 16:59

Cannot write byte array from .Net to C++

Post by rhilgartgeip »

I am having a problem writing a byte array to the OPCUA server. The server is a C++ sdk and the client is a .Net sdk. I am able to write a byte array from the .Net client sdk to a .Net server and am also able to write from the UA Expert client to a C++ Server but am unable to write from the .Net client to a C++ server.
I am sending the byte array with the following command:

dataValue.Value = TypeUtils.Cast(bytes, BuiltInType.ByteString);
nodesToWrite.Add(new WriteValue()
{
NodeId = nodeid,
AttributeId = Attributes.Value,
Value = dataValue
});
List<StatusCode> results = m_session.Write(nodesToWrite);

This will be seen in the Server is Data type = 23 (DataValue) and isArray = 0, what I would expect is Data type (Byte) and isArray = 1.
I have tried to write to the UA demo Server with the same results.
I am looking for the correct command to use to send byte arrays from a .Net Client to a C++ server???
I have tried different combinations of commands and BuiltInTypes with no success.
Thanks

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

Re: Cannot write byte array from .Net to C++

Post by Support Team »

You should be aware that there are two different types:
1) ByteString
2) Array of Bytes
Same same, but different.

So from your code you are creating a ByteString (which is not an Array). But your assignment "Value=dataValue" is not doing what you expecting, you are assinging dataValue to the Variant. Your ByteString is data type = 15 and isArray = 0. So where is the data type = 23 comming from?

Best Regards
Support Team

rhilgartgeip
Full Member
Full Member
Posts: 6
Joined: 06 Jun 2013, 16:59

Re: Cannot write byte array from .Net to C++

Post by rhilgartgeip »

The following 2 code snippets produces in the Server the following results. So my question is why do we not get the proper Datatype and ArrayType with any of the command setups that I have tried?

byte[] bytes = new byte[] { 9, 8};
dataValue.Value = TypeUtils.CastArray(bytes, BuiltInType.Byte, BuiltInType.Byte, null);
nodesToWrite.Add(new WriteValue()
{
NodeId = nodeid,
AttributeId = Attributes.Value,
Value = dataValue
});
List<StatusCode> results = m_session.Write(nodesToWrite);
--------------------------------------------------------------------------------------------------------------------------------
byte[] bytes = new byte[] { 9, 8};
dataValue.Value = TypeUtils.CastArray(bytes, BuiltInType.ByteString, BuiltInType.Byte, null);
nodesToWrite.Add(new WriteValue()
{
NodeId = nodeid,
AttributeId = Attributes.Value,
Value = dataValue
});
List<StatusCode> results = m_session.Write(nodesToWrite);
---------------------------------------------------------------------------------------------------------------------------------
When I us the following code I get an exception on the TypeUtils.Cast line
Exception “Specified cast is not valid.”
byte[] bytes = new byte[] { 9, 8 };
dataValue.Value = TypeUtils.Cast(bytes, BuiltInType.Byte);
nodesToWrite.Add(new WriteValue()
{
NodeId = nodeid,
AttributeId = Attributes.Value,
Value = dataValue
});
List<StatusCode> results = m_session.Write(nodesToWrite);


If I use:
dataValue.Value = TypeUtils.Cast(bytes, BuiltInType.ByteString);
I get the same results as above Datatype= 0x17 and ArrayType = 0x0.
So either I am not setting up the Cast or Write commands properly or there is some other problem.
Is there an example of writing Byte Arrays to a C++ Server that I can follow or can you give me the proper commands or setup to use to achieve this?
Thanks

rhilgartgeip
Full Member
Full Member
Posts: 6
Joined: 06 Jun 2013, 16:59

Re: Cannot write byte array from .Net to C++

Post by rhilgartgeip »

I have found that when I stuff the byte array into a UaBase.ByteCollection i get the proper data into the server:
DataType = 3
ArrayType = 1

UnifiedAutomation.UaBase.ByteCollection byteColl = new ByteCollection(bytes);
dataValue.Value = TypeUtils.Cast(byteColl, BuiltInType.Byte);

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

Re: Cannot write byte array from .Net to C++

Post by Support Team »

Ok, maybe we need some more clarifications here. In .NET both casts give you the same result. In both cases you end up having a ByteString.

Code: Select all

byte[] bytes1 = new byte[] { 1, 2, 3 };
byte[] bytes2 = new byte[] { 3, 4, 5 };

DataValue val1 = new DataValue();
val1.Value = TypeUtils.Cast(bytes1, BuiltInType.ByteString);

DataValue val2 = new DataValue();
val2.Value = TypeUtils.CastArray(bytes2, BuiltInType.Byte, BuiltInType.Byte, null);
Now, when writing to a ByteString in the C++ Server, both approaches work just fine, because in reality a ByteString is written anyway.
But when writing to a Array of Bytes in the C++ Server both MAY fail, because regarding the OPC UA spec a server MAY support the writing of ByteStrings onto Array of Byte, but it does not have to support this feature.

Best Regards
Support Team

Post Reply