ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Trying to write a service to publish data from serial

asked 2013-03-09 03:34:40 -0600

ianphil397 gravatar image

updated 2014-01-28 17:15:36 -0600

ngrennan gravatar image

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/", line 22, in <module>
  File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/", line 16, in talker
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/ros_comm-1.8.12-py2.7.egg/rospy/", 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: <function callback="" at="" 0x922430="">
Traceback (most recent call last):
  File "/opt/ros/fuerte/lib/python2.7/dist-packages/ros_comm-1.8.12-py2.7.egg/rospy/", line 678, in _invoke_callback
  File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/", line 8, in callback
    rospy.loginfo(rospy.get_name() %
TypeError: not all arguments converted during string formatting

How do I fix this?

edit retag flag offensive close merge delete


Okay so I need something like

data = ser.readline()




ianphil397 gravatar image ianphil397  ( 2013-03-09 04:03:28 -0600 )edit

Your listener printout in loginfo is wrong. This is unrelated to this question. Please open a new one.

dornhege gravatar image dornhege  ( 2013-03-09 04:55:08 -0600 )edit

DId your code work?

uzair gravatar image uzair  ( 2014-04-30 09:43:17 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-03-09 04:01:40 -0600

dornhege gravatar image

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

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2013-03-09 03:34:40 -0600

Seen: 1,578 times

Last updated: Mar 09 '13