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

Doubt regarding serial communication

asked 2015-07-09 04:44:20 -0600

vinjk gravatar image

updated 2015-07-09 20:44:54 -0600

Hi all,

I am new to ROS and Linux. I am using ROS Indigo on Ubuntu 14.04 LTS. I am trying to establish serial communication with an microcontroller using ROS serial package ( ). Came across an example program by garyservin ( ). The program works well.

The instruction packet I want to send to MCU has start bytes "@@". However, when I try to publish to this to the topic "write" I get this error:

rostopic: error: Argument error: while scanning for the next token found character '@' that cannot start any token   in "<string>", line 1, column 1:

Similarly if I try to send a number I get this error:

ERROR: Unable to publish message. One of the fields has an incorrect type:
  field data must be of type str

The datatype used in the example program is std_msgs/String.

What I want to send is actually data in hex code. Something like 0x40 0x40 0x09 0x01 0x00 0x00 0x02 0x0d 0x0a

Could someone guide me on how to solve this problem?


#include <ros/ros.h>
#include <serial/serial.h>
#include <std_msgs/String.h>
#include <std_msgs/Empty.h>

serial::Serial ser;

void write_callback(const std_msgs::String::ConstPtr& msg){
    ROS_INFO_STREAM("Writing to serial port" << msg->data);

int main (int argc, char** argv){
    ros::init(argc, argv, "serial_example_node");
    ros::NodeHandle nh;

    ros::Subscriber write_sub = nh.subscribe("write", 1000, write_callback);
    ros::Publisher read_pub = nh.advertise<std_msgs::String>("read", 1000);

        serial::Timeout to = serial::Timeout::simpleTimeout(1000);
    catch (serial::IOException& e)
        ROS_ERROR_STREAM("Unable to open port ");
        return -1;

        ROS_INFO_STREAM("Serial Port initialized");
        return -1;

    ros::Rate loop_rate(5);


            ROS_INFO_STREAM("Reading from serial port");
            std_msgs::String result;
            ROS_INFO_STREAM("Read: " <<;



edit retag flag offensive close merge delete


It looks like you're writing a node in python which is publishing to a topic with the std_msgs/String type. You should post the source for that program if you want help with it, or follow @William's suggestion and just modify the example program.

ahendrix gravatar image ahendrix  ( 2015-07-09 18:57:52 -0600 )edit

Sorry about that. The program is the same one made by garyservin (see the link). I've posted the program in my question for your convenience. It's C++ program.

vinjk gravatar image vinjk  ( 2015-07-09 20:46:27 -0600 )edit

The error message you post is clearly coming from a python program, and not for the sample code. If you're publishing to the write topic using rostopic, please include the exact command you're running.

ahendrix gravatar image ahendrix  ( 2015-07-09 23:02:07 -0600 )edit

This is the exact command I tried to run

rostopic pub /write -r 100 std_msgs/String @@1233
vinjk gravatar image vinjk  ( 2015-07-10 00:39:51 -0600 )edit

Did you try putting the string you want to send in quotes?

William gravatar image William  ( 2015-07-10 02:05:57 -0600 )edit

@William I had tried that as well

vinjk gravatar image vinjk  ( 2015-07-10 03:34:53 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-07-10 03:32:29 -0600

vinjk gravatar image

Hi @William , @ahendrix

I solved the problem. I did as per William's suggestion used UInt8MultiArray datatype. But before writing it to the serial port I convert it to string.

void write_callback(const std_msgs::UInt8MultiArray::ConstPtr& msg)
    std::string str(msg->data.begin(), msg->data.end());

I used the command below (just an example) to publish to topic /write

rostopic pub /write std_msgs/UInt8MultiArray '{data: [64,64,11,1,1,1,2,208,32,13,10]}'
edit flag offensive delete link more


Thank you guys for your time.

vinjk gravatar image vinjk  ( 2015-07-10 03:37:12 -0600 )edit

answered 2015-07-09 16:12:54 -0600

William gravatar image

This isn't the most efficient way to talk with your micro, you could just modify the example to write directly to you micro or you could modify the node to take an array of uint8_t's rather than a string.

edit flag offensive delete link more


tried as you suggested. Used std_msgs/UInt8MultiArray instead of std_msgs/String

void write_callback(const std_msgs::UInt8MultiArray::ConstPtr& msg){        
    //ROS_INFO_STREAM("Writing to serial port " << msg->data);

Nothing is send or received

vinjk gravatar image vinjk  ( 2015-07-09 22:17:18 -0600 )edit

[character limit reached in prev comment so writing this here]

I executed this in another terminal

rostopic pub /write std_msgs/UInt8MultiArray '{data: [82, 79, 83]}' -r 1
vinjk gravatar image vinjk  ( 2015-07-09 22:21:16 -0600 )edit

Question Tools

1 follower


Asked: 2015-07-09 04:44:20 -0600

Seen: 855 times

Last updated: Jul 10 '15