diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c index 04c502a4bd..ae8389fd9e 100644 --- a/ports/nrf/common-hal/busio/UART.c +++ b/ports/nrf/common-hal/busio/UART.c @@ -337,6 +337,11 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t NVIC_EnableIRQ(nrfx_get_irq_number(self->uarte->p_reg)); + if (rx_bytes == 0) { + *errcode = EAGAIN; + return MP_STREAM_ERROR; + } + return rx_bytes; } diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c index 60f2b407fe..d849d63ae8 100644 --- a/shared-bindings/busio/UART.c +++ b/shared-bindings/busio/UART.c @@ -200,10 +200,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_ // These are standard stream methods. Code is in py/stream.c. // //| def read(self, nbytes: Optional[int] = None) -> Optional[bytes]: -//| """Read characters. If ``nbytes`` is specified then read at most that many +//| """Read bytes. If ``nbytes`` is specified then read at most that many //| bytes. Otherwise, read everything that arrives until the connection //| times out. Providing the number of bytes expected is highly recommended -//| because it will be faster. +//| because it will be faster. If no bytes are read, return ``None``. +//| +//| .. note:: When no bytes are read due to a timeout, this function returns ``None``. +//| This matches the behavior of `io.RawIOBase.read` in Python 3, but +//| differs from pyserial which returns ``b''`` in that situation. //| //| :return: Data read //| :rtype: bytes or None"""