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
:
22 constexpr
double loop_delay = 1.0;
23 constexpr
size_t top = 100;
25 int main(
int argc,
char* argv[])
32 output.open(
"/sender");
33 for (
size_t i = 1; i <= top; i++) {
42 printf(
"Sent message: %s\n", bot.toString().c_str());
simple_sender/CMakeLists.txt
8 if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
9 cmake_minimum_required(VERSION 3.12)
10 project(simple_sender)
11 find_package(YARP REQUIRED COMPONENTS os)
14 add_executable(simple_sender)
15 target_sources(simple_sender PRIVATE simple_sender.cpp)
16 target_link_libraries(simple_sender PRIVATE YARP::YARP_os
simple_receiver
Here's simple_receiver/simple_receiver.cpp
:
21 int main(
int argc,
char* argv[])
29 input.open(
"/receiver");
31 Network::connect(
"/sender",
"/receiver");
33 printf(
"Got message: %s\n", bot.toString().c_str());
simple_receiver/CMakeLists.txt
8 if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
9 cmake_minimum_required(VERSION 3.12)
10 project(simple_receiver)
11 find_package(YARP REQUIRED COMPONENTS os)
14 add_executable(simple_receiver)
15 target_sources(simple_receiver PRIVATE simple_receiver.cpp)
16 target_link_libraries(simple_receiver PRIVATE YARP::YARP_os
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
21 int main(
int argc,
char* argv[])
31 Network::setLocalMode(
true);
36 BufferedPort<Bottle> in;
37 BufferedPort<Bottle> out;
48 Network::connect(
"/out",
"/in");
54 Bottle& outBot1 = out.prepare();
55 outBot1.fromString(
"hello world");
56 printf(
"Writing bottle 1 (%s)\n", outBot1.toString().c_str());
60 Bottle& outBot2 = out.prepare();
61 outBot2.fromString(
"2 3 5 7 11");
62 printf(
"Writing bottle 2 (%s)\n", outBot2.toString().c_str());
69 Bottle* inBot1 = in.read();
70 printf(
"Bottle 1 is: %s\n", inBot1->toString().c_str());
73 Bottle* inBot2 = in.read();
74 printf(
"Bottle 2 is: %s\n", inBot2->toString().c_str());
8 if(NOT DEFINED CMAKE_MINIMUM_REQUIRED_VERSION)
9 cmake_minimum_required(VERSION 3.12)
10 project(buffered_port)
11 find_package(YARP REQUIRED COMPONENTS os)
14 add_executable(buffered_port)
15 target_sources(buffered_port PRIVATE buffered_port.cpp)
16 target_link_libraries(buffered_port PRIVATE YARP::YARP_os