I am using the termios api in Linux to communicate with a serial device. I'm trying to detect if the device has disconnected so I can try to reconnect after some timeout. I have the following example code:
while(1)
{
FD_ZERO(&rfds);
FD_SET(tty_fd, &rfds);
// have tried checking fcntl(tty_fd, F_GETFL); too
// Blocking call to wait until we have data
select(tty_fd+1, &rfds, NULL, NULL, NULL);
// While we have data, collect it
while (read(tty_fd, &c, 1)>0 && bytesRead++<200)
{
serialBuffer.push_back(c);
}
bytesRead = 0;
// Try to parse it
BufferParse();
}
I'm not actually seeing select() or fcntl return error values (-1) after the ttyUSB device is physically disconnected. I could, of course, check to see if the file in /dev/ exists, but I was hoping there was a more elegant solution.
Would appreciate any advice, thanks!