ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

C++ & Serial Read Problem

asked 2012-08-04 16:30:38 -0600

cheour gravatar image

updated 2014-01-28 17:13:14 -0600

ngrennan gravatar image


I'm trying to enhance my skills on ROS with C++. I'm already wrote programs on many languages but since strict C++ architecture is a little bit different for me so that I'm having problems with what I'm trying to.

My aim is to make a communication node that manages serial communication between my mcu and Ubuntu PC. I wrote the communication node in C++ and the simplized version looks like this:

namespace FF
  class SerialComm

                    void ManagePackages();
                    void PrintState();
                    bool WriteMotorPackage(FF::MotorDrive::Request &req,
                        FF::MotorDrive::Response &res);

                    ros::NodeHandle         node;


                    void InitPublishers();
                    void OpenConnection();
                    void SearchPackages();
                    void CatchPackage();
                    void DecodePackage();
                    void PublishFState();
                    void PublishMotorState();

                    float BytesToFloat(unsigned char b0,
                                        unsigned char b1,
                                        unsigned char b2,
                                        unsigned char b3);

                    ros::Publisher          FStatePublisher;
                    ros::Publisher          motorStatePublisher;

                    cereal::CerealPort      device;

                    char                    reply[4096];
                    std::vector<unsigned char>      validData;
                    int                     dataSize;

        struct Motors
          unsigned char _1;
          unsigned char _2;

        struct FState_
          char errors;
                      int distance

                   FState_  FState;
           Motors   FMotors;



What this code doing is First open a serial connection and then in each while cycle to read the serial port with SearchPackages and CatchPackages, finally decode package with DecodePackages function. After package decode I'm using Publish functions. What I do is write the values to FMotors variable like FMotors._1 = incomingData and then publish it with a ROS publisher.

But the problem occurs here. First time it reads the value and write it to variables and publish it. And I can see it from the topic echo. But after first read it never updates the variables. Topic Variables always have the value of the first read. But I'm sure there is no problem with reading and catching true values from serial port. Problem is updating the variables. Maybe because of I'm not very familiar with pointer and static concepts in C++ object style. Whatever it is your ideas and helps will be very useful to solve this. problem.

edit retag flag offensive close merge delete


hard to say what the problem is from your class description. Are you able to confirm that the serial reading and decoding is working correctly every time? If so then, given what you say sounds like you migh that some static object/variable in your code causing the once only execution.

MarkyMark2012 gravatar image MarkyMark2012  ( 2012-08-05 21:00:39 -0600 )edit

How does your main look like? Do you call ros::spin or ros::spinOnce?

Lorenz gravatar image Lorenz  ( 2012-08-06 03:13:49 -0600 )edit

I solved it! Well it

cheour gravatar image cheour  ( 2012-10-19 02:17:12 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2012-10-19 02:17:12 -0600

cheour gravatar image

updated 2012-10-19 02:18:20 -0600

I solved it! Well it looks like I did not familiar with const function parameter. I set function parameters to constant parameters like myFunction(const uint8[] msg) so it is working great now.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-08-04 16:30:38 -0600

Seen: 655 times

Last updated: Aug 04 '12