ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

read data from serial port and publish over a topic

asked 2014-04-30 10:12:23 -0600

uzair gravatar image

updated 2014-04-30 10:27:42 -0600

I am trying to write a code to read data from the serial port and publish it over a topic. I know that I can create an node out of the arduino but that will need some huge changes in the code. So this is what I am doing.

#!/usr/bin/env python
import roslib; roslib.load_manifest('numpy_tutorials') #not sure why I need this
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():
       data= ser.read(2) # I have "hi" coming from the arduino as a test run over the serial port
       rospy.loginfo(data)
       pub.publish(String(data))
       rospy.sleep(1.0)


if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

WHen I run this code, I get the following error.

Traceback (most recent call last):
  File "./serial.py", line 21, in <module>
    talker()
  File "./serial.py", line 8, in talker
    ser = serial.Serial('/dev/ttyACM0', 9600)
AttributeError: 'module' object has no attribute 'Serial'

I think the module serial is not being imported. But why? I was running this script in a package called numpy_tutorials. However when I removed this line import roslib; roslib.load_manifest('numpy_tutorials') #not sure why I need this

and copy pasted the same code in another package called beginner_tutorials, the code runs fine. Why is it not running in
numpy_tutorials package? I have the rospy dependency in both the package.xml files as well.

edit retag flag offensive close merge delete

Comments

/dev/ttyACM0 means Hokuyo laser? What is connected with the port?

Ken_in_JAPAN gravatar image Ken_in_JAPAN  ( 2014-04-30 10:37:09 -0600 )edit

I have the arduino mega connected to the port. It is writing "hi" to the serial port /dev/ttyACM0. I am still trying to setup the system before i transmit the actual data from the sensors.

uzair gravatar image uzair  ( 2014-04-30 11:04:30 -0600 )edit

Are you sure that the "serial" package that you import is indeed the one you need ? There might be some conflict with other packages. You can try "import serial; help(serial)" to get more details.

pierrelux gravatar image pierrelux  ( 2014-04-30 15:18:38 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-01-11 09:47:31 -0600

Sr4l gravatar image

The problem is your filename. You are not importing the PySerial library, you are importing a locale file in your current working directory.

edit flag offensive delete link more
0

answered 2014-05-22 00:00:56 -0600

Captcha gravatar image

updated 2014-05-22 00:01:36 -0600

Hi, try the following :

#!/usr/bin/env python
import roslib; roslib.load_manifest('numpy_tutorials') #not sure why I need this
import rospy
from std_msgs.msg import String
import serial

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

def talker():
 while not rospy.is_shutdown():
   data= ser.read(2) # I have "hi" coming from the arduino as a test run over the serial port
   rospy.loginfo(data)
   pub.publish(String(data))
   rospy.sleep(1.0)


if __name__ == '__main__':
  try:
    pub = rospy.Publisher('chatter', String)
    rospy.init_node('talker')
    talker()
  except rospy.ROSInterruptException:
    pass

i have it working fine with me !

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2014-04-30 10:12:23 -0600

Seen: 7,208 times

Last updated: May 22 '14