OPC Studio User's Guide and Reference
Getting Started with OPC Wizard under .NET Framework and VS Designer
Getting Started > Getting Started with OPC Wizard > Getting Started with OPC Wizard under .NET Framework or .NET > Getting Started with OPC Wizard under .NET Framework and VS Designer
In This Topic

In this Getting Started, we will create an OPC UA server running in C# or VB.NET. The server will be hosted in a Windows Forms application running in .NET Framework, and will provide a variable with dynamic random value.

We will show how the OPC Wizard allows you to develop an OPC server using Visual Studio designer. As a developer, you will be mostly just placing and configuring OPC Wizard components. The designer will generate the majority of the code for you.

The procedure described in this article currently only works in projects that are targeting .NET Framework (i.e. not in projects targeting .NET 6+).

Prerequisites

OPC UA Server in a Windows Forms Application

The principle described in this article will also work in console applications and library projects.
  1. Start the Visual Studio IDE (Integrated Development Environment).

  2. In this step, we will create a new .NET Windows Forms application project for .NET Framework.

    Select File -> New -> Project from the menu. The Create a new project dialog appears. In the language selection drop-down (initially set to All languages), select either C# or Visual Basic. Type Windows Forms into the search box. Select Windows Forms App (.NET Framework); do not select the one labeled just  Windows Form App.  Press the Next button. Go through the remaining steps of the wizard, making sure that you select a supported framework (see OPC Wizard Required .NET Runtimes).

  3. In this step, we will place the OPC Wizard component (EasyUAServer Class) on the form. Doing so will also add a reference to the OpcLabs.OpcWizard NuGet package.

    Switch to the Toolbox window and expand the OPC Wizard Components category. From it, drag the EasyUAServer component to the surface of Form1.

    When including OPC Wizard components for the first time in the project, dragging the toolbox item to the designer surface causes the necessary NuGet packages be installed and referenced. In such case, you might be asked to repeat the last toolbox operation, i.e. drag the item from the toolbox again.

  4. In this step, we will add a data variable called "DataVariable1" to the server.

    In the Properties window, make sure that the selected object is "easyUAServer1" (or "EasyUAServer1", in VB.NET). Then, select the Objects -> Nodes row (under the Contents category), and pres the ... button on the right side of the row. A Node Collection Editor window appears, with an empty list of members. Press the Add button. A "DataVariable1" members appears in the list on the left side, with its various properties on the right side of the window.

  5. In this step, we will define how a Read from the data variable should be done.

    Switch to the Events tab of the property grid on the right side of the window. This is done by pressing the button with yellow "lightning" icon on the toolbar above the property grid. Then, double click in the Read row under the Runtime category (the Read row is already preselected). You will see that next to the name of the Read event, an event handler named uaDataVariable1_Read (or UaDataVariable1_Read, in BN.NET) appears. This means that the Visual Studio has created an empty event handler for this event. Press the OK button to close the Node Collection Editor dialog.

    In the Form1.cs (or Form1.vb) window inside the Form1 class, add the following field declaration (on the class level):

    Random _random = new Random();
    
    Dim _random As New Random 
    

    Also in the same window and class, locate the created event handler (named uaDataVariable1_Read), place the cursor inside its body, and fill in the following code:

    e.HandleAndReturn(_random.Next());
    
    e.HandleAndReturn(_random.Next)
    
  6. In this step, we will make the OPC server created with the OPC Wizard start when the main form loads.

    For this, switch to the Form1.cs [Design] (or Form1.vb [Design]) window, i.e. the window with the visual designer for the form, and click on the form surface. This will select the form itself for viewing and editing inside the Properties window. Verify that the "Form1" is the selected object at the top of the Properties window. Then, switch to the Events tab of the property grid. This is done by pressing the button with yellow "lightning" icon on the toolbar above the property grid. Double-click in the Load row under the Behavior category (the Load row will already be preselected). Visual Studio will create an empty event handler for the Load event on the form, and take you directly to the place in the code where you can put the event handler body. Fill in the following code:

    easyUAServer1.Start();
    
    EasyUAServer1.Start()
    
  7. Select Run -> Start Debugging (F5) from the menu, or press the corresponding button on the toolbar.   

    This will build and launch the program. The server is now running. A firewall pop-up window may appear, asking you for consent with allowing the communication. Provide the consent as needed; allowing the communication on private networks is sufficient.

    OPC UA clients can connect to the server's endpoint on "opc.tcp://localhost:48040/" . You can verify it using any OPC UA client; see further below for instructions.

    After you are done, exit the program using its "Close" icon, or by pressing Alt+F4.

Verification

When the OPC UA server is running, any OPC UA client can connect to it and read or subscribe to the value of the variable we have defined in the server. When the OPC UA client is on the same computer, it can connect to "opc.tcp://localhost:48040/" to access this server; otherwise, replace the "localhost" in the endpoint URL by the name of the computer (host) on your network.

If the OPC UA Local Discovery Server (LDS) is installed on the computer where the server is running, the server will automatically register itself with the LDS and will thus become discoverable by OPC UA clients through the LDS. For this to work, the LDS must either accept unauthenticated registrations, or be configured to trust the server. For more information, see OPC UA LDS Integration. The use of the LDS is optional.

If the OPC UA clients allows you to browse the address space of the server, the variable that we have defined is located directly under the Objects folder, and is named DataVariable1. Its node Id is:

Verification with the Connectivity Explorer

If you have the Connectivity Explorer application (e.g. if you have installed OPC Wizard using the OPC Studio Setup program, or obtained the Connectivity Explorer separately e.g. through ClickOnce deployment), you can use it for verification of the created OPC UA server as follows:

  1. Start the Connectivity Explorer application.
  2. In the Point Editor window, select Points (Composite) -> OPC Unified Architecture (Client-Server) Points -> Well-known -> opc.tcp://localhost:48040/ .
  3. Expand the selected node by clicking on the "+" mini-icon next to it, or by pressing + on the keyboard.
  4. Select the DataVariable1 node in the list view (middle pane) of the Point Editor window.
  5. Double-click on this DataVariable1 node, or select the "Add Live Data Row" in the Actions pane, or press Enter on the keyboard.
  6. Observe the variable value in the Live Point Data window.

Verification with the OpcCmd Utility

If you have the OpcCmd utility (e.g. if you have installed OPC Wizard using the OPC Studio Setup program, or obtained the OpcCmd separately e.g. through ClickOnce deployment), you can use it for verification of the created OPC UA server as follows:

  1. Start the OpcCmd utility.
  2. Enter the following command:
     
            uaClient subscribe opc.tcp://localhost:48040/ nsu=http://opclabs.com/OpcUA/Custom/Objects;s=DataVariable1
    
    
  3. Observe the variable value changes in the console output. The command will terminate automatically after 1 minute, or you can stop it by pressing X on the keyboard.

Security Notice

For simplicity in configuration, the OPC UA server created in this Getting Started exposes an insecure endpoint, and allows OPC UA connections without application authentication. Any production OPC UA server should, at least by default, disable the insecure endpoint. For more information, see Securing OPC Wizard Servers.

See Also

Fundamentals

Reference