Ask Your Question
1

Serialize ROS message and pass it

asked 2018-09-12 01:51:45 -0500

dforer gravatar image

updated 2018-09-12 02:16:38 -0500

gvdhoorn gravatar image

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.
edit retag flag offensive close merge delete

Comments

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?

gvdhoorn gravatar image gvdhoorn  ( 2018-09-12 01:56:26 -0500 )edit

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?

gvdhoorn gravatar image gvdhoorn  ( 2018-09-12 01:57:24 -0500 )edit

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.

dforer gravatar image dforer  ( 2018-09-12 01:59:33 -0500 )edit

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.

dforer gravatar image dforer  ( 2018-09-12 02:02:14 -0500 )edit
1

Please edit your original question and expand it a little to make this more clear.

gvdhoorn gravatar image gvdhoorn  ( 2018-09-12 02:04:13 -0500 )edit
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.

gvdhoorn gravatar image gvdhoorn  ( 2018-09-12 02:18:01 -0500 )edit

Thanks, I've solved it.

dforer gravatar image dforer  ( 2018-09-12 06:16:41 -0500 )edit
1

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.

gvdhoorn gravatar image gvdhoorn  ( 2018-09-12 07:06:27 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2018-10-04 06:04:37 -0500

dforer gravatar image

updated 2018-10-04 10:45:58 -0500

gvdhoorn gravatar image

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__()
edit flag offensive delete link more
1

answered 2020-04-28 10:40:10 -0500

EricCousineau-TRI gravatar image

For Python 3:

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/...

edit flag offensive delete link more

Comments

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?

emielke gravatar image emielke  ( 2021-07-27 15:24:17 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2018-09-12 01:51:45 -0500

Seen: 2,775 times

Last updated: Oct 04 '18