diff --git a/src/modules/mavlink/mavlink_receiver.cpp b/src/modules/mavlink/mavlink_receiver.cpp index 29f299f918..c4f1cbceec 100644 --- a/src/modules/mavlink/mavlink_receiver.cpp +++ b/src/modules/mavlink/mavlink_receiver.cpp @@ -1797,13 +1797,15 @@ MavlinkReceiver::receive_thread(void *arg) while (!_mavlink->_task_should_exit) { if (poll(&fds[0], 1, timeout) > 0) { if (_mavlink->get_protocol() == SERIAL) { + + /* + * to avoid reading very small chunks wait for data before reading + * this is designed to target ~30 bytes at a time + */ + const unsigned character_count = 20; + /* non-blocking read. read may return negative values */ - if ((nread = ::read(uart_fd, buf, sizeof(buf))) < (ssize_t)sizeof(buf)) { - /* - * to avoid reading very small chunks wait for data before reading - * this is designed to target ~30 bytes at a time - */ - const unsigned character_count = 20; + if ((nread = ::read(uart_fd, buf, sizeof(buf))) < (ssize_t)character_count) { unsigned sleeptime = (1.0f / (_mavlink->get_baudrate() / 10)) * character_count * 1000000; usleep(sleeptime); }