|
8 | 8 |
|
9 | 9 | #include "libserial/serial.hpp" |
10 | 10 | #include <string> |
| 11 | +#include <poll.h> |
11 | 12 |
|
12 | 13 | namespace libserial { |
13 | 14 |
|
@@ -94,22 +95,21 @@ size_t Serial::readUntil(std::shared_ptr<std::string> buffer, char terminator) { |
94 | 95 | throw SerialException("Read timeout exceeded while waiting for terminator"); |
95 | 96 | } |
96 | 97 |
|
97 | | - // Use select() to check if data is available with remaining timeout |
98 | | - fd_set read_fds; |
99 | | - FD_ZERO(&read_fds); |
100 | | - FD_SET(fd_serial_port_, &read_fds); |
| 98 | + // Use poll() to check if data is available with remaining timeout. |
| 99 | + // poll() does not have the FD_SETSIZE limitation that select() has |
| 100 | + // and is more robust for larger file descriptor values. |
| 101 | + struct pollfd pfd; |
| 102 | + pfd.fd = fd_serial_port_; |
| 103 | + pfd.events = POLLIN; |
101 | 104 |
|
102 | | - struct timeval timeout; |
103 | 105 | int64_t remaining_timeout = read_timeout_ - elapsed; |
104 | | - timeout.tv_sec = remaining_timeout / 1000; |
105 | | - timeout.tv_usec = (remaining_timeout % 1000) * 1000; |
| 106 | + int timeout_ms = static_cast<int>(remaining_timeout); |
106 | 107 |
|
107 | | - int select_result = select(fd_serial_port_ + 1, &read_fds, nullptr, nullptr, &timeout); |
108 | | - |
109 | | - if (select_result < 0) { |
110 | | - throw SerialException("Error in select(): " + std::string(strerror(errno))); |
| 108 | + int poll_result = poll(&pfd, 1, timeout_ms); |
| 109 | + if (poll_result < 0) { |
| 110 | + throw SerialException("Error in poll(): " + std::string(strerror(errno))); |
111 | 111 | } |
112 | | - else if (select_result == 0) { |
| 112 | + else if (poll_result == 0) { |
113 | 113 | throw SerialException("Read timeout exceeded while waiting for data"); |
114 | 114 | } |
115 | 115 | } |
|
0 commit comments