How to read data from a serial port and publish it on a topic in C++
Language : C++
System : Ubuntu on Virtual Box
Hello,
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
Asked by Juliette_ROS on 2019-10-01 08:30:50 UTC
Answers
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 https://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/serial_port.html
Asked by duck-development on 2019-10-01 12:50:15 UTC
Comments
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
Asked by Juliette_ROS on 2019-10-02 02:46:00 UTC
Hi
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.
Asked by borgcons on 2020-06-08 16:11:11 UTC
Comments
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 (https://index.ros.org/doc/ros2/Releases/Release-Foxy-Fitzroy/#std-msgs) and the message in those message definitions: https://github.com/ros2/example_interfaces/blob/foxy/msg/String.msg.
Asked by Mbuijs on 2020-06-09 05:11:42 UTC
Comments
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.
Asked by gvdhoorn on 2019-10-01 08:54:08 UTC
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.
Asked by LeoE on 2019-10-01 09:01:03 UTC
@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 ?
Asked by Juliette_ROS on 2019-10-02 01:38:00 UTC
@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.
Asked by Juliette_ROS on 2019-10-02 02:40:23 UTC
But you did say:
So the only thing that exists is a protocol spec?
it doesn't need to be open-source. A simple C/C++ or Python library would already help quite a bit.
Asked by gvdhoorn on 2019-10-02 05:38:58 UTC
@gvdhoorn : I searched on internet but found nothing related to this device...
Asked by Juliette_ROS on 2019-10-03 01:53:01 UTC
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.
Asked by gvdhoorn on 2019-10-03 01:58:52 UTC
Maybe you better use data transfer serial port and access via
serial-over-ethernet.com
? As far as I know, it can work without problems on Ubuntu, and there is functionality for creating a virtual com port.Asked by blankTG on 2019-10-17 04:33:05 UTC
@blankTG: I've let your comment through moderation, and I've removed the link you included. It seems like a commercial product. Nothing wrong with that, but I'm not sure we want to link to an alternative solution like this when the OP has already solved things.
And I'd like to reduce the possibility that we're letting through spam accounts.
Asked by gvdhoorn on 2019-10-17 04:37:30 UTC