This OPC application was created to provide the sample code for creating Subscriber applications using the OPC Foundation UA .NET Standard PubSub Library. There is a .NET Core 3.1 (2.1) console version of the Subscriber which runs on any OS supporting .NET Standard. The Reference Subscriber is configured to run in parallel with the Console Reference Publisher
- Open the solution UA Reference.sln with Visual Studio 2019.
- Choose the project
ConsoleReferenceSubscriber
in the Solution Explorer and set it with a right click asStartup Project
. - Hit
F5
to build and execute the sample.
This section describes how to run the ConsoleReferenceSubscriber.
Please follow instructions in this article to setup the dotnet command line environment for your platform.
- Open a command prompt.
- Navigate to the folder Applications/ConsoleReferenceSubscriber.
- To run the Subscriber sample type
dotnet run --project ConsoleReferenceSubscriber.csproj --framework netcoreapp3.1.
The Subscriber will start and listen for network messages sent by the Reference Publisher.
ConsoleReferenceSubscriber can be executed using the following command line arguments:
- -h|help - Shows usage information
- -m|mqtt_json - Creates a connection using there MQTT with Json encoding Profile. This is the default option.
- -u|udp_uadp - Creates a connection using there UDP with UADP encoding Profile.
To run the Subscriber sample using a connection with MQTT with Json encoding execute:
dotnet run --project ConsoleReferenceSubscriber.csproj --framework netcoreapp3.1
or
dotnet run --project ConsoleReferenceSubscriber.csproj --framework netcoreapp3.1 -m
To run the Subscriber sample using a connection with the UDP with UADP encoding execute:
dotnet run --project ConsoleReferenceSubscriber.csproj --framework netcoreapp3.1 -u
To create a new OPC UA Subscriber application:
- Open Microsoft Visual Studio 2019 environment,
- Create a new project and give it a name,
- Add a reference to the OPCFoundation.NetStandard.Opc.Ua.PubSub NuGet package,
- Initialize Subscriber application (see Subscriber Initialization).
The following four steps are required to implement a functional Subscriber:
-
Create Subscriber Configuration.
// Create configuration using UDP protocol and UADP Encoding PubSubConfigurationDataType pubSubConfiguration = CreateSubscriberConfiguration_UdpUadp();
Or use the alternative configuration object for MQTT with JSON encoding
// Create configuration using MQTT protocol and JSON Encoding PubSubConfigurationDataType pubSubConfiguration = CreateSubscriberConfiguration_MqttJson();
The CreateSubscriberConfiguration methods can be found in ConsoleReferenceSubscriber/Program.cs file.
-
Create an instance of the UaPubSubApplication Class using the configuration data from step 1.
// Subscribe to data events UaPubSubApplication uaPubSubApplication = UaPubSubApplication.Create(pubSubConfiguration);
-
Provide the event handler for the DataReceived event. This event will be raised when data sets matching the subscriber configuration arrive over the network. See the DataReceived Event section for more details.
// Create an instance of UaPubSubApplication uaPubSubApplication.DataReceived += PubSubApplication_DataReceived;
-
Start PubSub application
// Start the publisher uaPubSubApplication.Start();
After this step the Subscriber will listen for NetworkMessages as configured.
The Subscriber configuration is a subset of the PubSub Configuration. A functional Subscriber application needs to have a configuration (PubSubConfigurationDataType instance) that contains at least one connection (PubSubConnectionDataType instance) with at least one reader group configuration (ReaderGroupDataType instance). The reader group contains at least one data set reader (DataSetReaderDataType instance) that describes a published data set that can be processed and retrieved by the Subscriber application. The diagram shows the subset of classes involved in an OPC UA Publisher configuration.