Rosanswers logo

Hi, I've copied the code from the tutorial "publisher and subscriber", and tried to modify it to read serial data from an an arduino and publish it. Here is the code for my publisher:

#!/usr/bin/env python
import roslib; roslib.load_manifest('beginner_tutorials')
import rospy
from std_msgs.msg import String
import serial

def talker():
    ser = serial.Serial('/dev/ttyACM0', 9600)

    pub = rospy.Publisher('chatter', String)
    while not rospy.is_shutdown():

if __name__ == '__main__':
    except rospy.ROSInterruptException:

The code for my listener is unchanged from the tutorial. when I rosrun them both the publisher stop with the following error message:

File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/talker.py", line 22, in 
  File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/talker.py", line 16, in talker
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/ros_comm-1.8.12-py2.7.egg/rospy/topics.py", line 796, in publish
    raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data must be of type str

What is it I'm doing wrong and how and I resolve this issue? (I'm aware I can ser up the Arduino to publish directly though I'd like to avoid it as that would require a large modification to my Arduino code.)

Amend: After adding the following code:

sensor = ser.readline()

When I run them both I get the following error from the listener.

[ERROR] [WallTime: 1362846465.102142] bad callback: 
Traceback (most recent call last):
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/ros_comm-1.8.12-py2.7.egg/rospy/topics.py", line 678, in _invoke_callback
  File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/listener.py", line 8, in callback
    rospy.loginfo(rospy.get_name() % data.data)
TypeError: not all arguments converted during string formatting

How do I fix this?

Originally posted by ianphil397 on ROS Answers with karma: 11 on 2013-03-09

Post score: 1

Original comments

Comment by ianphil397 on 2013-03-09:
Okay so I need something like

data = ser.readline()




Comment by dornhege on 2013-03-09:
Your listener printout in loginfo is wrong. This is unrelated to this question. Please open a new one.

Comment by uzair on 2014-04-30:
DId your code work?


1 Answer 1


Rosanswers logo

You are publishing the Serial object instead of the line that you read from it (which you store nowhere).

Originally posted by dornhege with karma: 31395 on 2013-03-09

This answer was ACCEPTED on the original site

Post score: 2


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.