c++ application quits without error on serial data receive

  c++, serial-port, uart

I am trying to set up serial port communication in a c++ application on Ubuntu 20. I am opening the serial port like this:

serialPort = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (serialPort == -1)
{
    perror("FAILED ");
    exit(1);
}

fcntl(serialPort, F_SETOWN, getpid());
fcntl(serialPort, F_SETFL, (FNDELAY | FASYNC));

struct termios options;

tcgetattr(serialPort, &options);

cfsetispeed(&options, (speed_t) B115200);
cfsetospeed(&options, (speed_t) B115200);

options.c_cflag |= (CLOCAL | CREAD);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

options.c_iflag &= ~(IXON | IXOFF | IXANY | INPCK | ISTRIP | IGNBRK | BRKINT | PARMRK | IGNCR | ICRNL);

options.c_oflag &= ~OPOST;

options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;

tcsetattr(serialPort, TCSANOW, &options);

I know the port is open and functional, because when I try to write to it, my other device does receive the data. But every time the other device writes data back, the application quits without an error message. Even when I am in debug mode, the debugger just drops and the application quits. This happens regardless of who sends data first, but it does happen consistently every time data is received. Because of this I can also not check the data using the application, since it exits before I would get the chance. Does anybody know what is going on?

For those interested, here is the code I use to write to the serial port:

void MotorBenchmarker::sendMessage(char* message, int size)
{
    write(serialPort, message, size);
}

Source: Windows Questions C++

LEAVE A COMMENT