Serialize ROS message and pass it
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.
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?
Also:
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?
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.
This is a code example:
The String object is std_msgs string- ros msg.
Please edit your original question and expand it a little to make this more clear.
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 froms1
that it doesn't support.Thanks, I've solved it.
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.