您必须反序列化“msg->serialized_data”数据。如果您使用的是数据序列化的“cdr”格式,请查看以下代码。
// deserialization and conversion to ros message
my_pkg::msg::Msg msg;
auto ros_message = std::make_shared<rosbag2_introspection_message_t>();
ros_message->time_stamp = 0;
ros_message->message = nullptr;
ros_message->allocator = rcutils_get_default_allocator();
ros_message->message = &msg;
auto type_support = rosbag2::get_typesupport("my_pkg/msg/Msg", "rosidl_typesupport_cpp");
rosbag2::SerializationFormatConverterFactory factory;
std::unique_ptr<rosbag2::converter_interfaces::SerializationFormatDeserializer> cdr_deserializer_;
cdr_deserializer_ = factory.load_deserializer("cdr");
cdr_deserializer_->deserialize(msg, type_support, ros_message);
完整代码:https ://github.com/Kyungpyo-Kim/ROS2BagFileParsing