Friday, September 26, 2025

Building Qt OPC UA wrapper for open62541 on Windows 10 using MinGW

Building Qt OPC UA wrapper for open62541 on Windows 10 using MinGW

Base Requirements(tested on):
OS: Windows 10
Qt: Qt Creator 17.0.1/Qt 6.7.3 (MinGW 11.2.0 64-bit)
Python: Python 3.13 (needed for compiling open62541)

Note:
As of 9/26/2025, Qt 6.9.2 (MinGW 13.1.0 64-bit) is not able to build open62541


Set environment variables, open cmd.exe
call C:\Qt\6.7.3\mingw_64\bin\qtenv2.bat
set path=C:\Qt\Tools\CMake_64\bin;%path%
Build and install open62541
mkdir c:\g
cd c:\g
git clone https://github.com/open62541/open62541.git
cd open62541
git checkout v1.4.13
mkdir build && cd build
cmake .. -G "MinGW Makefiles" -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX="C:/open62541-install"

mingw32-make
mingw32-make install
Build Qt OpcUA
cd c:\g
git clone https://code.qt.io/qt/qtopcua.git
cd qtopcua
git checkout v6.7.3
mkdir build
cd build

qt-cmake .. ^
-G "MinGW Makefiles" ^
-DINPUT_open62541=system ^
-DCMAKE_PREFIX_PATH="C:/open62541-install" ^
-DCMAKE_INSTALL_PREFIX="C:/Qt/6.7.3/mingw_64"

mingw32-make
mingw32-make install

Thursday, September 11, 2025

Simple OPC client subscribing to an item using OPC .NET API

Simple OPC client subscribing to an item using OPC .NET API

This is a simple OPC Classic client using OPC .NET API subscribing to an item.

Step 1.

Create a .NET console application as outlined in Sample OPC client using OPC .Net API

Step 2. Replace C# console code as shown below

Notes:
Replace Hostname with the machine name of the OPC server
Replace "TEST1/SGGN1/OUT.CV" with the item to subscribe.
using System;
using Opc;
using Opc.Da;
using OpcCom;

namespace oClientNet
{
    class Program
    {
        private const string Hostname = "M6"; // machine name or IP
        private const string OpcItemId = "TEST1/SGGN1/OUT.CV";
        private const int UpdateRateMs = 1000;

        static void Main(string[] args)
        {
            try
            {
                RunOpcClient();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex}");
            }

            Console.WriteLine("This should close the connection to the server, key to continue");
            Console.ReadKey(true);
        }

        private static void RunOpcClient()
        {
            string urlString = $"opcda://{Hostname}/OPC.DeltaV.1";
            Opc.URL url = new Opc.URL(urlString);

            using (Opc.Da.Server server = new Opc.Da.Server(new OpcCom.Factory(), url))
            {
                server.Connect(new Opc.ConnectData(null, null));
                Console.WriteLine("Connected to OPC server.");

                // Create subscription state
                SubscriptionState state = new SubscriptionState
                {
                    Name = "MySubscription",
                    UpdateRate = UpdateRateMs,
                    Active = true
                };

                using (Subscription subscription = (Subscription)server.CreateSubscription(state))
                {
                    Item opc_item = new Item { ItemName = OpcItemId };
                    Item[] items = new Item[] { opc_item };
                    ItemResult[] results = subscription.AddItems(items);

                    subscription.DataChanged += OnDataChanged;

                    Console.WriteLine("Subscription created. Press Esc key to exit.");

                    while (Console.ReadKey(true).Key != ConsoleKey.Escape)
                    {
                    }
                }
                server.Disconnect();
            }
        }

        private static void OnDataChanged(object subscriptionHandle, object requestHandle, ItemValueResult[] values)
        {
            foreach (ItemValueResult itemValue in values)
            {
                if (itemValue.Quality == Quality.Good)
                {
                    Console.WriteLine($"{itemValue.ItemName}:{itemValue.Value}");
                }
                else
                {
                    Console.WriteLine($"Error for {itemValue.ItemName}: Quality={itemValue.Quality}");
                }
            }
        }
    }
}

Building Qt OPC UA wrapper for open62541 on Windows 10 using MinGW

Building Qt OPC UA wrapper for open62541 on Windows 10 using MinGW Base Requirements(tested on): OS: Windows 10 Qt: Qt Creator 17.0.1/Qt ...