C++ & Serial Read Problem
Hi,
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
{
public:
SerialComm();
~SerialComm();
void ManagePackages();
void PrintState();
bool WriteMotorPackage(FF::MotorDrive::Request &req,
FF::MotorDrive::Response &res);
ros::NodeHandle node;
private:
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.
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.
How does your main look like? Do you call ros::spin or ros::spinOnce?
I solved it! Well it