How to write to string tags using the .NET OPC Server?

Unified Architecture topics related to OPC UA Specification, compliant behavior and any technical issues of OPC UA, like Security, Information Model, Companion Specs DI, PLCopen, ADI, ...

Moderator: Support Team

Post Reply
walsc18
Jr. Member
Jr. Member
Posts: 1
Joined: 31 Jul 2018, 09:12

How to write to string tags using the .NET OPC Server?

Post by walsc18 »

Hi there,
I am using the .Net based OPC UA Client/Server SDK to run an OPC UA server in C#. I want to make available and write to tags that are strings.
Creating tags that are strings in datatype isn’t a problem in SystemConfiguration.xml. For example:

Code: Select all

<Properties>
      <Name>User</Name>
      <DataType>i=12</DataType>
      <Value>0</Value>
      <Writeable>true</Writeable>
    </Properties>
However, as of now I can only write to tags that are either integers or doubles. I believe this is due to there being only two write functions for each data type within the example code:
Ints:

Code: Select all

 private void Write(int blockAddress, int offset, int value)
        {
   
            byte[] bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, 0, m_registers, blockAddress + offset, bytes.Length);
        }
Doubles:

Code: Select all

private void Write(int blockAddress, int offset, double value)
        {
  

            byte[] bytes = BitConverter.GetBytes((float)value);
            Array.Copy(bytes, 0, m_registers, blockAddress + offset, bytes.Length);
        }
So far, I have tried to write another function like the existing ones that instead can write to strings:

Code: Select all

private void WriteString(int blockAddress, int offset, string value)
        {

            byte[] bytes = Encoding.ASCII.GetBytes((string)value);
            Array.Copy(bytes, 0, m_registers, blockAddress + offset, bytes.Length); 
       
        }    
But this returns a hexadecimal string instead of just a string.
For example, if I write “test” to the “User” tag this is the result I get in UAExpert (screenshot attached).
Is this correct or should I be able to get the exact string that I entered?

I have also tried following the official documentation which is to use the UaModeler to export auto-generated code and include it in my project however I run into a similar problem which is the string is there but has a Null value and is un-writeable.

Post Reply