How to connect to an OPC UA server using C# .NET|OPC UA Standard|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
How to connect to an OPC UA server using C# .NET
Avatar
Hamza Abazid
New Member
Members
Forum Posts: 1
Member Since:
02/03/2024
sp_UserOfflineSmall Offline
1
02/03/2024 - 06:55
sp_Permalink sp_Print

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.

 

    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();
            }
        }
    }
Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online:
Guest(s) 17
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1347
Posts: 4571