[ros-users] Message construction with memcpy

Josh Faust jfaust at willowgarage.com
Fri Jul 9 00:22:03 UTC 2010

Using memcpy is not possible right now due to the Message base class (which
is going away in the future).  Even once that's gone though it's unlikely to
work for most messages.  For example:

struct A
  char a;
  int b;

sizeof(A) == 8 (not 5), and offsetof(A, b) == 4 (not 1) on x64, but may be
different on different architectures.

If the data is in the ROS serialization format (see the table under built-in
types at http://www.ros.org/wiki/msg#Message_Description_Specification), you
may be able to use the ROS serialization code (

Unless you're doing this for a large number of devices/messages, it's
probably easiest (and least brittle) to read the individual values manually
into the message.


On Thu, Jul 8, 2010 at 4:21 PM, Bill Morris <morris at ee.ccny.cuny.edu> wrote:

> I am wondering if there is a suggested method for constructing a message
> directly from binary data stored in memory.
> Right now I read in data from a serial port into a buffer and once I
> have a complete chunk of data I memcpy it into a struct. I then copy
> each item in the struct into the message, item by item. I would like to
> avoid copying each item individually by name.
> Are the following structs and messages laid out in memory the same way?
> Are there byte order or alignment issues I should be aware of if i
> attempt to create a message using memcpy?
> Do I want to deserialize or serialize the message? Is there
> documentation for this somewhere? Is there a better way to do this?
>    struct IMU
>    {
>      int angle_nick;
>      int angle_roll;
>      int angle_yaw;
>      short acc_x;
>      short acc_y;
>      short acc_z;
>    };
> IMU.msg
> int32 angle_nick
> int32 angle_roll
> int32 angle_yaw
> int16 acc_x
> int16 acc_y
> int16 acc_z
> _______________________________________________
> ros-users mailing list
> ros-users at code.ros.org
> https://code.ros.org/mailman/listinfo/ros-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ros.org/pipermail/ros-users/attachments/20100708/e2eda02d/attachment-0003.html>

More information about the ros-users mailing list