+ while (true) {\r
+ int res = write(((serial_port_unix*)sp)->fd, pbtTx, szTxLen - bytes_written);\r
+ if (res < 0 && res != EAGAIN && res != EWOULDBLOCK) return false;\r
+ if (res > 0) {\r
+ pbtTx += res;\r
+ bytes_written += res;\r
+ }\r
+ if (bytes_written == szTxLen) return true; // we could write all bytes\r
+ gettimeofday(&t_current, NULL);\r
+ if (timercmp(&t_current, &t_end, >)) return false; // timeout\r
+ // set next select timeout\r
+ struct timeval t_remains;\r
+ timersub(&t_end, &t_current, &t_remains);\r
+ // Set the file descriptor set\r
+ fd_set wfds;\r
+ FD_ZERO(&wfds);\r
+ FD_SET(((serial_port_unix*)sp)->fd, &wfds);\r
+ // wait until more bytes can be written\r
+ res = select(((serial_port_unix*)sp)->fd+1, NULL, &wfds, NULL, &t_remains);\r
+ if (res < 0) return false; // error\r
+ if (res == 0) return false; // timeout\r