The way I did this in the past is to configure the port for asynchronous I / O using VMIN 0 and VTIME, say 5 decibeds. The purpose of this was to let the thread notice when it was time to close the application, as it might try to read, time out, check the close flag, and then try to read a little more.
Here is an example read function:
size_t myread(char *buf, size_t len) {
size_t total = 0;
while (len > 0) {
ssize_t bytes = read(fd, buf, len);
if (bytes == -1) {
if (errno != EAGAIN && errno != EINTR) {
if (total > 0) {
return total;
}
else {
return -1;
}
}
}
else if (bytes == 0) {
return total;
}
else {
total += bytes;
buf += bytes;
len -= bytes;
}
}
return total;
}
The recording function will look as you expected.
In your setup, make sure you install:
struct termios tios;
...
tios.c_cflag &= ~ICANON;
tios.c_cc[VMIN] = 0;
tios.c_cc[VTIME] = 5;
...
source
share