Skip to content

Commit

Permalink
Changed Serial readAtLeast timeout from microseconds to milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
katzfey authored and dagar committed Jun 18, 2024
1 parent e33ba81 commit c334e48
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions platforms/common/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ ssize_t Serial::read(uint8_t *buffer, size_t buffer_size)
return _impl.read(buffer, buffer_size);
}

ssize_t Serial::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_us)
ssize_t Serial::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_ms)
{
return _impl.readAtLeast(buffer, buffer_size, character_count, timeout_us);
return _impl.readAtLeast(buffer, buffer_size, character_count, timeout_ms);
}

ssize_t Serial::write(const void *buffer, size_t buffer_size)
Expand Down
2 changes: 1 addition & 1 deletion platforms/common/include/px4_platform_common/Serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Serial
bool close();

ssize_t read(uint8_t *buffer, size_t buffer_size);
ssize_t readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count = 1, uint32_t timeout_us = 0);
ssize_t readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count = 1, uint32_t timeout_ms = 0);

ssize_t write(const void *buffer, size_t buffer_size);

Expand Down
3 changes: 2 additions & 1 deletion platforms/nuttx/src/px4/common/SerialImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ ssize_t SerialImpl::read(uint8_t *buffer, size_t buffer_size)
return ret;
}

ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_us)
ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_ms)
{
if (!_open) {
PX4_ERR("Cannot readAtLeast from serial device until it has been opened");
Expand All @@ -264,6 +264,7 @@ ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t char
}

const hrt_abstime start_time_us = hrt_absolute_time();
hrt_abstime timeout_us = timeout_ms * 1000;
int total_bytes_read = 0;

while ((total_bytes_read < (int) character_count) && (hrt_elapsed_time(&start_time_us) < timeout_us)) {
Expand Down
3 changes: 2 additions & 1 deletion platforms/posix/src/px4/common/SerialImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ssize_t SerialImpl::read(uint8_t *buffer, size_t buffer_size)
return ret;
}

ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_us)
ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_ms)
{
if (!_open) {
PX4_ERR("Cannot readAtLeast from serial device until it has been opened");
Expand All @@ -257,6 +257,7 @@ ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t char
}

const hrt_abstime start_time_us = hrt_absolute_time();
hrt_abstime timeout_us = timeout_ms * 1000;
int total_bytes_read = 0;

while ((total_bytes_read < (int) character_count) && (hrt_elapsed_time(&start_time_us) < timeout_us)) {
Expand Down
3 changes: 2 additions & 1 deletion platforms/qurt/src/px4/SerialImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ ssize_t SerialImpl::read(uint8_t *buffer, size_t buffer_size)
return ret_read;
}

ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_us)
ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t character_count, uint32_t timeout_ms)
{
if (!_open) {
PX4_ERR("Cannot readAtLeast from serial device until it has been opened");
Expand All @@ -186,6 +186,7 @@ ssize_t SerialImpl::readAtLeast(uint8_t *buffer, size_t buffer_size, size_t char
}

const hrt_abstime start_time_us = hrt_absolute_time();
hrt_abstime timeout_us = timeout_ms * 1000;
int total_bytes_read = 0;

while (total_bytes_read < (int) character_count) {
Expand Down

0 comments on commit c334e48

Please sign in to comment.