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

How to read data from a serial port and publish it on a topic in C++

asked 2019-10-01 08:30:50 -0600

Juliette_ROS gravatar image

Language : C++

System : Ubuntu on Virtual Box


I am working on a Battery Management System (BMS), and I need to collect data from it (battery voltage, temperature, etc...). The connection is made through a USB cable, thanks to a USB Serial converter because the data is in a "serial form".

I managed to connect the BMS to my virtual machine and I can see it in the list when I type lsusb in the terminal of Ubuntu :

BUS 001 Device 003: ID 0403:6001 Future Technology Devices International, Ltd FT232 Serial (UART) IC

Now my problem is that I need to read the data from this BMS and I don't know how to do :

  • Should I create a publisher to publish this data on a topic ?
  • Should I download an extension made to deal with serial connection ?
  • How could I, at least, "ping" my BMS to see if it is well connected to my Virtual Machine ? Because I have the communication protocol, explaining that I should send a "$" and then receive data but I don't know how to communicate with the BMS, where should I write this "$" ?

I use ROS only for a week now so I am very unexperimented, if you have an answer to my question please detail it as much as possible !

Thank you

edit retag flag offensive close merge delete



Have you already checked whether there is some code that can interface with your BMS? A C/C++ or Python library perhaps? Is it completely custom, or could the mfg perhaps have some code?

It might even be that there already is a ROS node available for it.

gvdhoorn gravatar image gvdhoorn  ( 2019-10-01 08:54:08 -0600 )edit

Have a look at this these are examples to interface your FTDI IC. Regarding ROS you can either constantly publish your data or set up a service which responds with the read data. It really depends on what exactly you want to make your code do.

LeoE gravatar image LeoE  ( 2019-10-01 09:01:03 -0600 )edit

@gvdhoorn : I searched on the internet but the BMS is sold by 123Electric and this company provides a software to use the BMS, so I don't think there is some open code anywhere... But maybe I don't searched on the right websites, do you know where I should do my researches ?

Juliette_ROS gravatar image Juliette_ROS  ( 2019-10-02 01:38:00 -0600 )edit

@LeoE These example are made for Windows I think so I can't use them on Ubuntu. Yes I have seen the difference between publisher and service, for my work I can use both, the other programs will adapt their behavior to mine.

Juliette_ROS gravatar image Juliette_ROS  ( 2019-10-02 02:40:23 -0600 )edit

the BMS is sold by 123Electric and this company provides a software to use the BMS, so I don't think there is some open code anywhere

But you did say:

Because I have the communication protocol, explaining that I should send a "$" and then receive data

So the only thing that exists is a protocol spec?

I don't think there is some open code anywhere

it doesn't need to be open-source. A simple C/C++ or Python library would already help quite a bit.

gvdhoorn gravatar image gvdhoorn  ( 2019-10-02 05:38:58 -0600 )edit

@gvdhoorn : I searched on internet but found nothing related to this device...

Juliette_ROS gravatar image Juliette_ROS  ( 2019-10-03 01:53:01 -0600 )edit

Ok. Well. You could always ask the manufacturer. If you've already done that, it might be that writing something yourself is the way forward.

gvdhoorn gravatar image gvdhoorn  ( 2019-10-03 01:58:52 -0600 )edit

Maybe you better use data transfer serial port and access via As far as I know, it can work without problems on Ubuntu, and there is functionality for creating a virtual com port.

blankTG gravatar image blankTG  ( 2019-10-17 04:33:05 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-10-01 12:50:15 -0600

duck-development gravatar image

For first test you can use the screen Programm to send the character $ you may need an neu line press enter. You need the boudrate

For example so:

screen /dev/ttyUSB0 9600

You may need to add you user to plugdev and dailout group.

Then you can use to Programm it. I would recomand to use python because it is mutch simpler for the serial port handling.

If you have to use cpp the you may use the Boost serial_port as boost is paart of ros

edit flag offensive delete link more


Thanks for your answer !

  • What is the screen Programm ? Do I need to download it ?
  • What is plugdev and dailout group ?
  • I think I can use Python if needed

I am going to study Boost serial_port, to see if I can use it

Juliette_ROS gravatar image Juliette_ROS  ( 2019-10-02 02:46:00 -0600 )edit

answered 2020-06-08 16:11:11 -0600

borgcons gravatar image

updated 2020-06-08 16:14:29 -0600


What we have done is to create a serial node and publish to a string topic. We have done the same for a tcp and udp node as well.

For serial node you have to decide is you want to publish from a read or publish on a terminator. For ASCII protocols its usually done by ending with a terminator. So your serial node should be of type ASCII or BINARY. For ASCII it would send on a parameterized terminator. For BINARY it would send whatever the read returns. It would be the responsibility of the high node receiving the BINARY data to assemble the data into a packet.

BTW you will have to you assign to move the data from the serial port to the string topic.

Hope all that helps.

Separating in serial into a string topic also makes testing, and debugging easier. An extra feature is that if you have the same type of device, like a BMS, that returns a packet of UDP instead of serial your BMS node does not have to change. We actually ran into this situation and all that was required was to change a launch file.

edit flag offensive delete link more


Generally speaking, I would say that this is not a recommded way of working. See also the deprecation of primitive message types in std_msgs in ROS2 Foxy ( and the message in those message definitions:

Mbuijs gravatar image Mbuijs  ( 2020-06-09 05:11:42 -0600 )edit

Question Tools


Asked: 2019-10-01 08:30:50 -0600

Seen: 2,891 times

Last updated: Jun 08 '20