Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.38/5 (4 votes)
See more:
Hi, I'm trying to Write a C#.net Windows form app to connect to an OPC UA Server

My application should connect to the Server using an endpoint, waiting for a value called "trigger" to change, once the value changes, the app will read the value of another node called "Bcode" and do some database queries and return values, and update some values like "OrderId", "AreaID" on the OPC UA Server.

writing the queries was ok, but I couldn't connect and read or write values to the server no matter what codes I tried.

I want to use OPC.UA Foundation .NET Standard library since it is the free library I found, all others are simpler but cost money.

here is the last code I tried, which has many errors.

What I have tried:

using System;
    using System.Windows.Forms;
    using Opc.Ua;
    using Opc.Ua.Client;
    
    namespace OPCUAExample
    {
        public partial class MainForm : Form
        {
            private UAClient uaClient;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void MainForm_Load(object sender, EventArgs e)
            {
                try
                {
                    // Create an instance of the OPC UA client
                    uaClient = new UAClient();
    
                    // Set up the OPC UA server endpoint
                    uaClient.EndpointUrl = "opc.tcp://localhost:4840"; // Replace with your server's endpoint
    
                    // Connect to the OPC UA server
                    uaClient.Connect();
    
                    // Read a value from the server
                    NodeId variableNodeId = new NodeId("VariableNodeId"); // Replace with the actual NodeId of the variable
                    DataValue dataValue = uaClient.ReadValue(variableNodeId);
    
                    // Display the value in the label
                    lblValue.Text = dataValue.Value.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error: {ex.Message}", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
            private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                // Disconnect from the OPC UA server when the form is closing
                uaClient?.Disconnect();
            }
        }
    }


    using System;
    using System.Windows.Forms;
    using Opc.Ua;
    using Opc.Ua.Client;
    
    namespace OPCUAExample
    {
        public partial class MainForm : Form
        {
            private Session session;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void MainForm_Load(object sender, EventArgs e)
            {
                try
                {
                    // Create an instance of the OPC UA client session
                    session = new Session();
    
                    // Set up the OPC UA server endpoint
                    var endpointUrl = "opc.tcp://localhost:4840"; // Replace with your server's endpoint
                    var endpointDescription = CoreClientUtils.SelectEndpoint(endpointUrl, useSecurity: false);
                    session.Endpoint = endpointDescription;
    
                    // Connect to the OPC UA server
                    session.Open();
    
                    // Read a value from the server
                    NodeId variableNodeId = new NodeId("VariableNodeId"); // Replace with the actual NodeId of the variable
                    DataValue dataValue = session.ReadValue(variableNodeId);
    
                    // Display the value in the label
                    lblValue.Text = dataValue.Value.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Error: {ex.Message}", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
            private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                // Disconnect from the OPC UA server when the form is closing
                session?.Close();
            }
        }
    }
Posted
Comments
Richard Deeming 5-Feb-24 5:34am    
If the library you're using doesn't work, then you either need to ask the people who wrote that library for help, or switch to a different library.

1 solution

Some years ago, I had to implement an OPC UA Client and server with .NET. Here are some steps from my journey that may help you:

- Nuget packages available are not working as expected. Use the source code directly from GitHub.
- The first test should be done using the samples available on the GitHub project. There are both server and client samples.
- When you try with a PLC, start with no security.
- Use the sample OPC UA client from GitHub to read the PLC node, just to understand how to compose the node ID.
- if sample client is working and you app no, try to debug the sample client win form.

UA-.NETStandard-Samples/Samples at master · OPCFoundation/UA-.NETStandard-Samples · GitHub[^]

This should be the link to the samples, hope it help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900