Trying to write a service to publish data from serial
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)
rospy.init_node('talker')
while not rospy.is_shutdown():
ser.readline()
rospy.loginfo(ser)
pub.publish(String(ser))
rospy.sleep(1.0)
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
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 <module> talker() File "/home/pi/fuerte_workspace/sandbox/serial_message/scripts/talker.py", line 16, in talker pub.publish(String(ser)) 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()
rospy.loginfo(sensor)
pub.publish(String(sensor))
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/topics.py", line 678, in _invoke_callback cb(msg) 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?
Okay so I need something like
then
?
Your listener printout in loginfo is wrong. This is unrelated to this question. Please open a new one.
DId your code work?