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

ROS serial communication with microcontroller [closed]

asked 2012-03-21 10:02:09 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

I am newbie to ROS and I want to send Odometry data from P89V51RD2 controller to ROS. I found link: text , but I am not able to run it .. Can someone help out with same code , right from creating packages and meeting dependencies.. Thanks..

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by SL Remy
close date 2017-07-21 02:02:04.653467


I want to send the 'x' 'y' 'z' coordinates as (char) from uC to ROS ..

anonymous userAnonymous ( 2012-03-21 10:06:35 -0600 )edit

I presume you're referring to Could you provide some background into what problems you're having?

Ryan gravatar image Ryan  ( 2012-03-21 10:09:10 -0600 )edit

Hmm. I am referring to the code in the link .. I just found out that it is working, but it is taking 30 seconds or more to receive data.. it takes data for some time and after few seconds, it publishes all .. is it possible to make it more nearer to real time.. should i change rcvBufSize..?

anonymous userAnonymous ( 2012-03-21 13:09:22 -0600 )edit

When i set rcvBufSize to 1, i dont get any true response, it keeps on giving data = ' ' as output to rostopic echo uc0Response.. when i set rcvBufSize to 5 , I get response in group of 4 without loss of any data..but it take more than 20 seconds to show up on the screen..

anonymous userAnonymous ( 2012-03-21 13:37:10 -0600 )edit

is it normal to get such slow data fetching and publishing?

anonymous userAnonymous ( 2012-03-21 15:30:22 -0600 )edit

8 Answers

Sort by » oldest newest most voted

answered 2012-03-21 12:44:05 -0600

tjay gravatar image

updated 2012-03-21 12:44:33 -0600

It isn't serial specific like rosserial, but rosbridge may also suit your needs (full disclosure, author of rosbridge here). Check out this video of using rosbridge with an Arduino (though the principles apply to any uController).

edit flag offensive delete link more


We tend to recommend rosserial with uCs with Arduino-like specs, and rosbridge for more capable ones (ARM, for instance).

Ryan gravatar image Ryan  ( 2012-03-21 15:52:17 -0600 )edit

Good point. I guess I tend to think the opposite because with less capable uCs I use a machine side adapter program and a light custom protocol. I hate having any sort of real protocol implementation on the uC unless it's beefy. The exception is rosserial on Arduino, because of the good support.

tjay gravatar image tjay  ( 2012-03-21 18:43:12 -0600 )edit

answered 2012-03-22 00:19:46 -0600

You can always use cereal_port, a serial port library for ROS, you can find it in the serial_communication stack, it's very easy to use!

Hope this helps.

edit flag offensive delete link more


Agree, cereal_port is a good library

Kevin gravatar image Kevin  ( 2012-03-22 14:03:59 -0600 )edit

Pardon this newbie, but doesn't seem to have support for Hydro yet, or am I confused about how the wiki presents information. I see Electric and Fuerte, but no Hydro. What is the best/easiest way to do serial communication from Hydro? Thanks.

Kurt Leucht gravatar image Kurt Leucht  ( 2014-01-09 08:49:01 -0600 )edit

answered 2012-03-21 18:33:39 -0600

tjay gravatar image

updated 2012-03-21 18:50:16 -0600

I also apologize for commenting as an answer (however, 300 characters without advanced formatting was not likely going to cut it for a response).

First off, let me say that I understand that unix command hackery isn't everyone's cup of tea. A simple helper program, such as this Python equivalent to my example one-liner does the same job just as well. As with the unix command, you may need to change the serial device name.

One the other hand, if you do want to get this working from the command line...

I assume you intentionally changed /dev/ttyACM0 to /dev/ttyUSB0. However, you may still want to check your device name and suffix number. I also assume you intentionally changed the - to STDOUT. Those changes aside, I do see a few potential typos:

1,1 s/^./raw\r\n\r\n/

should read:

