ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Receiving Data over UDP through node

asked 2017-04-18 23:13:47 -0600

Masoud gravatar image

updated 2017-04-18 23:15:27 -0600

Hello everybody

I am using a custom CAN to LAN converter board and switch to connect 14 EPOS2 driver to my PC through LAN port in order to read some encoders data. These hardware is sending data using UDP protocol. Now I need to write a ROS node to communicate through UDP protocol to read these encoders outputs. I'm kind of new in C++ and ROS programming. Would you please help me or provide some documentation or tutorials to solve this problem.

I also found this package:

but it seems that it is communicating over TCP protocol while I need UDP. Is it possible to change it from TCP to UDP?

This question is similar to mine, but it was not much helpful for me:


edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2017-04-19 07:04:29 -0600

Karl Damkjær Hansen gravatar image

I would use the Boost ASIO library.

Example from the documentation on making a simple UDP daytime client:

// client.cpp
// ~~~~~~~~~~
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::udp;

int main(int argc, char* argv[])
    if (argc != 2)
      std::cerr << "Usage: client <host>" << std::endl;
      return 1;

    boost::asio::io_service io_service;

    udp::resolver resolver(io_service);
    udp::resolver::query query(udp::v4(), argv[1], "daytime");
    udp::endpoint receiver_endpoint = *resolver.resolve(query);

    udp::socket socket(io_service);;

    boost::array<char, 1> send_buf  = {{ 0 }};
    socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);

    boost::array<char, 128> recv_buf;
    udp::endpoint sender_endpoint;
    size_t len = socket.receive_from(
        boost::asio::buffer(recv_buf), sender_endpoint);

    std::cout.write(, len);
  catch (std::exception& e)
    std::cerr << e.what() << std::endl;

  return 0;
edit flag offensive delete link more

answered 2017-04-22 02:37:49 -0600

Masoud gravatar image

I searched the net for similar programs. Here I found a package named comm_tcp that seems similar to the one I need. But unfortunately It is using TCP/IP protocol while I need to communicate through UDP. I would be grateful if anybody could help me to make required changes to these two source files, so it uses UDP instead of TCP.


edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-04-18 23:13:47 -0600

Seen: 4,580 times

Last updated: Apr 22 '17