0
$\begingroup$

Rosanswers logo

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.


Originally posted by uzair on ROS Answers with karma: 87 on 2014-04-30

Post score: 0


Original comments

Comment by Ken_in_JAPAN on 2014-04-30:
/dev/ttyACM0 means Hokuyo laser? What is connected with the port?

Comment by uzair on 2014-04-30:
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.

Comment by pierrelux on 2014-04-30:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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


Originally posted by Sr4l with karma: 26 on 2016-01-11

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

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