From f8e57b9fe3b35c3412c521d7e6dc40b90cafc857 Mon Sep 17 00:00:00 2001 From: Maxime COZZI Date: Fri, 24 May 2024 11:41:21 +0200 Subject: [PATCH] fix byte count and delay when writing on serial --- .../HAL/NATIVE/serial_network_hal.c | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/network/serial_network/HAL/NATIVE/serial_network_hal.c b/network/serial_network/HAL/NATIVE/serial_network_hal.c index 2696f2002..d66f281a0 100644 --- a/network/serial_network/HAL/NATIVE/serial_network_hal.c +++ b/network/serial_network/HAL/NATIVE/serial_network_hal.c @@ -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(); }