Error while receiving data from O-Drive via USB in C++ code

Hello Folks, I am using USB cable of my PC to communicate to O-Drive using ASCII protocol.
I am writing code in c++, and using this attached library for communication over /dev/ttyACM0.

But sometime, there is some problem and it misses the recieved values.
For this I am using blocking_mode to end reception on receiving /n/r.

Do somebody have a working c++ code to communicate to O-Drive ove USB with ASCII protocol.

My code is as below:

#ifndef ODRIVE_CONTROL__HARDWARE_SERIAL_INTERFACE_H
#define ODRIVE_CONTROL__HARDWARE_SERIAL_INTERFACE_H

#include <termios.h>
#include <fcntl.h>
#include
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>

#define SERIAL_BUFFER_SIZE 128

class comm_serial
{
int fd;
char buffer[SERIAL_BUFFER_SIZE];

int set_interface_attribs(int speed);
void set_blocking(int should_block);

public:
/* Send Command to Serial Port /
bool comm_write(std::string command)
{
int n = write(fd, command.c_str(), command.length());
if(n != command.length())
return false;
return true;
}
/
Read Response from Serial Port /
std::string comm_read(void)
{
std::string response;
memset(buffer, 0, SERIAL_BUFFER_SIZE);
int n = read(fd, buffer, SERIAL_BUFFER_SIZE);
if(n > 0)
{
response = buffer;
}
return response;
}
/
CONSTRUCTOR */
comm_serial(std::string portname, int speed)
{
fd = open(portname.c_str(), O_RDWR | O_NOCTTY | O_SYNC);
if(fd < 0)
{
printf(“error %d opening %s”, errno, portname.c_str());
}
set_interface_attribs(B115200); // set speed to 115,200 bps, 8n1 (no parity)
set_blocking(1); // set blocking
}
};

int comm_serial::set_interface_attribs(int speed)
{
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0)
{
printf(“error from tcgetattr: %d”, errno);
return -1;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);

tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break as \000 chars
tty.c_iflag &= ~IGNBRK;                         // ignore break signal
tty.c_lflag = 0;                                // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0;                                // no remapping, no delays
tty.c_cc[VMIN]  = 0;                            // read doesn't block
tty.c_cc[VTIME] = 5;                            // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY);         // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);                // ignore modem controls, enable reading
tty.c_cflag &= ~(PARENB | PARODD);              // shut off parity
tty.c_cflag |= 0;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;

if(tcsetattr(fd, TCSANOW, &tty) != 0)
{
    printf("error %d from tcsetattr", errno);
    return -1;
}
return 0;

}

void comm_serial::set_blocking(int should_block)
{
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0)
{
printf(“error %d from tggetattr”, errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if(tcsetattr(fd, TCSANOW, &tty) != 0)
printf(“error %d setting term attributes”, errno);
}

#endif // ODRIVE_CONTROL__HARDWARE_SERIAL_INTERFACE_H

I’m using boost::asio for communicating with ODrive in a project. Here is minimal example.
use

cmake . -Bbuild
cd build
make
./odrive_ascii

hope it helps.

I got to solve this problem by flushing before writing data to the fd.
So the code for sending data look like this:

bool comm_write(std::string command)
{
tcflush(fd, TCIFLUSH);
int n = write(fd, command.c_str(), command.length());
if(n != command.length())
return false;
return true;
}