How to use the primary UART

MicroPython provides access to the primary UART via sys.stdin (see sys.stdin limitations) and sys.stdout (and sys.stderr as an alias to sys.stdout). Unlike Python3, MicroPython does not allow overriding stdin, stdout and stderr with other stream objects.

sys.stdin sys.stdin supports standard stream methods read and readline in text mode, converting carriage return (\r) to newline (\n).

Note Do not use the stdin methods readlines or readinto because they will be removed in future firmware.

Use sys.stdin.buffer (instead of sys.stdin) for binary mode without any line ending conversions. The read() method takes a single, optional parameter of the number of bytes to read. For a positive value, read() blocks until receiving that many bytes from the standard stream methods primary UART. For non-blocking, call read() without the parameter (or with a negative value) and it returns whatever characters are available or None if no bytes are waiting.

sys.stdout supports the write() method in text mode, sending an additional carriage return (\r) before each newline (\n). Use sys.stdout.buffer (instead of sys.stdout) for binary mode without any line ending conversions. The write() method buffers its output, and can return before sending all bytes out on the UART.

sys.stdin limitations

Note that sys.stdin provides access to a filtered input stream with the following limitations: