How can I implement timeout for read() when reading from a serial port (C/C++)
问题 I am reading bytes from a serial port in C++ using a file descriptor and the posix/unix read() function. In this example, I am reading 1 byte from the serial port (baud rate settings and similiar are omitted for clarity): #include <termios.h> #include <fcntl.h> #include <unistd.h> int main(void) { int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY); char buf[1]; int bytesRead = read(fd, buf, 1); close(fd); return 0; } If the device connected to /dev/ttyS0 does not send any information, the program