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

How to open a serial port using a ROS node?

asked 2018-10-22 06:46:42 -0500

Spyros gravatar image

updated 2018-10-22 09:39:09 -0500

I have setup a serial connection between my laptop and a PC. I run a Python script in order to send some data (strings, integers etc) to PC's serial terminal (the goal is to connect the laptop to a PIC microcontroller. The PC is just for testing). I want to to do the same thing using a ROS node, instead of just running the python script in an IDE. I spent days searching the internet for a solution and I tried several things I found, without finding the right way to do it.

If I understand correctly:

  • rosserial works only with "connected rosserial-enabled devices". But I don't want to modify the microcontroller's code.
  • I searched the serial package documentation: and but I didn't find out how to use it.
  • I found rosbridge_suite package: but I don't know if I can use it and how in my case.
  • I tried to run from rosserial_python package to see what happens (I changed serial port name and baud rate) I got the error: ImportError: No module named yaml Then, I searched to find how the nodes sends data but I didn't find any sent() function to use it in my script.
  • I tried to run my script as it is, using rosrun, adding only:

    #!/usr/bin/env python import rospy from std_msgs.msg import string

    and didn't work.

    • Then I changes my code and the final version is at the end of this post (some functions may not be necessary - I am sorry about the format). When I run it with rosrun I get the error: bad interpreter: No such file or directory and I get nothing on my serial terminal.

I use Ubuntu 16.04, ROS kinetic, Python 2.7.

Any possible solution, advise or example will be appreciated. Thanks in advance.

#!/usr/bin/env python
import rospy
import serial
import time
from std_msgs.msg import String
def serial_connection():
    rospy.init_node('serial_connection', anonymous = True)
    rate = rospy.Rate(10)
    syxnotita = 0
    euros = 0
    mesi_gwnia = 0
    while not rospy.is_shutdown():
        data = [syxnotita, euros, mesi_gwnia]
        ser = serial.Serial('/dev/ttyUSB0', 9600)
        print (
        while ser.isOpen():
            print ("Port Open")
            for i in range(5):
                print ('Hola!')
if __name__ == '__main__':
    except rospy.ROSInterruptException:
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-23 22:53:06 -0500

billy gravatar image

Using serial port is not really a ROS question, but I feel your pain so I'll help out.

First: your code runs just fine on 14.04.4 with Jade using just "rosrun serial". Confirmed serialport output as well as print statements working. No changes required at all. I suspect it runs fine on 16 as well but something wrong with your config. maybe

Second: I created the package with "catkin_create_pkg serial std_msgs rospy roscpp" as described in the tutorials. You didn't mention anything about creating a package so maybe that is where you missed out.

You'll need to explain what you mean by "and didn't work" because that kind of description isn't useful at all.

edit flag offensive delete link more

Question Tools



Asked: 2018-10-22 06:46:42 -0500

Seen: 2,587 times

Last updated: Oct 23 '18