UA Server SDK C++ Bundle
1.3.3.206
|
This lesson will guide you through the process of setting up a basic OPC UA Server console application.
Content:
The lesson uses servermain.cpp from examples.
In addition the following utility files are used from examples/utilities:
Linux specific: Signal handlers
On Linux you SHOULD install a signal handler to handle SIGPIPE. SIGPIPE is sent when writing on a closed socket which can happen when a connection is broken. The default handler terminates the application which is the desired behaviour when using pipes for inter-process communication, but not for networking applications like OPC UA. You can ignore this signal by calling signal(SIGPIPE, SIG_IGN)
or by installing a signal handler.
You should also handle the signals SIGINT and SIGTERM like shown in the following example. SIGINT is sent when a user wants to interrupt a process by pressing CTRL-C on controlling terminal. SIGTERM is sent when a process is requested to terminate. SIGTERM is the default signal sent to a process by the kill
or killall
command. Unlike the SIGKILL signal, it can be caught by an application so that it can properly terminate. SIGTERM is also sent by the init
process during system shutdown, it then waits a few seconds and then sends SIGKILL to all remaining processes to forcibly terminate them.
For more information on signals please see man
signal(2)
, man
sigaction(2)
, or the book UNIX Network Programming from Richard Stevens, Addison Wesley.
In our examples we setup signal handlers in the utility function RegisterSignalHandler()
in shutdown.cpp. You can either use this utility function or set up your own signal handlers like shown in the following example.
Set up a console application.
Windows:
Create a new project. Use the following settings:
Add the files listed below to your application:
The example is using a console application.
The following code provides a generic main function without OPC UA code that extracts the application path from the input arguments and calls the function OpcServerMain where we will add the OPC UA specific code. The function in the following code contains only the waiting loop for a user shutdown command.
The UA Stack and the XML Parser modules are ANSI C based modules and do require a global initialization before any of their functionality is used.
The initialization of the XML Parser is only needed if the XML configuration file is used through the class ServerConfigXml.
Add the marked line and replace the code in OpcServerMain() as shown below.
If the UaPlatformLayer::init call succeeds, the UA SDK and UA Stack classes and functions can be used.
The following code is used to initialize and start the server object. OPC UA clients can connect to the server after this code is executed.
The following code is used to wait for a user shut down command and to shut down the OPC UA server.
Add the following include paths to your application:
[SDK Installation Directory]\examples\utilities
[SDK Installation Directory]\include\uastack
[SDK Installation Directory]\include\uabase
[SDK Installation Directory]\include\uapki
[SDK Installation Directory]\include\xmlparser
[SDK Installation Directory]\include\uaserver
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\openssl\inc32
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\libxml2\include
The following include path is only needed for this example:
[SDK Installation Directory]/examples/simulation_buildingautiomation
For more information see LibraryOverview.
Windows:
For Additional Library Directories enter the following values:
[SDK Installation Directory]\lib
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\openssl\out32dll.dbg (Debug)
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\libxml2\out32dll.dbg (Debug)
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\openssl\out32dll (Release)
[SDK Installation Directory]\third-party\win32\[VisualStudioVersion]\libxml2\out32dll (Release)
For Additional Dependencies (Debug) enter:
uastackd.lib
uabased.lib
uapkid.lib
uamoduled.lib
coremoduled.lib
xmlparserd.lib
libeay32d.lib
libxml2d.lib
ws2_32.lib
rpcrt4.lib
crypt32.lib
For Additional Dependencies (Release) enter:
uastack.lib
uabase.lib
uapki.lib
uamodule.lib
coremodule.lib
xmlparser.lib
libeay32.lib
libxml2.lib
ws2_32.lib
rpcrt4.lib
crypt32.lib
Linux:
For Additional Library Directories enter the following values:
-L[SDK Installation Directory]/lib
For Additional Dependencies (Debug) enter:
-luastackd -luabased -luapkid -luamoduled -lcoremoduled -lxmlparserd -lssl -lxml2
For Additional Dependencies (Release) enter:
-luastack -luabase -luapki -luamodule -lcoremodule -lxmlparser -lssl -lxml2
For more information see LibraryOverview.
Add
OPCUA_SUPPORT_SECURITYPOLICY_BASIC128RSA15=1
OPCUA_SUPPORT_SECURITYPOLICY_BASIC256=1
OPCUA_SUPPORT_SECURITYPOLICY_NONE=1
OPCUA_SUPPORT_PKI=1
LIBXML_STATIC
SUPPORT_XML_CONFIG
_UA_STACK_USE_DLL
Additional defines for Windows:
UNICODE
_UNICODE
_CRT_SECURE_NO_WARNINGS
_CRT_SECURE_NO_DEPRECATE
Set the output path to “bin” where the config file “ServerConfig.xml” can be found.
Windows:
Enter these values:
Compile and run the server application.
Try to connect to the server with an OPC UA Client, e.g. UaExpert. Figure 1-1 shows how to add the server. For more information, refer to the documentation of UaExpert.
Figure 1-1 Add server to UaExpert
Edit the server properties as shown in Figure 1-2.
Figure 1-2 Edit server properties.