tutorial_server_firstSteps.rst 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 1. Building a simple server
  2. ===========================
  3. This series of tutorial guide you through your first steps with open62541. For
  4. compiling the examples, you need a compiler (MS Visual Studio 2015 or newer,
  5. GCC, Clang and MinGW32 are all known to be working). The compilation
  6. instructions are given for GCC but should be straightforward to adapt.
  7. It will also be very helpfull to install an OPC UA Client with a graphical
  8. frontend, such as UAExpert by Unified Automation. That will enable you to
  9. examine the information model of any OPC UA server.
  10. To get started, downdload the open62541 single-file release from
  11. http://open62541.org or generate it according to the :ref:`build instructions
  12. <building>` with the "amalgamation" option enabled. From now on, we assume you
  13. have the ``open62541.c/.h`` files in the current folder.
  14. Now create a new C source-file called ``myServer.c`` with the following content:
  15. .. code-block:: c
  16. #include <signal.h>
  17. #include "open62541.h"
  18. UA_Boolean running = true;
  19. void signalHandler(int sig) {
  20. running = false;
  21. }
  22. int main(void) {
  23. signal(SIGINT, signalHandler); /* catch ctrl-c */
  24. UA_ServerConfig config = UA_ServerConfig_standard;
  25. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  26. config.networkLayers = &nl;
  27. config.networkLayersSize = 1;
  28. UA_Server *server = UA_Server_new(config);
  29. UA_Server_run(server, &running);
  30. UA_Server_delete(server);
  31. nl.deleteMembers(&nl);
  32. return 0;
  33. }
  34. This is all that is needed for a simple OPC UA server. Compile the the server
  35. with GCC using the following command:
  36. .. code-block:: bash
  37. $ gcc -std=c99 open62541.c myServer.c -o myServer
  38. Now start the server (and stop with ctrl-c):
  39. .. code-block:: bash
  40. $ ./myServer
  41. You have now compiled and run your first OPC UA server. You can go ahead and
  42. browse the information model with UA Expert. The server will be listening on
  43. ``opc.tcp://localhost:16664`` - go ahead and give it a try.
  44. In the following tutorials, you will be shown how to populate the server's
  45. information model and how to create a client application.