7.1.1. String: Hello World
After you have learned a lot about the pre-compiled applications that come with eCAL, let’s create our own! In the good habit of every tutorial, we will write a Hello World Application, that sends the string “Hello World” to an eCAL topic. The second part will be all about the subscriber, which will receive the message and print it to the console.
You will see how it is done in the major programming languages we support: C++, C, C# and Python.
For CMake usage tips, please refer to the CMake section of this documentation.
7.1.1.1. Hello World Publisher
Let’s begin with the publisher side of our “Hello World” application.
The base initialization of the eCAL publisher is the same in all languages:
Before you do anything else, you need to initialize eCAL with
Initialize(..).Then you create the publisher and send a message in the frequency you want. In our example we will send the message every 500 ms in an infinite loop. You can add a stop condition to the loop, if you want to send just a limited amount of messages.
After you are done with publishing data and you don’t need eCAL anymore, you can call the
Finalize()function to clean up the resources and unregister the process.
For simplicity, we will use the same message type in all languages. As this is simple string message, we will use the eCAL string publisher to send.
7.1.1.2. Hello World Publisher Files
|fa-folder-open| Publisher samples ├─ |fa-folder-open| C++ │ └─ |fa-file-alt|hello_send.cpp│ ├─ |fa-folder-open| C │ └─ |fa-file-alt|hello_send.c│ ├─ |fa-folder-open| C# │ └─ |fa-file-alt|hello_send_csharp.cs│ └─ |fa-folder-open| Python └─ |fa-file-alt|hello_send.py
7.1.1.3. Hello World Subscriber
Now let’s have a look at the subscriber side. Basically, the initialization is the same as for the publisher. The only difference is that you need to assign a callback function to the subscriber:
Call
Initialize()to initialize eCAL.Create the subscriber.
Assign a callback function to the subscriber with
SetReceiveCallback.Do something to keep the process alive. In our example we will use a simple infinite loop. Process the incoming messages as you wish.
After you are done with receiving data and you don’t need eCAL anymore, you can call the
Finalize()function to clean up the resources and unregister the process.
7.1.1.4. Hello World Subscriber Files
|fa-folder-open| Subscriber samples ├─ |fa-folder-open| C++ │ └─ |fa-file-alt|hello_receive.cpp│ ├─ |fa-folder-open| C │ └─ |fa-file-alt|hello_receive.c│ ├─ |fa-folder-open| C# │ └─ |fa-file-alt|hello_receive_csharp.cs│ └─ |fa-folder-open| Python └─ |fa-file-alt|hello_receive.py