Rosanswers logo

For a lab at my university I have developed a robot with an Arduino Mega ADK that can be controlled by ROS via the rosserial package. The connection between the Arduino and the laptop running ROS is made via bluetooth. During the prototyping phase the bluetooth module, HC-05, was connected to the Serial port that is also used for flashing the Arduino. Whenever I wanted to flash the Arduino I just disconnected the bluetooth module to prevent it from interfering.

However, we need to put all the electronics in a 3D printed casing, because we don't want students to touch the electronics during the lab. This revealed that we needed a solution for the interference of the bluetooth module. One option is to turn off the bluetooth with a switch, but this is far from ideal. The other option is to force the Arduino to use another Serial port (Serial1 in this case) for communication with ROS. In ros_lib (generated withrosrun rosserial_arduino make_libraries.py .) I found the file ArduinoHardware.h which contains some definitions including the iostream on lines 66-72. I changed line 71 to Serial1 which forced the Arduino to use Serial1 instead of Serial.

Although this did the job, it is not a very elegant solution. Is there some other, better way to solve this problem? For instance, could I use #define USBCON to change the Serial port or will this have other implications because this is for the Leonardo? (I only saw this just now, so I still have to test this)

P.S. Just to be sure, I'm looking for an answer for the client side, not the host side. I already found numerous answers for changing the port on the host side.

Originally posted by WFH27 on ROS Answers with karma: 33 on 2014-11-25

Post score: 2


1 Answer 1


Rosanswers logo

You need to find the ArduinoHardware.h file in your machine (it's usually in your libraries folder somewhere in ros_lib) and change this line to use the Serial you want (e.g. iostream = &Serial1;)

Hope it helps!

Sorry, I don't know why I didn't see that on the OP.

Here's what I've found, you should create a new Hardware definition class in your sketch and specify the port you want there and instanciate it when you create the nodeHandle object.

class NewHardware : public ArduinoHardware
  NewHardware():ArduinoHardware(&Serial1, 57600){};

ros::NodeHandle_<NewHardware>  nh;

I don't have a Mega to test this so I used a regular UNO and re-specified &Serial as the serial port and it worked.

Hope it helps!

Originally posted by Gary Servin with karma: 962 on 2014-11-26

This answer was ACCEPTED on the original site

Post score: 4

Original comments

Comment by WFH27 on 2014-11-27:
The solution that you describe is what I already came up with in my question. This will fix the Serial port for all sketches that I want to write, but I would like a solution where I can adjust the used Serial port per sketch.

Comment by WFH27 on 2014-11-27:
Thanks a lot. It works.

Comment by Gary Servin on 2014-11-27:
can you mark it as solved if this worked for you? So others can find it useful. Thanks!

Comment by narendiran1996 on 2017-01-15:
i had different problem. This solved. Thanks very much.

Comment by Marcus Barnet on 2022-03-03:
thanks! it worked for me!


Your Answer

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