1,1 s/^.*/raw\r\n\r\n/

(i.e. it's missing a star)


should read:


(i.e. missing a star and some quoting backslashes)

and your:


is missing a quote after the :, it should read:


finally (and this may be an intentional omission on your part) your orison is missing the final:

| nc localhost 9090

All that said, you can copy the exact orison (sans your device changes) from this pastebin, mentioned in the video.

edit flag offensive delete link more



Agree, the character limit in comments is really annoying

Kevin gravatar image Kevin  ( 2012-03-22 14:03:15 -0600 )edit

I also just realized that if you aren't logged in you don't see past the first five comments and aren't offered the textual "more" button. However, if you try to post a comment, the comments are revealed to you.

What is this? I don't even...

tjay gravatar image tjay  ( 2012-03-23 03:41:13 -0600 )edit

answered 2012-03-21 15:28:11 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

@above, Sir, I do not get any output after I enter : socat /dev/ttyUSB0,ignoreeof STDOUT | sed -u -e '1,1 s/^./raw\r\n\r\n/' -e '1! s/([0-9])/\x00{"receiver":"\/echoTime","msg":{"data":\1},"type":std_msgs\/Int64"}\xff/'

anything wrong with the statement. Am using diamondback ros and same rosbridge version.. [Sorry, I didnt find the comment button at above answer]

edit flag offensive delete link more


@tjay: Sir, thanks for such detailed reply, and I am really bad at Linux command line ... I was tending to do the same thing you did in video at about 11:30 .. STDOUT came from there and at about 14:30 , when it checked that data is being received, there is no | nc .. part ....

anonymous userAnonymous ( 2012-03-21 22:02:44 -0600 )edit

I checked for 'ttyUSB0', It is correct and after entering the above line else there might be permission error, when list the rostopic, /echoTime still doesnt show up.. I copy pasted from the paste bin, still same result, nothing shows up.. :(

anonymous userAnonymous ( 2012-03-21 22:05:12 -0600 )edit

@chirag712 No problem. Have you tried the python program? (after changing the device stuff)

tjay gravatar image tjay  ( 2012-03-21 22:31:55 -0600 )edit

Man , Am looking like real dumbhead here, I did run the python code , "python /home/chirag/" without anything, I downloaded the file and just ran it, I dont know anything aboout python.. :( .. I got following error File "/home/chirag/", line 7, in <module> sock.connect(('localhost'

anonymous userAnonymous ( 2012-03-21 23:08:33 -0600 )edit

...File "/home/chirag/", line 7, in <module> ... sock.connect(('localhost',9090)) File "/usr/lib/python2.7/", line 224, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 111] Connection refused

anonymous userAnonymous ( 2012-03-21 23:09:07 -0600 )edit

@chirag712 No worries. rosbridge has to be up and there has to be something printing numbers to the relevant serial port before you run the adapter. It's not particularly well-written. :)

tjay gravatar image tjay  ( 2012-03-21 23:31:34 -0600 )edit

File "/home/chirag/", line 16, in <module> usecs = int(line) ValueError: invalid literal for int() with base 10: 'S' .. I sent serially 'S' character from the controller..

anonymous userAnonymous ( 2012-03-21 23:59:59 -0600 )edit

So the program is designed to receive a series of numbers followed by newlines, what are you trying to send?

tjay gravatar image tjay  ( 2012-03-22 04:34:10 -0600 )edit

answered 2012-03-22 10:29:04 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Could you refer to this link :screenshot, to see what actually the problem is.. Please help .. none of the python progs worked for me , probably am doin something wrong , so please check the video for procedure I am following .. Thank you all for your efforts so far.. :)

edit flag offensive delete link more


This is invaluable for debugging, thanks so much for making the effort to do this. Looking at the video, it looks like I haven't made it clear that and don't output to STDOUT. They output to the /echoTime and /serialChar ROS topics.

tjay gravatar image tjay  ( 2012-03-22 11:29:46 -0600 )edit

To view the output, when is running open another ROS terminal and type: rostopic echo /serialChar

tjay gravatar image tjay  ( 2012-03-22 11:30:09 -0600 )edit

The procedure is similar for (though looking at your code I don't think the number-centric is likely to work): rostopic echo /echoTime

tjay gravatar image tjay  ( 2012-03-22 11:31:27 -0600 )edit

Let me know if either of those commands work for you. Sorry for all these comments, I didn't want to open another answer and had more than 300 characters of content and wanted to use whitespace.

tjay gravatar image tjay  ( 2012-03-22 11:32:36 -0600 )edit

Yuppie..the works fine , just didnt check the rostopic echo /serialchar .. is there any tutorial available so that i can modify the code to publish strings ..i mean i can set the length before which each character is buffered and published together as string.. ?

anonymous userAnonymous ( 2012-03-22 11:57:31 -0600 )edit

i get this error with sane: chirag@Thunderbolt:~$ python /home/chirag/ Traceback (most recent call last): File "/home/chirag/", line 16, in <module> usecs = int(line) ValueError: invalid literal for int() with base 10: 'S'

anonymous userAnonymous ( 2012-03-22 12:04:15 -0600 )edit

So we have basic communication working. Excellent! Given what I have seen of your application it isn't surprising that sane4char works while sane does not. sane assumes the incoming data is numerical. The error you're seeing is the result of it trying to count to 'S'.

tjay gravatar image tjay  ( 2012-03-22 14:09:21 -0600 )edit

As for tutorials on writing more advanced adapters, unfortunately I have not found time to do that just yet. However, if all you're trying to do is receive strings and you can make sure those strings are newline ('\n') terminated, the changes required are not so difficult (see next comment).

tjay gravatar image tjay  ( 2012-03-22 14:14:55 -0600 )edit

answered 2012-03-22 02:02:53 -0600

I haven't tested the example but it should give you an idea of what is required to get this working. Notice that you have additional methods for reading from the serial port such as readLine or readBetween that allows you to read between 2 predefined chars.

#include <ros/ros.h>
#include <cereal_port/CerealPort.h>

#define REPLY_SIZE 8
#define TIMEOUT 1000

// This example opens the serial port and sends a request 'R' at 1Hz and waits for a reply.
int main(int argc, char** argv)
    ros::init(argc, argv, "example_node");
    ros::NodeHandle n;

    cereal::CerealPort device;
    char reply[REPLY_SIZE];

    // Change the next line according to your port name and baud rate
    try{"/dev/ttyUSB0", 9600); }
    catch(cereal::Exception& e)
        ROS_FATAL("Failed to open the serial port!!!");
    ROS_INFO("The serial port is opened.")

    ros::Rate r(1);
        // Send 'R' over the serial port

        // Get the reply, the last value is the timeout in ms
        try{, REPLY_SIZE, TIMEOUT); }
        catch(cereal::TimeoutException& e)
        ROS_INFO("Got this reply: %s", reply);


To compile this example.cpp file you should make sure you have the serial_communication stack installed and don't forget to add the following lines to your pkg CMakeList.txt:

rosbuild_add_executable(example_node src/example.cpp)
target_link_libraries(example_node cereal_port)
edit flag offensive delete link more

answered 2012-03-22 04:48:08 -0600

tjay gravatar image

If you're trying to publish characters, try this Python program instead.

Before running it, make sure rosbridge and your uC is running. You may need to edit the program for your serial device (i.e. speed and name).

edit flag offensive delete link more

answered 2012-03-22 00:52:06 -0600

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

@above : Sir am very new to ROS and Linux, could you provide with some sample code, I need to publish the data that i get from serial port. The data is of form of a simple char.

edit flag offensive delete link more

Question Tools


Asked: 2012-03-21 10:02:09 -0600

Seen: 7,594 times

Last updated: Mar 22 '12