Receiving Data over UDP through node
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:
https://github.com/abhinavjain241/comm_tcp
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:
http://answers.ros.org/question/207677/sending-data-over-udp-through-node/
Thanks
Asked by Masoud on 2017-04-18 23:13:47 UTC
Answers
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 http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
int main(int argc, char* argv[])
{
try
{
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);
socket.open(udp::v4());
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(recv_buf.data(), len);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
Asked by Karl Damkjær Hansen on 2017-04-19 07:04:29 UTC
Comments
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.
Thanks
Asked by Masoud on 2017-04-22 02:37:49 UTC
Comments