QuickOPC User's Guide and Reference
DefineMapping(AbstractMappingSource,AbstractMapping,AbstractLinkingTarget) Method
Example 



OpcLabs.BaseLib Assembly > OpcLabs.BaseLib.LiveMapping Namespace > AbstractMapper Class > DefineMapping Method : DefineMapping(AbstractMappingSource,AbstractMapping,AbstractLinkingTarget) Method
The mapping source to be used with the mapping.
The mapping being defined.
The linking target to be set for the mapping.
Defines a mapping, assuring that mapping sources are not duplicated.
Syntax
'Declaration
 
Public Overloads Sub DefineMapping( _
   ByVal mappingSource As AbstractMappingSource, _
   ByVal mapping As AbstractMapping, _
   ByVal linkingTarget As AbstractLinkingTarget _
) 
'Usage
 
Dim instance As AbstractMapper
Dim mappingSource As AbstractMappingSource
Dim mapping As AbstractMapping
Dim linkingTarget As AbstractLinkingTarget
 
instance.DefineMapping(mappingSource, mapping, linkingTarget)

Parameters

mappingSource
The mapping source to be used with the mapping.
mapping
The mapping being defined.
linkingTarget
The linking target to be set for the mapping.
Remarks

The method attempts to find an equivalent mapping source that already exists in the mapper, and if found, uses it. Otherwise, the new mapping source is added to mapping sources of the mapper.

Example

.NET

.NET

.NET

.NET

// This example for OPC DA type-less mapping shows how to define a mapping and perform a read operation.

using System;
using OpcLabs.BaseLib.ComponentModel.Linking;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.LiveMapping;

namespace DocExamples.DataAccess._DAClientMapper
{
    partial class DefineMapping
    {
        class MyClass2
        {
            public object Value { get; set; }
        }

