Python3 ros msg serialization

asked 2021-07-28 15:31:32 -0500

emielke gravatar image

I recently decided to move from python2/melodic up to python3/noetic. However, I am running into issues running my swig wrapped c++ code. Essentially I have to_cpp and from_cpp functions in python, and corresponding ones in c++ (from_python and to_python).

Here are the python ones:

def _to_cpp(self, msg):
    buff = io.BytesIO()
    return buff.getvalue()

def _from_cpp(self, str_msg, msg_type):
    msg = msg_type()
    return msg.deserialize(str_msg)

And the c++:

template <typename M>
M from_python(const std::string str_msg)
  size_t serial_size = str_msg.size();
  boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
  for (size_t i=0; i<serial_size; ++i)
    buffer[i] = str_msg[i];
  ros::serialization::IStream stream(buffer.get(), serial_size);
  M msg;
  ros::serialization::Serializer<M>::read(stream, msg);
  return msg;

template <typename M>
std::string to_python(const M& msg)

  size_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);
  std::string str_msg;
  for (size_t i=0; i<serial_size; ++i)
  return str_msg;

An example of the python usage is below:

output = self._from_cpp(self.wrappedCPPFunction(self._to_cpp(custom_ros_msg)))

And an example of the c++ function that is wrapped is below (note, this function is overloaded):

std::string ExampleClass::CPPFunction(const std::string& input_msg) const
  custom_msgs::CustomMsg custom_msg = from_python<custom_msgs::CustomMsg>(input_msg);
  trajectory_msgs::JointTrajectory joint_trajectory;      
  ... // other function calls to determine joint trajectory
  return to_python(joint_trajectory) 

At runtime, I get an error when passing the custom_ros_msg through the wrappedCPPFunction: xception in your execute callback: Wrong number or type of arguments for overloaded function 'CPPFunction'

I suspect it doesn't like the change from the pythonic2 StringIO to the pythonic3 BytesIO, but I've tried messing with that, to no avail. Any ideas?

edit retag flag offensive close merge delete