Issues connecting to OPC UA server using Opc.Ua.Client in version 1.1.0

87 Views Asked by At

Description: I'm facing challenges while attempting to connect to an OPC UA server using the Opc.Ua.Client library, specifically version 1.1.0. I've tried several approaches based on available examples, but I'm encountering errors related to method overloads and response handling.

Details:

  1. Library Version: 1.1.0
  2. Objective: Connect to an OPC UA server, perform browsing, and retrieve channels.
  3. Code Snippet:
 using Microsoft.AspNetCore.Mvc;
 using Opc.Ua.Client;
 using Opc.Ua;

 namespace InternshipProject.Controllers
 {
    public class OPCConnectionController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult ConnectToOpc(string opcConnectionString)
        {
            try
            {
                var endpointDescription = CoreClientUtils.SelectEndpoint(opcConnectionString, useSecurity: false);
                var endpointConfiguration = EndpointConfiguration.Create();

                using (var session = Session.Create(
                    configuration: endpointConfiguration,
                    endpoint: endpointDescription,
                    updateBeforeConnect: false,
                    checkDomain: false,
                    sessionName: "OPC Session"))
                {
                    session.ReturnDiagnostics = DiagnosticsMasks.All;

                    session.Open(ApplicationType.Client);

                    // Get channels
                    var channels = BrowseChannels(session);

                    // Pass channels to the view
                    ViewBag.Channels = channels;

                    ViewBag.StatusMessage = "Connected to OPC server.";
                    return View("Index");
                }
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = $"Failed to connect to OPC server: {ex.Message}";
                return View("Index");
            }
        }

        private List<string> BrowseChannels(Session session)
        {
            var channels = new List<string>();

            var browseRequest = new BrowseRequest
            {
                NodesToBrowse = new BrowseDescriptionCollection
            {
                new BrowseDescription
                {
                    NodeId = Objects.RootFolder,
                    BrowseDirection = BrowseDirection.Forward,
                    ReferenceTypeId = ReferenceTypeIds.HierarchicalReferences,
                    IncludeSubtypes = true,
                    NodeClassMask = (uint)NodeClass.Object,
                    ResultMask = (uint)BrowseResultMask.All
                }
            },
                RequestedMaxReferencesPerNode = 0
            };

            var browseResponse = session.Browse(null, browseRequest);

            foreach (var result in browseResponse.Results)
            {
                foreach (var referenceDescription in result.References)
                {
                    channels.Add(referenceDescription.DisplayName);
                }
            }

            return channels;
        }
    }
 }

Errors Encountered:

  • Error: No overload for method 'Create' takes 5 arguments.
  • Error: No overload for method 'Browse' takes 2 arguments.
  • Error: 'ResponseHeader' does not contain a definition for 'Results'.

Question:

How can I modify my code to resolve these errors and successfully connect to the OPC UA server using Opc.Ua.Client version 1.1.0?

Note: I appreciate any insights or guidance on resolving this issue. If there are specific changes in version 1.1.0 that affect the code, please let me know.

0

There are 0 best solutions below