        public static void Main1()
        {
            #region Example-DefineAndRead
            // Instantiate the client mapper object.
            var mapper = new DAClientMapper();

            var target = new MyClass2();

            // Define a type-less mapping.

            mapper.DefineMapping(
                 new DAClientItemSource("OPCLabs.KitServer.2", "Simulation.Register_I4", DADataSource.Cache),
                 new DAClientItemMapping(typeof(Int32)),
                 new ObjectMemberLinkingTarget(target.GetType(), target, "Value"));

            // Perform a read operation.
            mapper.Read();
'  This example for OPC DA type-less mapping shows how to define a mapping and perform a read operation.

Imports OpcLabs.BaseLib.ComponentModel.Linking
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.LiveMapping

Namespace _DAClientMapper
    Partial Friend Class DefineMapping
        Class MyClass2
            Public Property Value As Object
        End Class

#Region "Example-DefineAndRead"
        Public Shared Sub Main1()
            Dim mapper = New DAClientMapper()
            Dim target = New MyClass2()

            ' Define a type-less mapping.

            mapper.DefineMapping( _
                     New DAClientItemSource( _
                         "OPCLabs.KitServer.2",
                         "Simulation.Register_I4",
                         New DAReadParameters(DADataSource.Cache)),
                     New DAClientItemMapping(GetType(Int32)),
                     New ObjectMemberLinkingTarget(target.GetType(), target, "Value"))

            ' Perform a read operation.
            mapper.Read()

            ' Display the result.
            Console.WriteLine(target.Value)
        End Sub
// This example for OPC DA type-less mapping shows how to define mappings of various kinds, and perform subscribe and 
// unsubscribe operations.

using System;
using System.Threading;
using OpcLabs.BaseLib.ComponentModel.Linking;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.Generic;
using OpcLabs.EasyOpc.DataAccess.LiveMapping;

namespace DocExamples.DataAccess._DAClientMapper
{
    partial class DefineMapping
    {
        class MyClassMappingKinds
        {
            public Double CurrentValue
            {
                set
                {
                    // Display the incoming value
                    Console.WriteLine("Value: {0}", value);
                }
            }

            public DAVtq<Double> CurrentVtq
            {
                set
                {
                    // Display the incoming Vtq
                    Console.WriteLine("Vtq: {0}", value);
                }
            }

            public Exception CurrentException
            {
                set
                {
                    // Display the incoming exception
                    Console.WriteLine("Exception: {0}", value);
                }
            }

            public DAVtqResult<Double> CurrentResult
            {
                set
                {
                    // Display the incoming result
                    Console.WriteLine("Result: {0}", value);
                }
            }
        }

        public static void MappingKinds()
        {
            // Instantiate the client mapper object.
            var mapper = new DAClientMapper();

            var target = new MyClassMappingKinds();

            // Define several type-less mappings for the same source, with different mapping kinds.

            Type targetType = target.GetType();
            var source = new DAClientItemSource("OPCLabs.KitServer.2", "Demo.Ramp", 1000, DADataSource.Cache);

            mapper.DefineMapping(
                 source,
                 new DAClientItemMapping(typeof(Double)),
                 new ObjectMemberLinkingTarget(targetType, target, "CurrentValue"));

            mapper.DefineMapping(
                 source,
                 new DAClientItemMapping(typeof(Double), DAItemMappingKind.Vtq),
                 new ObjectMemberLinkingTarget(targetType, target, "CurrentVtq"));

            mapper.DefineMapping(
                 source,
                 new DAClientItemMapping(typeof(Double), DAItemMappingKind.Exception),
                 new ObjectMemberLinkingTarget(targetType, target, "CurrentException"));

            mapper.DefineMapping(
                 source,
                 new DAClientItemMapping(typeof(Double), DAItemMappingKind.Result),
                 new ObjectMemberLinkingTarget(targetType, target, "CurrentResult"));

            // Perform a subscribe operation.
            mapper.Subscribe(true);

            Thread.Sleep(30 * 1000);

            // Perform an unsubscribe operation.
            mapper.Subscribe(false);
        }
    }
}
' This example for OPC DA type-less mapping shows how to define mappings of various kinds, and perform subscribe and 
' unsubscribe operations.

Imports OpcLabs.BaseLib.ComponentModel.Linking
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.Generic
Imports OpcLabs.EasyOpc.DataAccess.LiveMapping

Namespace _DAClientMapper
    Partial Friend Class DefineMapping
        Class MyClassMappingKinds
            Public WriteOnly Property CurrentValue As Double
                Set(value As Double)
                    ' Display the incoming value
                    Console.WriteLine("Value: {0}", value)
                End Set
            End Property

            Public WriteOnly Property CurrentVtq As DAVtq(Of Double)
                Set(value As DAVtq(Of Double))
                    ' Display the incoming Vtq
                    Console.WriteLine("Vtq: {0}", value)
                End Set
            End Property

            Public WriteOnly Property CurrentException As Exception
                Set(value As Exception)
                    ' Display the incoming exception
                    Console.WriteLine("Exception: {0}", value)
                End Set
            End Property

            Public WriteOnly Property CurrentResult As DAVtqResult(Of Double)
                Set(value As DAVtqResult(Of Double))
                    ' Display the incoming result
                    Console.WriteLine("Result: {0}", value)
                End Set
            End Property
        End Class

        Public Shared Sub MappingKinds()
            Dim mapper = New DAClientMapper()
            Dim target = New MyClassMappingKinds()

            ' Define several type-less mappings for the same source, with different mapping kinds.

            Dim targetType = target.GetType()
            Dim source = New DAClientItemSource("OPCLabs.KitServer.2", "Demo.Ramp", 1000, DADataSource.Cache)

            mapper.DefineMapping(
                 source,
                 New DAClientItemMapping(GetType(Double)),
                 New ObjectMemberLinkingTarget(targetType, target, "CurrentValue"))

            mapper.DefineMapping(
                 source,
                 New DAClientItemMapping(GetType(Double), DAItemMappingKind.Vtq),
                 New ObjectMemberLinkingTarget(targetType, target, "CurrentVtq"))

            mapper.DefineMapping(
                 source,
                 New DAClientItemMapping(GetType(Double), DAItemMappingKind.Exception),
                 New ObjectMemberLinkingTarget(targetType, target, "CurrentException"))

            mapper.DefineMapping(
                 source,
                 New DAClientItemMapping(GetType(Double), DAItemMappingKind.Result),
                 New ObjectMemberLinkingTarget(targetType, target, "CurrentResult"))

            ' Perform a subscribe operation.
            mapper.Subscribe(True)

            Threading.Thread.Sleep(30 * 1000)

            ' Perform an unsubscribe operation.
            mapper.Subscribe(False)
        End Sub
    End Class
End Namespace
// This example for OPC DA type-less mapping shows how to define a mapping and perform subscribe and unsubscribe operations.

using System;
using System.Threading;
using OpcLabs.BaseLib.ComponentModel.Linking;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.LiveMapping;

namespace DocExamples.DataAccess._DAClientMapper
{
    partial class DefineMapping
    {
        class MyClassSubscribe
        {
            public Double Value
            {
                set
                {
                    // Display the incoming value
                    Console.WriteLine(value);
                }
            }
        }

        public static void Subscribe()
        {
            // Instantiate the client mapper object.
            var mapper = new DAClientMapper();

            var target = new MyClassSubscribe();

            // Define a type-less mapping.

            mapper.DefineMapping(
                 new DAClientItemSource("OPCLabs.KitServer.2", "Demo.Ramp", 1000, DADataSource.Cache),
                 new DAClientItemMapping(typeof(Double)),
                 new ObjectMemberLinkingTarget(target.GetType(), target, "Value"));

            // Perform a subscribe operation.
            mapper.Subscribe(true);

            Thread.Sleep(30 * 1000);

            // Perform an unsubscribe operation.
            mapper.Subscribe(false);
        }
    }
}
' This example for OPC DA type-less mapping shows how to define a mapping and perform subscribe and unsubscribe operations.

Imports OpcLabs.BaseLib.ComponentModel.Linking
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.LiveMapping

Namespace _DAClientMapper
    Partial Friend Class DefineMapping
        Class MyClassSubscribe
            Public WriteOnly Property Value As Double
                Set(value As Double)
                    ' Display the incoming value
                    Console.WriteLine(value)
                End Set
            End Property
        End Class

        Public Shared Sub Subscribe()
            Dim mapper = New DAClientMapper()
            Dim target = New MyClassSubscribe()

            ' Define a type-less mapping.

            mapper.DefineMapping(
                 New DAClientItemSource("OPCLabs.KitServer.2", "Demo.Ramp", 1000, DADataSource.Cache),
                 New DAClientItemMapping(GetType(Double)),
                 New ObjectMemberLinkingTarget(target.GetType(), target, "Value"))

            ' Perform a subscribe operation.
            mapper.Subscribe(True)

            Threading.Thread.Sleep(30 * 1000)

            ' Perform an unsubscribe operation.
            mapper.Subscribe(False)
        End Sub
    End Class
End Namespace
// This example for OPC UA type-less mapping shows how to define a mapping and perform a read operation.

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using OpcLabs.BaseLib.ComponentModel.Linking;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.LiveMapping;

namespace UADocExamples._UAClientMapper
{
    class DefineMapping
    {
        class MyClass2
        {
            public object Value { get; set; }
        }

        public static void Main1()
        {
#region Example-DefineAndRead

            UAEndpointDescriptor endpointDescriptor =
                "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer";
            // or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            // or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            var mapper = new UAClientMapper();
            var target = new MyClass2();

            // Define a type-less mapping.

            MemberInfo memberInfo = target.GetType().GetMember("Value").SingleOrDefault();
            Debug.Assert(memberInfo != null);

            mapper.DefineMapping(
                new UAClientDataMappingSource(
                    endpointDescriptor,
                    "nsu=http://test.org/UA/Data/ ;i=10389",
                    UAAttributeId.Value,
                    UAIndexRangeList.Empty,
                    UAReadParameters.CacheMaximumAge),
                new UAClientDataMapping(typeof(Int32)),
                new ObjectMemberLinkingTarget(target.GetType(), target, memberInfo));

            // Perform a read operation.
            mapper.Read();
' This example for OPC UA type-less mapping shows how to define a mapping and perform a read operation.

Imports System.Linq
Imports OpcLabs.BaseLib.ComponentModel.Linking
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.LiveMapping

Namespace _UAClientMapper
    Friend Class DefineMapping
        Class MyClass2
            Public Property Value As Object
        End Class

#Region "Example-DefineAndRead"
        Public Shared Sub Main1()

            ' Define which server we will work with.
            Dim endpointDescriptor As UAEndpointDescriptor =
                    "opc.tcp://opcua.demo-this.com:51210/UA/SampleServer"
            ' or "http://opcua.demo-this.com:51211/UA/SampleServer" (currently not supported)
            ' or "https://opcua.demo-this.com:51212/UA/SampleServer/"

            Dim mapper = New UAClientMapper()
            Dim target = New MyClass2()

            ' Define a type-less mapping.

            Dim memberInfo = target.GetType().GetMember("Value").SingleOrDefault()
            Debug.Assert(memberInfo IsNot Nothing)

            mapper.DefineMapping( _
                New UAClientDataMappingSource( _
                    endpointDescriptor, _
                    "nsu=http://test.org/UA/Data/ ;i=10389", _
                    UAAttributeId.Value, _
                    UAIndexRangeList.Empty, _
                    UAReadParameters.CacheMaximumAge), _
                New UAClientDataMapping(GetType(Int32)), _
                New ObjectMemberLinkingTarget(target.GetType(), target, memberInfo))

            ' Perform a read operation.
            mapper.Read()

            ' Display results
            Console.WriteLine(target.Value)
        End Sub
Requirements

Target Platforms: .NET Framework: Windows 10 (selected versions), Windows 11 (selected versions), Windows Server 2016, Windows Server 2022; .NET: Linux, macOS, Microsoft Windows

See Also