Quickstarts.DataAccessServer.UnderlyingSystemBlock.WriteTagValue C# (CSharp) Method

WriteTagValue() public method

Writes the tag value.
public WriteTagValue ( string tagName, object value ) : uint
tagName string Name of the tag.
value object The value.
return uint
        public uint WriteTagValue(string tagName, object value)
        {
            UnderlyingSystemTag tag = null;
            TagsChangedEventHandler onTagsChanged = null;

            lock (m_tags)
            {
                onTagsChanged = OnTagsChanged;

                // find the tag.
                tag = FindTag(tagName);
                
                if (tag == null)
                {
                    return StatusCodes.BadNodeIdUnknown;
                }

                // cast value to correct type.
                try
                {
                    switch (tag.DataType)
                    {
                        case UnderlyingSystemDataType.Integer1:
                        {
                            tag.Value = (sbyte)value;
                            break;
                        }

                        case UnderlyingSystemDataType.Integer2:
                        {
                            tag.Value = (short)value;
                            break;
                        }

                        case UnderlyingSystemDataType.Integer4:
                        {
                            tag.Value = (int)value;
                            break;
                        }

                        case UnderlyingSystemDataType.Real4:
                        {
                            tag.Value = (float)value;
                            break;
                        }

                        case UnderlyingSystemDataType.String:
                        {
                            tag.Value = (string)value;
                            break;
                        }
                    }
                }
                catch
                {
                    return StatusCodes.BadTypeMismatch;
                }

                // updated the timestamp.
                tag.Timestamp = DateTime.UtcNow;
            }

            // raise notification.
            if (tag != null && onTagsChanged != null)
            {
                onTagsChanged(new UnderlyingSystemTag[] { tag });
            }

            return StatusCodes.Good;
        }

Usage Example

        /// <summary>
        /// Used to receive notifications when the value attribute is read or written.
        /// </summary>
        public ServiceResult OnWriteTagValue(
            ISystemContext context,
            NodeState node,
            ref object value)
        {
            UnderlyingSystem system = context.SystemHandle as UnderlyingSystem;

            if (system == null)
            {
                return(StatusCodes.BadCommunicationError);
            }

            UnderlyingSystemBlock block = system.FindBlock(m_blockId);

            if (block == null)
            {
                return(StatusCodes.BadNodeIdUnknown);
            }

            uint error = block.WriteTagValue(node.SymbolicName, value);

            if (error != 0)
            {
                // the simulator uses UA status codes so there is no need for a mapping table.
                return(error);
            }

            return(ServiceResult.Good);
        }