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

rostopic pub does not allow carriage return or line feed in the string

asked 2019-10-18 17:03:07 -0500

borgcons gravatar image

Am am trying to send a string like


to a modem through a String topic in ROS

I am able to do it in code but I can not do it in a terminal window

rostopic pub /my_topic std_msgs/String "data: 'AT\r\n'"

Is there a way to do this?

edit retag flag offensive close merge delete


I have one terminal opened with $ rostopic echo /foo and another where I do $ rostopic pub /foo std_msgs/String 'AT\r\n'. The first one receives, as expected:

data: "AT\\r\\n"

The same output can be achieved with $ rostopic pub /banana std_msgs/String "{data: AT\r\n}" which is more similar to your syntax.

This to say I don't think your issue is clear. Is it that you don't want the double backslash? Or you want the \r and \n to be interpreted as carriage return?

aPonza gravatar image aPonza  ( 2019-10-23 11:06:07 -0500 )edit

Yes I want the \r and \n to be interpreted as CR and LF

borgcons gravatar image borgcons  ( 2020-05-12 04:26:45 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2023-06-08 05:48:15 -0500

Rufus gravatar image

If you want \n to be translated into a newline, you can combine echo -e with rostopic pub

rostopic pub /my_topic std_msgs/String "$(echo -e "data: 'AT\r\n")"


edit flag offensive delete link more

answered 2019-10-18 19:08:19 -0500

updated 2019-10-18 23:36:29 -0500

s.macenski@reese:~$ echo $'\r'

s.macenski@reese:~$ echo '\r'

Does the $ fix your issue?

Edit: I think you actually want \\ out instead. But you'd have to indicate specifically what issue you're having that you're looking to solve, its not necessarily clear to me, maybe I'm misreading what you're looking for

edit flag offensive delete link more


I'm not sure how this helps me do a rostopic pub

borgcons gravatar image borgcons  ( 2019-10-18 19:27:02 -0500 )edit

because its still your shell interpreting the return. Did you try it?

stevemacenski gravatar image stevemacenski  ( 2019-10-18 19:46:45 -0500 )edit

This is what I am trying to do:

rostopic pub /my_topic std_msgs/String "data: 'AT\r\n'"

How do I use the $ for this?

I just don't get it yet

borgcons gravatar image borgcons  ( 2019-10-18 21:46:59 -0500 )edit

I'm suggesting trying 'AT$\r\n' but I'm not sure if it'll work, I was looking to you to tell me if that fixes it.

If not another option to try is double slash, ei 'AT<2 \>r<2 \>n' which should also potentially give you the viable solution

stevemacenski gravatar image stevemacenski  ( 2019-10-18 23:30:57 -0500 )edit

Neither work

borgcons gravatar image borgcons  ( 2019-10-19 17:10:44 -0500 )edit

Question Tools

1 follower


Asked: 2019-10-18 17:03:07 -0500

Seen: 802 times

Last updated: Jun 08 '23