I'm working on a project in which I need to read and write data from a serial port, and this needs to be non-blocking for reasons I won't go into. The select() function looks like what I want to use, but I'm struggling with getting a working implementation.

In open_port() I define the settings for the port and that it is non-blocking. In otherselect() I assign the descriptor to the open_port() and attempt to read. I also have a 1 second sleep call at the end of the function to attempt to avoid the reading being too fast for hardware.

When running I get a message printing out every second for "no data available" before I send the message, and after I send a message it prints it out, but it is usually in pieces with binary characters along with it. For example, when sending the word "buffer" it will print "ffer" followed by a binary character.

I have almost no experience with termios or select, so any suggestions would be appreciated.

#include <iostream>
#include "stdio.h"
#include "termios.h"
#include "errno.h"
#include "fcntl.h"
#include "string.h"
#include "time.h"
#include "sys/select.h"

using namespace std;

int open_port(){
struct termios oldtio,newtio;
int serial_fd;
if ((serial_fd = open("/dev/ttyS0", O_RDWR | O_EXCL | O_NDELAY)) == -1) {
    cout << "unable to open" << endl;
    return -1;
}
if (tcgetattr(serial_fd, &oldtio) == -1) {
    cout << "tcgetattr failed" << endl;
    return -1;
}
cfmakeraw(&newtio); // Clean all settings
newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS8 | B115200; // 8 databits
newtio.c_cflag |= (CLOCAL | CREAD);
newtio.c_cflag &= ~(PARENB | PARODD); // No parity
newtio.c_cflag &= ~CRTSCTS; // No hardware handshake
newtio.c_cflag &= ~CSTOPB; // 1 stopbit
newtio.c_iflag = IGNBRK;
newtio.c_iflag &= ~(IXON | IXOFF | IXANY); // No software handshake
newtio.c_lflag = 0;
newtio.c_oflag = 0;
newtio.c_cc[VTIME] = 1;
newtio.c_cc[VMIN] 
Edit
Report