Skip to content

Commit

Permalink
fix byte count and delay when writing on serial
Browse files Browse the repository at this point in the history
  • Loading branch information
houkhouk committed May 24, 2024
1 parent 9ec9cb7 commit 7208271
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions network/serial_network/HAL/NATIVE/serial_network_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,32 +329,35 @@ void SerialHAL_Send(uint8_t *data, uint16_t size)
LUOS_ASSERT(0);
}
#else

// Check if the output buffer is full
// Check if the output buffer is full
int bytes_in_buffer;
ssize_t totalBytesWritten = 0;
ssize_t bytesWritten;
ioctl(serial_port, TIOCOUTQ, &bytes_in_buffer);
bytesWritten = write(serial_port, data, size);
if (bytesWritten < 0)
if (bytesWritten > 0)
{
printf("Error writing to serial port\n");
close(serial_port);
LUOS_ASSERT(0);
totalBytesWritten += bytesWritten;
}
while (bytesWritten < size)
while (totalBytesWritten < size)
{
// Wait for the buffer to be empty
usleep(1000);
ioctl(serial_port, TIOCOUTQ, &bytes_in_buffer);
bytesWritten += write(serial_port, &data[bytesWritten], size - bytesWritten);
bytesWritten = write(serial_port, &data[bytesWritten], size - bytesWritten);
if (bytesWritten < 0)
{
printf("Error writing to serial port\n");
close(serial_port);
LUOS_ASSERT(0);
}
else
{
usleep(1000);
totalBytesWritten += bytesWritten;
}
}
LUOS_ASSERT(bytesWritten == size);
LUOS_ASSERT(totalBytesWritten == size);
#endif
Serial_TransmissionEnd();
}
Expand Down

0 comments on commit 7208271

Please sign in to comment.