1

Rosanswers logo

Hi all. I want to build my own ROS-like msg transferring through zmq (for practice). My problem is that I need to create and pub a msg object in python and transfer it to a sub receiving and desalinize it in c++.

My problem is that I can't manage to serialize it in the python side. I've tries to create a message object and use the serialize function but the zmq's send function doesn't seem to pass it.

Code example:

import zmq from std_msgs.msg 
import String from StringIO 
import StringIO import rospy
context = zmq.Context() 
socket = context.socket(1) 
context = zmq.Context() 
socket = context.socket(1)

socket.bind("tcp://localhost:5217") while True: command = raw_input("insert command ") if command == 'c': s = String("Hello World".encode('utf-8')) s1 = StringIO() s.serialize(s1) socket.send_multipart([s1]) print "sent: " + data.str()

When I'm using regular int msg it is working, but when I'm trying to use the ROS serialize function I get error message:

  File "/usr/local/lib/python2.7/dist-packages/zmq/sugar/socket.py", line 434, in send_multipart
    i, rmsg,
TypeError: Frame 0 (<StringIO.StringIO instance at 0...) does not support the buffer interface.

Originally posted by dforer on ROS Answers with karma: 26 on 2018-09-12

Post score: 1


Original comments

Comment by gvdhoorn on 2018-09-12:
You're going to have to provide a little more information: are you trying to use the ROS message classes and (de)serialisation infrastructure, but not the ROS middleware parts? So you want to serialise a msg to a byte buffer and then use ZMQ as your transport?

Comment by gvdhoorn on 2018-09-12:
Also:

but the zmq's send function doesn't seem to pass it.

This is not enough information to help you. Please be specific as to what you've tried, provide some code samples, etc.

Even then I'm wondering whether this question is on-topic for ROS Answers: if you could clarify a bit?

Comment by dforer on 2018-09-12:
Yes exactly. I want to take a python ros msg object, serialize it, pass it with zmq and deseirlize it in c++, like ros messaging just not using the ZMQ as my transport.

Comment by dforer on 2018-09-12:
This is a code example:

    s = String("Hello World".encode('utf-8'))
    s1 = StringIO()
    s.serialize(s1)
    socket.send(s1)

The String object is std_msgs string- ros msg.

Comment by gvdhoorn on 2018-09-12:
Please edit your original question and expand it a little to make this more clear.

Comment by gvdhoorn on 2018-09-12:\

File "/usr/local/lib/python2.7/dist-packages/zmq/sugar/socket.py", line 434, in send_multipart

The error seems to be in the ZMQ and your code: this just says that the send_multipart(..) ZMQ function is trying to use functionality from s1 that it doesn't support.

Comment by dforer on 2018-09-12:
Thanks, I've solved it.

Comment by gvdhoorn on 2018-09-12:
It would be nice if you could let us know how you solved it.

That way future readers can also benefit from your question.

Please post an answer below and then accept your own answer.

2 Answers2

1

Rosanswers logo

For Python 3:

from io import BytesIO

def serialize_msg(msg): buff = BytesIO() msg.serialize(buff) return buff.getvalue()

Traced it using ROS1 Melodic's implementation here:

https://github.com/ros/ros_comm/blob/fad51345f1937a8fca036b264a4c6fb034970a5f/clients/rospy/src/rospy/topics.py#L906


Originally posted by EricCousineau-TRI with karma: 11 on 2020-04-28

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by emielke on 2021-07-27:
The python2 solution worked for me, but not the python3. On the c++ side, it says the type is not a string, like it was expecting with python2. How do I get it to appear like a string as in python2?

Lucas Walter
  • 3,337
  • 1
  • 14
  • 19
0

Rosanswers logo

The answer is Very simple, the field that you need to send is s1.buflist (from the example above):

    s = String("Hello World".encode('utf-8'))
    s1 = StringIO()
    s.serialize(s1)
    socket.send_multipart(s1.buflist)
    print "sent: " + data.__str__()

Originally posted by dforer with karma: 26 on 2018-10-04

This answer was ACCEPTED on the original site

Post score: 0