Skip to content

Commit

Permalink
optimize uart read timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Neutree committed May 11, 2024
1 parent bb420a7 commit c0199e4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions components/peripheral/port/linux/maix_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,21 @@ namespace maix::peripheral::uart
int buff_len = len > 0 ? len : 512;
Bytes *data = new Bytes(NULL, buff_len);
int received = 0;
uint64_t t = time::time_ms();
int t2;
while(1)
{
read_len = read(data->data + received, buff_len - received, len > 0 ? len - received : len, timeout);
t2 = (int)(time::time_ms() - t);
if(timeout > 0 && t2 >= timeout)
break;
read_len = read(data->data + received, buff_len - received, len > 0 ? len - received : len, timeout > 0 ? t2 : timeout);
if (read_len < 0)
read_len = 0;
received += read_len;
data->data_len = received;
if(received == buff_len)
if(timeout > 0 && (int)(time::time_ms() - t) >= timeout)
break;
else if(received == buff_len)
{
buff_len = data->data_len * 2;
Bytes *data2 = new Bytes(data->data, buff_len, true, true);
Expand Down

0 comments on commit c0199e4

Please sign in to comment.