There are many code examples available in the "example/" subdirectory of YARP.
If you're reading this online, click on the examples link to see many of them.
Here we work through an example of using YARP communication between programs. We will have two programs, a sender and receiver, simple_sender.cpp and simple_receiver.cpp
Here's simple_sender.cpp:
Here's simple_receiver.cpp:
Here's an example CMakeLists.txt file to help with compiling if you use CMake (of course you can compile any way you like, you don't have to use CMake). Make sure you set the YARP_DIR environment variable to point to where you compiled YARP (or give the path through the CMake GUI).
cmake_minimum_required(VERSION 3.5)
find_package(YARP REQUIRED)
add_executable(simple_sender simple_sender.cpp)
target_link_libraries(simple_sender ${YARP_LIBRARIES})
add_executable(simple_receiver simple_receiver.cpp)
target_link_libraries(simple_receiver ${YARP_LIBRARIES})
After compiling, on three separate consoles, do:
yarp server # this starts the YARP name server ./simple_sender # this runs the sender ./simple_receiver # this runs the receiver
Or in windows that would be:
yarp.exe server # this starts the YARP name server simple_sender.exe # this runs the sender simple_receiver.exe # this runs the receiver
You'll need to be in the right directories to run the executables, or have them in your path.
Here's what you should see on the terminal for the sender:
Sent message: testing 1 of 100 Sent message: testing 2 of 100 Sent message: testing 3 of 100 Sent message: testing 4 of 100 ...
Here's what you should see on the terminal for the receiver:
yarp: Receiving input from /sender to /receiver using tcp Got message: testing 7 of 100 yarp: Removing input from /sender to /receiver
You can run the receiver many times to connect and reconnect.