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

How to deserialize a ros message without knowing its type or content

asked 2011-06-20 01:56:36 -0600

pparescasellas gravatar image

Hey there,

I'm trying to serialize any ros message received using ros::serialization and once this is done I need a way to deserialize it again but without knowing the message type. I'll try to explain how I intend to do this and I'd like to know if there's a better way to accomplish it.

I need to do this just to fill an xml with all the data contained in the message received like so:

<message_a>
  <x type="float64" value="25.2" />
  <y type="float64" value="23.2" />
  etc..
</message_a>

I'm just bothering with all the serialization and deserialization because I don't seem to find another way to accomplish that without knowing the message type (I need to make a generic function that fills an xml no matter what the message looks like).

First of all, I serialize the message this way:

const std::string& publisher_name = event.getPublisherName() ;
const cola2_common::BhResponse::ConstPtr& msg = event.getMessage() ; //BhResponse is a message I have defined.

ROS_INFO( "BhResponse received from: [%s]", publisher_name.c_str() ) ;

uint32_t serial_size = ros::serialization::serializationLength( *msg ) ;
boost::shared_array< uint8_t > buffer( new uint8_t[ serial_size ] ) ;

ros::serialization::OStream stream( buffer.get(), serial_size ) ;
ros::serialization::serialize( stream, *msg ) ;

Once I have the serialized stream I'd like to deserialize it again but without knowing the message type (so I can't use ros::serialization::deserialize). I have thought to do that asking for the definition of the message (so I get the type and name of all the contained data) and then keep rebuilding manually.

If I ask for the definition of the previous message I get this:

ROS_INFO( "BhResponse message Definition: %s", msg->__getMessageDefinition().c_str() ) ;

/**BhResponse message Definition:
float64[6] setPoints
float64[6] activationLevels
int32      priority */

I find the first problem here: __getMessageDefinition() is deprecated; how do I get this information without calling this method?

And finally, once I have the serialized message and its definition, I was going to keep building the xml step by step; first I'd add the name of the variable, then I'd add the type and after that, manually deserialize the stream to get its value, etc.

I'd appreciate any help I can get! Thanks!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-09-02 10:15:40 -0600

kwc gravatar image
edit flag offensive delete link more

Question Tools

Stats

Asked: 2011-06-20 01:56:36 -0600

Seen: 5,864 times

Last updated: Sep 02 '11