8000 nrf: return None when UART.read() reads nothing · litui/circuitpython@3abfd21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3abfd21

Browse files
committed
nrf: return None when UART.read() reads nothing
1 parent 14fc4a0 commit 3abfd21

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

ports/nrf/common-hal/busio/UART.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
337337

338338
NVIC_EnableIRQ(nrfx_get_irq_number(self->uarte->p_reg));
339339

340+
if (rx_bytes == 0) {
341+
*errcode = EAGAIN;
342+
return MP_STREAM_ERROR;
343+
}
344+
340345
return rx_bytes;
341346
}
342347

shared-bindings/busio/UART.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
200200
// These are standard stream methods. Code is in py/stream.c.
201201
//
202202
//| def read(self, nbytes: Optional[int] = None) -> Optional[bytes]:
203-
//| """Read characters. If ``nbytes`` is specified then read at most that many
203+
//| """Read bytes. If ``nbytes`` is specified then read at most that many
204204
//| bytes. Otherwise, read everything that arrives until the connection
205205
//| times out. Providing the number of bytes expected is highly recommended
206-
//| because it will be faster.
206+
//| because it will be faster. If no bytes are read, return ``None``.
207+
//|
208+
//| .. note:: When no bytes are read due to a timeout, this function returns ``None``.
209+
//| This matches the behavior of `io.RawIOBase.read` in Python 3, but
210+
//| differs from pyserial which returns ``b''`` in that situation.
207211
//|
208212
//| :return: Data read
209213
//| :rtype: bytes or None"""

0 commit comments

Comments
 (0)
0