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.
Simple port example
Here we work through an example of using YARP communication between programs. We will have two programs, a sender and receiver, simple_sender
and simple_receiver
simple_sender
Here's simple_sender/simple_sender.cpp
:
19constexpr double loop_delay = 1.0;
20constexpr size_t top = 100;
22int main(
int argc,
char* argv[])
29 output.open(
"/sender");
30 for (
size_t i = 1; i <= top; i++) {
33 bot.addString(
"testing");
39 printf(
"Sent message: %s\n", bot.toString().c_str());
A simple collection of objects that can be described and transmitted in a portable way.
Utilities for manipulating the YARP network, including initialization and shutdown.
A mini-server for network communication.
int main(int argc, char *argv[])
void delay(double seconds)
Wait for a certain number of seconds.
The main, catch-all namespace for YARP.
simple_sender/CMakeLists.txt
5if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
6 cmake_minimum_required(VERSION 3.19)
8 find_package(YARP REQUIRED COMPONENTS os)
11add_executable(simple_sender)
12target_sources(simple_sender PRIVATE simple_sender.cpp)
13target_link_libraries(simple_sender
simple_receiver
Here's simple_receiver/simple_receiver.cpp
:
18int main(
int argc,
char* argv[])
26 input.open(
"/receiver");
28 Network::connect(
"/sender",
"/receiver");
30 printf(
"Got message: %s\n", bot.toString().c_str());
simple_receiver/CMakeLists.txt
5if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
6 cmake_minimum_required(VERSION 3.19)
7 project(simple_receiver)
8 find_package(YARP REQUIRED COMPONENTS os)
11add_executable(simple_receiver)
12target_sources(simple_receiver PRIVATE simple_receiver.cpp)
13target_link_libraries(simple_receiver
Running the example
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.
Buffered port example
18int main(
int argc,
char* argv[])
28 Network::setLocalMode(
true);
33 BufferedPort<Bottle> in;
34 BufferedPort<Bottle> out;
45 Network::connect(
"/out",
"/in");
51 Bottle& outBot1 = out.prepare();
52 outBot1.fromString(
"hello world");
53 printf(
"Writing bottle 1 (%s)\n", outBot1.toString().c_str());
57 Bottle& outBot2 = out.prepare();
58 outBot2.fromString(
"2 3 5 7 11");
59 printf(
"Writing bottle 2 (%s)\n", outBot2.toString().c_str());
66 Bottle* inBot1 = in.read();
67 printf(
"Bottle 1 is: %s\n", inBot1->toString().c_str());
70 Bottle* inBot2 = in.read();
71 printf(
"Bottle 2 is: %s\n", inBot2->toString().c_str());
A mini-server for performing network communication in the background.
5if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
6 cmake_minimum_required(VERSION 3.19)
8 find_package(YARP REQUIRED COMPONENTS os)
11add_executable(buffered_port)
12target_sources(buffered_port PRIVATE buffered_port.cpp)
13target_link_libraries(buffered_port