Ask Your Question

How to send 'hexadecimal' message packets over tcp/ip network?

asked 2017-04-07 01:58:11 -0500

updated 2017-04-12 00:23:57 -0500


I am using ros indigo and ubuntu 14.04. I have a sensor that is connected through tcp/ip network. Its a querry response type of device. So basically I need to send meaningful data packets to get back meaningful data packets that would indicate various status.

So I have created a rosnode, and published a write topic that will be publishing my message into the respective tcp address and port. However the msg needs to be in Hexadecimal.

Sample data packet:

00 84 00 00 00 06 54 03 00 01 00 04

How can I send such a packet in a meaningful? Could it be sent over as a string? I think not. Any assistance will be greatly appreciated.

edit retag flag offensive close merge delete



I don't know anything about your E84 sensor, but in general it sounds like you're on the right path. You'll probably need to write the sockets code for communicating with the E84 yourself, and then publish ROS messages representing your sensor data.

ahendrix gravatar image ahendrix  ( 2017-04-10 11:51:52 -0500 )edit

thanks @ahendrix

arunavanag gravatar image arunavanag  ( 2017-04-10 22:19:41 -0500 )edit

@ahendrix I have edited my question, you might have an answer. its more generic now.

arunavanag gravatar image arunavanag  ( 2017-04-12 00:25:10 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2017-04-12 02:28:29 -0500

ahendrix gravatar image

What you're asking isn't really ROS-related. Go read a systems programming textbook or find a sockets tutorial online if you want more details.

Use the socket() and connect() system calls to set up your socket. Once you have a file descriptor, you can use the write() system call to send data on that socket. Data is data; it doesn't matter if you represent it as decimal, binary, hex, or ascii; to the computer it's all just data.

A very short snippet to construct a buffer and write it to a socket is:

uint8_t buffer[] = { 0x00, 0x84, 0x00, 0x00, 0x00, 0x06, 0x54, 0x03, 0x00, 0x01, 0x00, 0x04 };
int err = write(socket_file_descriptor, buffer, 12);

and don't forget to check the return code! The return code from write will tell you if you've lost the network connection to your device, and a variety of other useful things that will help you debug future issues.

edit flag offensive delete link more


I am already using socket and I have created TCP ros wrapper over that. I need to send the right packet. Right now I am sending in a string which is returning me illegal data value from my device which means I have established a connection. However, I need to publish the correct packet as a ros msg.

arunavanag gravatar image arunavanag  ( 2017-04-12 04:04:50 -0500 )edit

The ros msg needs to go into my subscribed topic. @ahendrix

arunavanag gravatar image arunavanag  ( 2017-04-12 04:05:21 -0500 )edit

You probably shouldn't be writing a ROS node that translates ROS messages directly into TCP; it's usually much better to keep the packet formatting logic within your code, and publish ROS messages that actually represent the received sensor data.

ahendrix gravatar image ahendrix  ( 2017-04-12 16:09:14 -0500 )edit

Ok, presently I have successfully done a query and received a response message. However, I am sending a set of decimals in a char array and converting it into a string and sending out and sniffing the network and verifying the messages in hex. Need to get it more readable and do some conversions.

arunavanag gravatar image arunavanag  ( 2017-04-13 02:02:35 -0500 )edit

@ahendrix Do you know how to send this { 0x00, 0x84, 0x00, 0x00, 0x00, 0x06, 0x54, 0x03, 0x00, 0x01, 0x00, 0x04 } as a ros msg which is of type const std_msgs::StringConstPtr

arunavanag gravatar image arunavanag  ( 2017-04-19 04:26:27 -0500 )edit

Why not convert it into a more meaningful form first?

ahendrix gravatar image ahendrix  ( 2017-04-19 04:52:27 -0500 )edit

@ahendrix like what? That is the packet I need to send.

arunavanag gravatar image arunavanag  ( 2017-04-19 04:59:17 -0500 )edit

Presumably this packet represents some action that you want the sensor to take, or or some sensor reading. You should convert it to a request message or a sensor reading message. Otherwise you're just building a leaky abstraction and adding latency.

ahendrix gravatar image ahendrix  ( 2017-04-19 11:24:19 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-04-07 01:58:11 -0500

Seen: 2,987 times

Last updated: Apr 12 '17